-
Notifications
You must be signed in to change notification settings - Fork 4
/
KeyboardSendsClicks.html
59 lines (55 loc) · 1.94 KB
/
KeyboardSendsClicks.html
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
<html>
<script>
function clicked(e) {
this.style.backgroundColor="#" + Math.random().toString(16).slice(2, 8);
e.stopPropagation();
}
function adddiv() {
var d = document.body.appendChild(document.createElement("div"));
d.innerText = "hey";
d.onclick = clicked;
}
function init() {
adddiv();
adddiv();
adddiv();
function createMouseEvent(typeString, oldMouseEventTemplate) {
var e = document.createEvent("MouseEvents");
e.initMouseEvent(typeString, true, true, window, 1,
oldMouseEventTemplate.screenX,
oldMouseEventTemplate.screenY,
oldMouseEventTemplate.clientX,
oldMouseEventTemplate.clientY,
false, false, false, false, 0, null);
return e;
};
keyStore = {};
document.body.onmousemove = function mousemove(e) { lastMouse = e; };
document.body.onkeydown = function keydown(e) {
if (e.ctrlKey) {
console.log("Storing a key " + e.keyCode + " " + e.keyIdentifier);
keyStore[e.keyIdentifier] = lastMouse;
e.preventDefault();
} else if (keyStore.hasOwnProperty(e.keyIdentifier)) {
cachedEvent = keyStore[e.keyIdentifier];
//send a synthetic click
emousedown = createMouseEvent("mousedown", cachedEvent);
emouseup = createMouseEvent("mouseup", cachedEvent);
eclick = createMouseEvent("click", cachedEvent);
var target = document.elementFromPoint(cachedEvent.clientX,
cachedEvent.clientY)
console.log("Sending mousedown, mouseup, click, to ", target);
if (target) {
target.dispatchEvent(emousedown);
target.dispatchEvent(emouseup);
target.dispatchEvent(eclick);
}
e.preventDefault();
}
};
}
</script>
<body onload=init();>
Hover the mouse over a location you will send a click.
Press CTRL and then any key.
Move the mouse away, then press that key again to replay the click.