aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancesco <francesco.bbt@gmail.com>2021-09-17 22:38:09 +0200
committerGitHub <noreply@github.com>2021-09-17 22:38:09 +0200
commitc018ca3c6061da7360081d7df0d23c5fc9c6997b (patch)
tree9340c659bd5da5d696e1e3aad26cc673082d0763
parentf6b66348681e1d41baa86c3da091f97ef69299c9 (diff)
downloadlite-xl-c018ca3c6061da7360081d7df0d23c5fc9c6997b.tar.gz
lite-xl-c018ca3c6061da7360081d7df0d23c5fc9c6997b.zip
Fix numpad fn keys (#532)
* Fix the numeric keypad function keys As suggested in: https://github.com/lite-xl/lite-xl/issues/64 * Apply scancode lookup to KEY_UP events
-rw-r--r--src/api/system.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/api/system.c b/src/api/system.c
index 2f1bf763..f69de477 100644
--- a/src/api/system.c
+++ b/src/api/system.c
@@ -26,14 +26,11 @@ static const char* button_name(int button) {
}
-static char* key_name(char *dst, int sym) {
- strcpy(dst, SDL_GetKeyName(sym));
- char *p = dst;
+static void str_tolower(char *p) {
while (*p) {
*p = tolower(*p);
p++;
}
- return dst;
}
struct HitTestInfo {
@@ -93,6 +90,23 @@ static SDL_HitTestResult SDLCALL hit_test(SDL_Window *window, const SDL_Point *p
return SDL_HITTEST_NORMAL;
}
+static const char *numpad[] = { "end", "down", "pagedown", "left", "", "right", "home", "up", "pageup", "ins", "delete" };
+
+static const char *get_key_name(const SDL_Event *e, char *buf) {
+ SDL_Scancode scancode = e->key.keysym.scancode;
+ /* Is the scancode from the keypad and the number-lock off?
+ ** We assume that SDL_SCANCODE_KP_1 up to SDL_SCANCODE_KP_9 and SDL_SCANCODE_KP_0
+ ** and SDL_SCANCODE_KP_PERIOD are declared in SDL2 in that order. */
+ if (scancode >= SDL_SCANCODE_KP_1 && scancode <= SDL_SCANCODE_KP_1 + 10 &&
+ !(KMOD_NUM & SDL_GetModState())) {
+ return numpad[scancode - SDL_SCANCODE_KP_1];
+ } else {
+ strcpy(buf, SDL_GetKeyName(e->key.keysym.sym));
+ str_tolower(buf);
+ return buf;
+ }
+}
+
static int f_poll_event(lua_State *L) {
char buf[16];
int mx, my, wx, wy;
@@ -162,7 +176,7 @@ top:
}
#endif
lua_pushstring(L, "keypressed");
- lua_pushstring(L, key_name(buf, e.key.keysym.sym));
+ lua_pushstring(L, get_key_name(&e, buf));
return 2;
case SDL_KEYUP:
@@ -176,7 +190,7 @@ top:
}
#endif
lua_pushstring(L, "keyreleased");
- lua_pushstring(L, key_name(buf, e.key.keysym.sym));
+ lua_pushstring(L, get_key_name(&e, buf));
return 2;
case SDL_TEXTINPUT: