Path: blob/main/test/browser/test_keydown_preventdefault_proxy.c
4150 views
// Copyright 2015 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45#include <assert.h>6#include <stdio.h>7#include <emscripten/emscripten.h>8#include <emscripten/eventloop.h>9#include <emscripten/html5.h>1011static int result = 1;1213// When running PROXY_TO_WORKER mode that return code of key event handlers14// is not honored (since the handlers are run asyncronously in the worker).15// Instead `shouldPreventDefault` in `proxyClient.js` returns true for all keys16// except backspace and tab.17//18// Therefore where we expect the keypress callback to be prevented for '\b'19// but not for 'A'.2021bool keydown_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {22printf("got keydown: %d\n", e->keyCode);23if ((e->keyCode == 'A') || (e->keyCode == '\b')) {24result *= 2;25} else {26printf("done: result=%d\n", result);27emscripten_force_exit(result);28}29return 0;30}3132bool keypress_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {33printf("got keypress: %d\n", e->keyCode);34// preventDefault should have been set for the backspace key so we should35// never get that here.36assert(e->keyCode != '\b');37result *= 3;38return 0;39}4041bool keyup_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {42printf("got keyup: %d\n", e->keyCode);43if ((e->keyCode == 'A') || (e->keyCode == '\b')) {44result *= 5;45}46return 0;47}4849int main(int argc, char **argv) {50printf("in main\n");5152emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keydown_callback);53emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keypress_callback);54emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keyup_callback);5556emscripten_runtime_keepalive_push();57return 0;58}59606162