Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_keydown_preventdefault_proxy.c
4150 views
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 <assert.h>
7
#include <stdio.h>
8
#include <emscripten/emscripten.h>
9
#include <emscripten/eventloop.h>
10
#include <emscripten/html5.h>
11
12
static int result = 1;
13
14
// When running PROXY_TO_WORKER mode that return code of key event handlers
15
// is not honored (since the handlers are run asyncronously in the worker).
16
// Instead `shouldPreventDefault` in `proxyClient.js` returns true for all keys
17
// except backspace and tab.
18
//
19
// Therefore where we expect the keypress callback to be prevented for '\b'
20
// but not for 'A'.
21
22
bool keydown_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
23
printf("got keydown: %d\n", e->keyCode);
24
if ((e->keyCode == 'A') || (e->keyCode == '\b')) {
25
result *= 2;
26
} else {
27
printf("done: result=%d\n", result);
28
emscripten_force_exit(result);
29
}
30
return 0;
31
}
32
33
bool keypress_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
34
printf("got keypress: %d\n", e->keyCode);
35
// preventDefault should have been set for the backspace key so we should
36
// never get that here.
37
assert(e->keyCode != '\b');
38
result *= 3;
39
return 0;
40
}
41
42
bool keyup_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
43
printf("got keyup: %d\n", e->keyCode);
44
if ((e->keyCode == 'A') || (e->keyCode == '\b')) {
45
result *= 5;
46
}
47
return 0;
48
}
49
50
int main(int argc, char **argv) {
51
printf("in main\n");
52
53
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keydown_callback);
54
emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keypress_callback);
55
emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keyup_callback);
56
57
emscripten_runtime_keepalive_push();
58
return 0;
59
}
60
61
62