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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
<!doctype html>
<meta charset=utf-8>
<html>
<head>
<title>Input Events</title>
<style>
div { padding:0px; margin: 0px; }
#trackPointer { position: fixed; }
#resultContainer { width: 600px; height: 60px; }
.area { width: 100px; height: 50px; background-color: #ccc; }
.block { width: 5px; height: 5px; border: solid 1px red; }
#dragArea { position: relative; }
#dragTarget { position: absolute; top:22px; left:47px;}
</style>
<script>
"use strict";
var allEvents = [];
function makeParagraph(message) {
let paragraph = document.createElement("p");
paragraph.textContent = message;
return paragraph;
}
function displayMessage(message) {
let eventNode = document.getElementById("events");
eventNode.innerHTML = ""
eventNode.appendChild(makeParagraph(message));
}
function appendMessage(message) {
document.getElementById("events").appendChild(makeParagraph(message));
}
/**
* Escape |key| if it's in a surrogate-half character range.
*
* Example: given "\ud83d" return "U+d83d".
*
* Otherwise JSON.stringify will convert it to U+FFFD (REPLACEMENT CHARACTER)
* when returning a value from executeScript, for example.
*/
function escapeSurrogateHalf(key) {
if (typeof key !== "undefined" && key.length === 1) {
var charCode = key.charCodeAt(0);
var highSurrogate = charCode >= 0xD800 && charCode <= 0xDBFF;
var surrogate = highSurrogate || (charCode >= 0xDC00 && charCode <= 0xDFFF);
if (surrogate) {
key = "U+" + charCode.toString(16);
}
}
return key;
}
function recordKeyboardEvent(event) {
var key = escapeSurrogateHalf(event.key);
allEvents.push({
"code": event.code,
"key": key,
"which": event.which,
"location": event.location,
"alt": event.altKey,
"ctrl": event.ctrlKey,
"meta": event.metaKey,
"shift": event.shiftKey,
"repeat": event.repeat,
"type": event.type
});
appendMessage(event.type + " " +
"code: " + event.code + ", " +
"key: " + key + ", " +
"which: " + event.which + ", " +
"keyCode: " + event.keyCode);
}
function recordInputEvent(event) {
allEvents.push({
"data": event.data,
"inputType": event.inputType,
"isComposing": event.isComposing,
});
appendMessage("InputEvent " +
"data: " + event.data + ", " +
"inputType: " + event.inputType + ", " +
"isComposing: " + event.isComposing);
}
function recordPointerEvent(event) {
if (event.type === "contextmenu") {
event.preventDefault();
}
allEvents.push({
"type": event.type,
"button": event.button,
"buttons": event.buttons,
"pageX": event.pageX,
"pageY": event.pageY,
"ctrlKey": event.ctrlKey,
"metaKey": event.metaKey,
"altKey": event.altKey,
"shiftKey": event.shiftKey,
"target": event.target.id
});
appendMessage(event.type + " " +
"pageX: " + event.pageX + ", " +
"pageY: " + event.pageY + ", " +
"button: " + event.button + ", " +
"buttons: " + event.buttons + ", " +
"ctrlKey: " + event.ctrlKey + ", " +
"altKey: " + event.altKey + ", " +
"metaKey: " + event.metaKey + ", " +
"shiftKey: " + event.shiftKey + ", " +
"target id: " + event.target.id);
}
function resetEvents() {
allEvents.length = 0;
displayMessage("");
}
document.addEventListener("DOMContentLoaded", function() {
let keyReporter = document.getElementById("keys");
keyReporter.addEventListener("keyup", recordKeyboardEvent);
keyReporter.addEventListener("keypress", recordKeyboardEvent);
keyReporter.addEventListener("keydown", recordKeyboardEvent);
keyReporter.addEventListener("input", recordInputEvent);
let mouseReporter = document.getElementById("pointers");
mouseReporter.addEventListener("click", recordPointerEvent);
mouseReporter.addEventListener("dblclick", recordPointerEvent);
mouseReporter.addEventListener("mousedown", recordPointerEvent);
mouseReporter.addEventListener("mouseup", recordPointerEvent);
mouseReporter.addEventListener("contextmenu", recordPointerEvent);
});
</script>
</head>
<body>
<div id="trackPointer" class="block"></div>
<div>
<h2>KeyReporter</h2>
<input type="text" id="keys" size="80">
</div>
<div>
<h2>ClickReporter</h2>
<div id="pointers" class="area">
</div>
</div>
<div id="resultContainer">
<h2>Events</h2>
<div id="events"></div>
</div>
</body>
</html>
|