aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-05-01 14:57:52 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-05-01 14:57:52 -0700
commit18d740126ce2b26cf09454eedceb16e9223ad5b0 (patch)
treef341d690e9463e3299ad917c330830653648efa0
parent3342a6b89dfb60001774e4f91d837db23de7e46c (diff)
unpress keys when losing focus
-rw-r--r--src/library_sdl.js17
-rw-r--r--tests/sdl_key_test.c40
2 files changed, 51 insertions, 6 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index db5187ce..911b83e2 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -408,10 +408,6 @@ var LibrarySDL = {
}
SDL.events.push(event);
- if (SDL.events.length >= 10000) {
- Module.printErr('SDL event queue full, dropping earliest event');
- SDL.events.shift();
- }
break;
case 'mouseout':
// Un-press all pressed mouse buttons, because we might miss the release outside of the canvas
@@ -430,6 +426,12 @@ var LibrarySDL = {
case 'blur':
case 'visibilitychange': {
// Un-press all pressed keys: TODO
+ for (var code in SDL.keyboardMap) {
+ SDL.events.push({
+ type: 'keyup',
+ keyCode: SDL.keyboardMap[code]
+ });
+ }
break;
}
case 'unload':
@@ -443,6 +445,10 @@ var LibrarySDL = {
SDL.events.push(event);
break;
}
+ if (SDL.events.length >= 10000) {
+ Module.printErr('SDL event queue full, dropping events');
+ SDL.events = SDL.events.slice(0, 10000);
+ }
return false;
},
@@ -480,10 +486,9 @@ var LibrarySDL = {
{{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.keysym + SDL.structs.keysym.unicode', 'key', 'i32') }}}
var code = SDL.keyCodes[event.keyCode] || event.keyCode;
- var down = event.type == "keydown";
{{{ makeSetValue('SDL.keyboardState', 'code', 'down', 'i8') }}};
if (down) {
- SDL.keyboardMap[code] = 1;
+ SDL.keyboardMap[code] = event.keyCode; // save the DOM input, which we can use to unpress it during blur
} else {
delete SDL.keyboardMap[code];
}
diff --git a/tests/sdl_key_test.c b/tests/sdl_key_test.c
new file mode 100644
index 00000000..ce117209
--- /dev/null
+++ b/tests/sdl_key_test.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_ttf.h>
+#include <emscripten.h>
+
+int result = 1;
+
+int keys[1000];
+
+void one() {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_KEYDOWN:
+ if (!keys[event.key.keysym.sym]) {
+ keys[event.key.keysym.sym] = 1;
+ printf("key down: sym %d scancode %d\n", event.key.keysym.sym, event.key.keysym.scancode);
+ }
+ break;
+ case SDL_KEYUP:
+ if (keys[event.key.keysym.sym]) {
+ keys[event.key.keysym.sym] = 0;
+ printf("key up: sym %d scancode %d\n", event.key.keysym.sym, event.key.keysym.scancode);
+ }
+ break;
+ }
+ }
+}
+
+int main(int argc, char **argv) {
+ memset(keys, 0, 1000*4);
+
+ SDL_Init(SDL_INIT_VIDEO);
+ SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
+
+ emscripten_set_main_loop(one, 0, 0);
+
+ return 0;
+}
+