1 // Copyright 2015 The Emscripten Authors.  All rights reserved.
2 // Emscripten is available under two separate licenses, the MIT license and the
3 // University of Illinois/NCSA Open Source License.  Both these licenses can be
4 // found in the LICENSE file.
5 
6 #include <stdio.h>
7 #include <emscripten.h>
8 #include <emscripten/html5.h>
9 static int result = 1;
10 
11 // The event handler functions can return 1 to suppress the event and disable the default action. That calls event.preventDefault();
12 // Returning 0 signals that the event was not consumed by the code, and will allow the event to pass on and bubble up normally.
13 extern "C"
14 {
keydown_callback(int eventType,const EmscriptenKeyboardEvent * e,void * userData)15   EM_BOOL keydown_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData)
16   {
17     if ((e->keyCode == 65) || (e->keyCode == 8))
18     {
19       result *= 2;
20     }
21     else
22     {
23       REPORT_RESULT(result);
24       emscripten_run_script("throw 'done'");
25     }
26     return 0;
27   }
28 }
29 
30 extern "C"
31 {
keypress_callback(int eventType,const EmscriptenKeyboardEvent * e,void * userData)32   EM_BOOL keypress_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData)
33   {
34     result *= 3;
35     return 0;
36   }
37 }
38 
39 extern "C"
40 {
keyup_callback(int eventType,const EmscriptenKeyboardEvent * e,void * userData)41   EM_BOOL keyup_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData)
42   {
43     if ((e->keyCode == 65) || (e->keyCode == 8))
44     {
45       result *= 5;
46     }
47     return 0;
48   }
49 }
50 
main(int argc,char ** argv)51 int main(int argc, char **argv)
52 {
53   printf("main argc:%d\n", argc);
54 
55   emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keydown_callback);
56   emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keypress_callback);
57   emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keyup_callback);
58 
59   return 0;
60 }
61 
62