File: test_keydown_preventdefault_proxy.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (60 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2015 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <stdio.h>
#include <emscripten/emscripten.h>
#include <emscripten/eventloop.h>
#include <emscripten/html5.h>

static int result = 1;

// When running PROXY_TO_WORKER mode that return code of key event handlers
// is not honored (since the handlers are run asyncronously in the worker).
// Instead `shouldPreventDefault` in `proxyClient.js` returns true for all keys
// except backspace and tab.
//
// Therefore where we expect the keypress callback to be prevented for '\b'
// but not for 'A'.

bool keydown_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
  printf("got keydown: %d\n", e->keyCode);
  if ((e->keyCode == 'A') || (e->keyCode == '\b')) {
    result *= 2;
  } else {
    printf("done: result=%d\n", result);
    emscripten_force_exit(result);
  }
  return 0;
}

bool keypress_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
  printf("got keypress: %d\n", e->keyCode);
  // preventDefault should have been set for the backspace key so we should
  // never get that here.
  assert(e->keyCode != '\b');
  result *= 3;
  return 0;
}

bool keyup_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
  printf("got keyup: %d\n", e->keyCode);
  if ((e->keyCode == 'A') || (e->keyCode == '\b')) {
    result *= 5;
  }
  return 0;
}

int main(int argc, char **argv) {
  printf("in main\n");

  emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keydown_callback);
  emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keypress_callback);
  emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 0, 1, keyup_callback);

  emscripten_runtime_keepalive_push();
  return 0;
}