Skip to content

Commit 655011f

Browse files
authored
Implement SDL_GetKeyName (#22921)
I found this pretty useful during debugging recently.
1 parent 04a03f4 commit 655011f

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/library_sdl.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,9 +1767,16 @@ var LibrarySDL = {
17671767
SDL_GetKeyState: () => _SDL_GetKeyboardState(0),
17681768

17691769
SDL_GetKeyName__proxy: 'sync',
1770-
SDL_GetKeyName__deps: ['$stringToNewUTF8'],
1770+
SDL_GetKeyName__deps: ['$stringToUTF8', 'realloc'],
17711771
SDL_GetKeyName: (key) => {
1772-
SDL.keyName ||= stringToNewUTF8('unknown key');
1772+
var name = '';
1773+
/* ASCII A-Z or 0-9 */
1774+
if ((key >= 97 && key <= 122) || (key >= 48 && key <= 57)) {
1775+
name = String.fromCharCode(key);
1776+
}
1777+
var size = lengthBytesUTF8(name) + 1;
1778+
SDL.keyName = _realloc(SDL.keyName, size);
1779+
stringToUTF8(name, SDL.keyName, size);
17731780
return SDL.keyName;
17741781
},
17751782

test/browser/test_sdl_key_test.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
#include <stdio.h>
99
#include <SDL/SDL.h>
10-
#include <SDL/SDL_ttf.h>
10+
#ifdef __EMSCRIPTEN__
1111
#include <emscripten.h>
12+
#endif
1213

1314
int result = 1;
1415

@@ -21,13 +22,13 @@ void one() {
2122
case SDL_KEYDOWN:
2223
if (!keys[event.key.keysym.sym]) {
2324
keys[event.key.keysym.sym] = 1;
24-
printf("key down: sym %d scancode %d\n", event.key.keysym.sym, event.key.keysym.scancode);
25+
printf("key down: sym=%d scancode=%d name=%s\n", event.key.keysym.sym, event.key.keysym.scancode, SDL_GetKeyName(event.key.keysym.sym));
2526
}
2627
break;
2728
case SDL_KEYUP:
2829
if (keys[event.key.keysym.sym]) {
2930
keys[event.key.keysym.sym] = 0;
30-
printf("key up: sym %d scancode %d\n", event.key.keysym.sym, event.key.keysym.scancode);
31+
printf("key up: sym=%d scancode=%d name=%s\n", event.key.keysym.sym, event.key.keysym.scancode, SDL_GetKeyName(event.key.keysym.sym));
3132
}
3233
break;
3334
}
@@ -40,7 +41,11 @@ int main(int argc, char **argv) {
4041
SDL_Init(SDL_INIT_VIDEO);
4142
SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
4243

44+
#if __EMSCRIPTEN__
4345
emscripten_set_main_loop(one, 0, 0);
46+
#else
47+
while (1) one();
48+
#endif
4449

4550
return 0;
4651
}

test/test_other.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,28 @@ def test_sdl_scan_code_from_key(self):
23922392
''')
23932393
self.do_runf('main.c', '204\n')
23942394

2395+
def test_sdl_get_key_name(self):
2396+
create_file('main.c', r'''
2397+
#include <stdio.h>
2398+
#include <SDL/SDL_keyboard.h>
2399+
2400+
int main() {
2401+
printf("a -> '%s'\n", SDL_GetKeyName(SDLK_a));
2402+
printf("z -> '%s'\n", SDL_GetKeyName(SDLK_z));
2403+
printf("0 -> '%s'\n", SDL_GetKeyName(SDLK_0));
2404+
printf("0 -> '%s'\n", SDL_GetKeyName(SDLK_9));
2405+
printf("F1 -> '%s'\n", SDL_GetKeyName(SDLK_F1));
2406+
return 0;
2407+
}
2408+
''')
2409+
self.do_runf('main.c', '''\
2410+
a -> 'a'
2411+
z -> 'z'
2412+
0 -> '0'
2413+
0 -> '9'
2414+
F1 -> ''
2415+
''')
2416+
23952417
@requires_network
23962418
def test_sdl2_mixer_wav(self):
23972419
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['-sUSE_SDL_MIXER=2'], output_filename='a.out.js')

0 commit comments

Comments
 (0)