File: fake_events.js

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 (52 lines) | stat: -rw-r--r-- 1,693 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
/*
 * Helper function used in browser tests to simulate HTML5 events
 */

function simulateKeyEvent(eventType, keyCode, code) {
  var props = { keyCode, charCode: keyCode, view: window, bubbles: true, cancelable: true };
  if (code) props['code'] = code;
  var event = new KeyboardEvent(eventType, props);
  return document.dispatchEvent(event);
}

function simulateKeyDown(keyCode, code = undefined) {
  var doDefault = simulateKeyEvent('keydown', keyCode, code);
  // As long as not handler called `preventDefault` we also send a keypress
  // event.
  if (doDefault) {
    simulateKeyEvent('keypress', keyCode, code);
  }
}

function simulateKeyUp(keyCode, code = undefined) {
  simulateKeyEvent('keyup', keyCode, code);
}

function simulateMouseEvent(x, y, button, absolute) {
  if (!absolute) {
    x += Module['canvas'].offsetLeft;
    y += Module['canvas'].offsetTop;
  }
  var event = document.createEvent("MouseEvents");
  if (button >= 0) {
    var event1 = document.createEvent("MouseEvents");
    event1.initMouseEvent('mousedown', true, true, window,
               1, x, y, x, y,
               0, 0, 0, 0,
               button, null);
    Module['canvas'].dispatchEvent(event1);
    var event2 = document.createEvent("MouseEvents");
    event2.initMouseEvent('mouseup', true, true, window,
               1, x, y, x, y,
               0, 0, 0, 0,
               button, null);
    Module['canvas'].dispatchEvent(event2);
  } else {
    var event1 = document.createEvent("MouseEvents");
    event1.initMouseEvent('mousemove', true, true, window,
               1, x, y, x, y,
               0, 0, 0, 0,
               0, null);
    Module['canvas'].dispatchEvent(event1);
  }
}