-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (64 loc) · 1.59 KB
/
server.js
File metadata and controls
76 lines (64 loc) · 1.59 KB
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
const robot = require("robotjs");
const firebase = require("firebase");
const config = {
apiKey: "AIzaSyBxTjTlNW-AAeiwZ_8xRuIzQjSvVZq2E84",
authDomain: "fir-keypress.firebaseapp.com",
databaseURL: "https://fir-keypress.firebaseio.com",
storageBucket: "fir-keypress.appspot.com",
messagingSenderId: "470329092816"
}
firebase.initializeApp(config)
const db = firebase.database()
const keyPressRef = db.ref('keys')
var lastKey = {}
setInterval(() => {
keyPressRef.limitToLast(1).once('value').then((snapshot) => {
const keyObject = snapshot.val()
const key = keyObject[Object.keys(keyObject)[0]]
// don't keep doing same command
if (lastKey.id === key.id) {
console.log('same as last!')
} else {
console.log('new key:', key)
lastKey = key
console.log(key.key,' ', lastKey.key);
console.log(keyCodeToString(key.key));
const toPress = keyCodeToString(key.key)
if (toPress != null) {
// robot.keyTap(toPress)
tactileTap(toPress)
}
}
})
}, 1000)
// switch statement to make room for additional keys if needed
function keyCodeToString(key) {
switch (key) {
case 'UP':
return 'up'
case 'DOWN':
return 'down'
case 'LEFT':
return 'left'
case 'RIGHT':
return 'right'
case 'A':
return 'a'
case 'B':
return 'b'
case 'X':
return 'x'
case 'Y':
return 'y'
default: {
return null
}
}
}
// Tactile Key Tap.
function tactileTap(key) {
robot.keyToggle(key, "down");
setTimeout(function() {
robot.keyToggle(key, "up");
}, 500);
}