summaryrefslogtreecommitdiff
path: root/textscreen
diff options
context:
space:
mode:
Diffstat (limited to 'textscreen')
-rw-r--r--textscreen/txt_main.h14
-rw-r--r--textscreen/txt_sdl.c60
2 files changed, 74 insertions, 0 deletions
diff --git a/textscreen/txt_main.h b/textscreen/txt_main.h
index 601548e5..a415ee1b 100644
--- a/textscreen/txt_main.h
+++ b/textscreen/txt_main.h
@@ -69,6 +69,16 @@ typedef enum
TXT_COLOR_BRIGHT_WHITE,
} txt_color_t;
+// Modifier keys.
+
+typedef enum
+{
+ TXT_MOD_SHIFT,
+ TXT_MOD_CTRL,
+ TXT_MOD_ALT,
+ TXT_NUM_MODIFIERS
+} txt_modifier_t;
+
// Initialize the screen
// Returns 1 if successful, 0 if failed.
@@ -94,6 +104,10 @@ void TXT_UpdateScreen(void);
int TXT_GetChar(void);
+// Read the current state of modifier keys that are held down.
+
+int TXT_GetModifierState(txt_modifier_t mod);
+
// Provides a short description of a key code, placing into the
// provided buffer.
diff --git a/textscreen/txt_sdl.c b/textscreen/txt_sdl.c
index 5ae151e9..767c9b3e 100644
--- a/textscreen/txt_sdl.c
+++ b/textscreen/txt_sdl.c
@@ -63,6 +63,8 @@ static int key_mapping = 1;
static TxtSDLEventCallbackFunc event_callback;
static void *event_callback_data;
+static int modifier_state[TXT_NUM_MODIFIERS];
+
// Font we are using:
static txt_font_t *font;
@@ -493,6 +495,48 @@ static int MouseHasMoved(void)
}
}
+// Examine a key press/release and update the modifier key state
+// if necessary.
+
+static void UpdateModifierState(SDL_keysym *sym, int pressed)
+{
+ txt_modifier_t mod;
+
+ switch (sym->sym)
+ {
+ case SDLK_LSHIFT:
+ case SDLK_RSHIFT:
+ mod = TXT_MOD_SHIFT;
+ break;
+
+ case SDLK_LCTRL:
+ case SDLK_RCTRL:
+ mod = TXT_MOD_CTRL;
+ break;
+
+ case SDLK_LALT:
+ case SDLK_RALT:
+#if !SDL_VERSION_ATLEAST(1, 3, 0)
+ case SDLK_LMETA:
+ case SDLK_RMETA:
+#endif
+ mod = TXT_MOD_ALT;
+ break;
+
+ default:
+ return;
+ }
+
+ if (pressed)
+ {
+ ++modifier_state[mod];
+ }
+ else
+ {
+ --modifier_state[mod];
+ }
+}
+
signed int TXT_GetChar(void)
{
SDL_Event ev;
@@ -522,8 +566,14 @@ signed int TXT_GetChar(void)
break;
case SDL_KEYDOWN:
+ UpdateModifierState(&ev.key.keysym, 1);
+
return TranslateKey(&ev.key.keysym);
+ case SDL_KEYUP:
+ UpdateModifierState(&ev.key.keysym, 0);
+ break;
+
case SDL_QUIT:
// Quit = escape
return 27;
@@ -542,6 +592,16 @@ signed int TXT_GetChar(void)
return -1;
}
+int TXT_GetModifierState(txt_modifier_t mod)
+{
+ if (mod < TXT_NUM_MODIFIERS)
+ {
+ return modifier_state[mod] > 0;
+ }
+
+ return 0;
+}
+
static const char *SpecialKeyName(int key)
{
switch (key)