-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleapExtension.js
65 lines (58 loc) · 2.08 KB
/
leapExtension.js
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
window.jQuery || (function addjQuery() {
var jq = document.createElement('script');
jq.src = "https://raw.githubusercontent.com/woojoo666/LeapExtension/master/jquery-2.1.0.min.js";
document.body.appendChild(jq);
})();
var simulateKeyPress = function(character) {
//console.log('Leap gesture: ' + (['left', 'up', 'right', 'down'])[character - 37]);
var eventObj = document.createEvent("Events");
eventObj.initEvent("keydown", true, true);
eventObj.which = character;
(document.activeElement || document.body).dispatchEvent(eventObj);
setTimeout(function() {
var eventObj = document.createEvent("Events");
eventObj.initEvent("keyup", true, true);
eventObj.which = character;
(document.activeElement || document.body).dispatchEvent(eventObj);
}, 50);
};
$(document.body).keydown(function(e) {
console.log(e.target.nodeName + ': keydown');
})
.keyup(function(e) {
console.log(e.target.nodeName + ': keyup');
})
.keypress(function(e) {
console.log(e.target.nodeName + ': keypress');
});
$(document).ready(function() {
simulateKeyPress(37); //test left arrow simulate
var ctl = new Leap.Controller({
enableGestures: true
});
var swiper = ctl.gesture('swipe');
var tolerance = 80;
var cooloff = 50;
var x = 2,
y = 2;
var slider = _.debounce(function(dir) {
simulateKeyPress(dir + 37);
}, cooloff);
swiper.update(function(g) {
if (Math.abs(g.translation()[0]) > tolerance || Math.abs(g.translation()[1]) > tolerance) {
var xDir = Math.abs(g.translation()[0]) > tolerance ? (g.translation()[0] > 0 ? -1 : 1) : 0;
var yDir = Math.abs(g.translation()[1]) > tolerance ? (g.translation()[1] < 0 ? -1 : 1) : 0;
var dir = 0;
if (xDir !== 0) {
if (xDir > 0) dir = 2;
else dir = 0;
} else {
if (yDir > 0) dir = 3;
else dir = 1;
}
slider(dir);
}
});
ctl.connect();
console.log("loaded leap");
});