add custom key repeat handler to allow for user-configurable, mode-dependent key repeats
This commit is contained in:
1 parent
dd7a5de619
commit
5bcd55ba97
3 files changed
+71
-12
No files matched your search
+68
-11
@@ -6,6 +6,8 @@
|
||||
#define INITIAL_HEIGHT 500
|
||||
#define FONT_SIZE 16
|
||||
|
||||
bool Key_Statuses[SDL_NUM_SCANCODES];
|
||||
|
||||
/**
|
||||
* Initialize SDL.
|
||||
*
|
||||
@@ -21,7 +23,7 @@ static bool Initialize_SDL()
|
||||
return true;
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0)
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
|
||||
{
|
||||
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.
|
||||
*/
|
||||
@@ -145,28 +167,33 @@ void Window::handle_event(SDL_Event & event)
|
||||
break;
|
||||
|
||||
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;
|
||||
break;
|
||||
case SDLK_j:
|
||||
if (m_line < m_buffer->get_num_lines() - 1)
|
||||
case SDL_SCANCODE_J:
|
||||
if (event.key.repeat == 0u)
|
||||
{
|
||||
m_line++;
|
||||
redraw();
|
||||
SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode);
|
||||
scroll_down();
|
||||
}
|
||||
break;
|
||||
case SDLK_k:
|
||||
if (m_line > 0)
|
||||
case SDL_SCANCODE_K:
|
||||
if (event.key.repeat == 0u)
|
||||
{
|
||||
m_line--;
|
||||
redraw();
|
||||
SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode);
|
||||
scroll_up();
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_KEYUP:
|
||||
Key_Statuses[event.key.keysym.scancode] = false;
|
||||
break;
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (event.window.event)
|
||||
{
|
||||
@@ -179,6 +206,36 @@ void Window::handle_event(SDL_Event & event)
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ protected:
|
||||
void resize();
|
||||
void redraw();
|
||||
void handle_event(SDL_Event & event);
|
||||
void scroll_down();
|
||||
void scroll_up();
|
||||
|
||||
SDL_Window * m_window;
|
||||
bool m_exit_requested;
|
||||
|
||||
Reference in new issue
Block a user