add custom key repeat handler to allow for user-configurable, mode-dependent key repeats

This commit is contained in:
Josh Holtrop 2016-07-10 21:23:49 -04:00
parent dd7a5de619
commit 5bcd55ba97
3 changed files with 71 additions and 12 deletions

View File

@ -6,6 +6,8 @@
#define INITIAL_HEIGHT 500 #define INITIAL_HEIGHT 500
#define FONT_SIZE 16 #define FONT_SIZE 16
bool Key_Statuses[SDL_NUM_SCANCODES];
/** /**
* Initialize SDL. * Initialize SDL.
* *
@ -21,7 +23,7 @@ static bool Initialize_SDL()
return true; return true;
} }
if (SDL_Init(SDL_INIT_VIDEO) != 0) if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
{ {
return false; return false;
} }
@ -133,6 +135,26 @@ void Window::run_event_loop()
} }
} }
Uint32 Key_Repeat(Uint32 interval, void * param)
{
if (Key_Statuses[(uintptr_t)param])
{
SDL_Event event;
event.user.code = 0;
event.user.data1 = param;
event.type = SDL_USEREVENT;
SDL_PushEvent(&event);
return 25u;
}
else
{
return 0u;
}
}
/** /**
* Handle a SDL event. * Handle a SDL event.
*/ */
@ -145,28 +167,33 @@ void Window::handle_event(SDL_Event & event)
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
switch (event.key.keysym.sym) Key_Statuses[event.key.keysym.scancode] = true;
switch (event.key.keysym.scancode)
{ {
case SDLK_ESCAPE: case SDL_SCANCODE_ESCAPE:
m_exit_requested = true; m_exit_requested = true;
break; break;
case SDLK_j: case SDL_SCANCODE_J:
if (m_line < m_buffer->get_num_lines() - 1) if (event.key.repeat == 0u)
{ {
m_line++; SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode);
redraw(); scroll_down();
} }
break; break;
case SDLK_k: case SDL_SCANCODE_K:
if (m_line > 0) if (event.key.repeat == 0u)
{ {
m_line--; SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode);
redraw(); scroll_up();
} }
break; break;
} }
break; break;
case SDL_KEYUP:
Key_Statuses[event.key.keysym.scancode] = false;
break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
switch (event.window.event) switch (event.window.event)
{ {
@ -179,6 +206,36 @@ void Window::handle_event(SDL_Event & event)
break; break;
} }
break; break;
case SDL_USEREVENT:
switch ((uintptr_t)event.user.data1)
{
case SDL_SCANCODE_J:
scroll_down();
break;
case SDL_SCANCODE_K:
scroll_up();
break;
}
break;
}
}
void Window::scroll_down()
{
if (m_line < m_buffer->get_num_lines() - 1)
{
m_line++;
redraw();
}
}
void Window::scroll_up()
{
if (m_line > 0)
{
m_line--;
redraw();
} }
} }

View File

@ -16,6 +16,8 @@ protected:
void resize(); void resize();
void redraw(); void redraw();
void handle_event(SDL_Event & event); void handle_event(SDL_Event & event);
void scroll_down();
void scroll_up();
SDL_Window * m_window; SDL_Window * m_window;
bool m_exit_requested; bool m_exit_requested;

View File

@ -24,7 +24,7 @@ def build(bld):
source = bld.path.ant_glob(["src/**/*.cc", "src/**/*.c", "libs/glcxx/src/glcxx/*"]), source = bld.path.ant_glob(["src/**/*.cc", "src/**/*.c", "libs/glcxx/src/glcxx/*"]),
includes = includes, includes = includes,
defines = ['GLCXX_GL_INCLUDE="gl3w.h"'] + defines, defines = ['GLCXX_GL_INCLUDE="gl3w.h"'] + defines,
cxxflags = ["-Wall", "-std=gnu++14", "-O2"], cxxflags = ["-Wall", "-std=gnu++14", "-O2", "-Wno-switch"],
lib = ["dl", "GL"], lib = ["dl", "GL"],
uselib = ["SDL2", "FreeType2"]) uselib = ["SDL2", "FreeType2"])