From bafcc12c95bd4491ba0f12cdc481958d1e6f5a25 Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 24 Sep 2016 17:02:09 +0100 Subject: [PATCH] Changed keyboard callbacks to pass key aswell as scancode --- src/keyboard.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++- src/main.c | 4 +- 2 files changed, 144 insertions(+), 5 deletions(-) diff --git a/src/keyboard.c b/src/keyboard.c index 9b8679c..5ba8fc9 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -91,6 +91,137 @@ void keyboard_deinit(void) { * Lua binds */ +const char *scancodeMap[] = { + [ 1] = "escape", + [ 2] = "1", + [ 3] = "2", + [ 4] = "3", + [ 5] = "4", + [ 6] = "5", + [ 7] = "6", + [ 8] = "7", + [ 9] = "8", + [ 10] = "9", + [ 11] = "0", + [ 12] = "-", + [ 13] = "=", + [ 14] = "backspace", + [ 15] = "tab", + [ 16] = "q", + [ 17] = "w", + [ 18] = "e", + [ 19] = "r", + [ 20] = "t", + [ 21] = "y", + [ 22] = "u", + [ 23] = "i", + [ 24] = "o", + [ 25] = "p", + [ 26] = "[", + [ 27] = "]", + [ 28] = "enter", + [ 29] = "lctrl", + [ 30] = "a", + [ 31] = "s", + [ 32] = "d", + [ 33] = "f", + [ 34] = "g", + [ 35] = "h", + [ 36] = "j", + [ 37] = "k", + [ 38] = "l", + [ 39] = ";", + [ 40] = "\"", + [ 41] = "`", + [ 42] = "lshift", + [ 43] = "\\", + [ 44] = "z", + [ 45] = "x", + [ 46] = "c", + [ 47] = "v", + [ 48] = "b", + [ 49] = "n", + [ 50] = "m", + [ 51] = ",", + [ 52] = ".", + [ 53] = "/", + [ 54] = "rshift", + [ 55] = "*", + [ 56] = "lalt", + [ 57] = " ", + [ 58] = "capslock", + [ 59] = "f1", + [ 60] = "f2", + [ 61] = "f3", + [ 62] = "f4", + [ 63] = "f5", + [ 64] = "f6", + [ 65] = "f7", + [ 66] = "f8", + [ 67] = "f9", + [ 68] = "f10", + [ 69] = "numlock", + [ 70] = "scrolllock", + [ 71] = "home", + [ 72] = "up", + [ 73] = "pageup", + [ 74] = "-", + [ 75] = "left", + [ 76] = "5", + [ 77] = "right", + [ 79] = "end", + [ 78] = "+", + [ 80] = "down", + [ 81] = "pagedown", + [ 82] = "insert", + [ 83] = "delete", + [ 84] = "?", + [ 85] = "?", + [ 86] = "?", + [ 87] = "?", + [ 88] = "?", + [ 89] = "?", + [ 90] = "?", + [ 91] = "?", + [ 92] = "?", + [ 93] = "?", + [ 94] = "?", + [ 95] = "?", + [ 96] = "enter", + [ 97] = "lctrl", + [ 98] = "/", + [ 99] = "f12", + [100] = "rctrl", + [101] = "pause", + [102] = "home", + [103] = "up", + [104] = "pageup", + [105] = "left", + [106] = "right", + [107] = "end", + [108] = "down", + [109] = "pagedown", + [110] = "insert", + [111] = "?", + [112] = "?", + [113] = "?", + [114] = "?", + [115] = "?", + [116] = "?", + [117] = "?", + [118] = "?", + [119] = "pause", + [120] = "?", + [121] = "?", + [122] = "?", + [123] = "?", + [124] = "?", + [125] = "?", + [126] = "?", + [127] = "?", + [128] = NULL, +}; + int l_keyboard_isDown(lua_State *L) { int code = luaL_checknumber(L, 1); @@ -103,13 +234,21 @@ int l_keyboard_isDown(lua_State *L) { int l_keyboard_poll(lua_State *L) { lua_newtable(L); int idx = 1; + while (keyboard_events.readi != keyboard_events.writei) { lua_newtable(L); - lua_pushstring(L, keyboard_events.data[keyboard_events.readi & 31].type - ? "down" : "up"); + + int type = keyboard_events.data[keyboard_events.readi & 31].type; + int code = keyboard_events.data[keyboard_events.readi & 31].code; + code &= 127; + + lua_pushstring(L, type ? "down" : "up"); lua_setfield(L, -2, "type"); - lua_pushnumber(L, keyboard_events.data[keyboard_events.readi & 31].code); + lua_pushnumber(L, code); lua_setfield(L, -2, "code"); + lua_pushstring(L, scancodeMap[code]); + lua_setfield(L, -2, "key"); + lua_rawseti(L, -2, idx); keyboard_events.readi++; idx++; diff --git a/src/main.c b/src/main.c index 9f91e43..6b917ef 100644 --- a/src/main.c +++ b/src/main.c @@ -77,9 +77,9 @@ int main(void) { /* Keyboard events */ "for _, e in ipairs(love.keyboard.poll()) do\n" "if e.type == 'down' then\n" - "if love.keypressed then love.keypressed(e.code) end\n" + "if love.keypressed then love.keypressed(e.key, e.code) end\n" "else\n" - "if love.keyreleased then love.keyreleased(e.code) end\n" + "if love.keyreleased then love.keyreleased(e.key, e.code) end\n" "end\n" "end\n" /* Update */