-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
134 lines (121 loc) · 3.59 KB
/
input.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
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
import initSync, { init, check_config, simulate } from './kanata_wasm.js';
let beginBtn = document.getElementById('beginRecording');
let stopBtn = document.getElementById('stopRecording');
let simInput = document.getElementById('siminput');
let cfgEl = document.getElementById('config');
function shiftSpaceInsertsTab(el) {
el.addEventListener('keydown', function (e) {
if (e.keyCode === 32 && e.shiftKey) {
event.preventDefault();
document.execCommand('insertText', false, '\t');
}
}, false);
}
shiftSpaceInsertsTab(cfgEl);
shiftSpaceInsertsTab(simInput);
initSync();
function testConfig() {
init();
var cfg = cfgEl.value;
var out = check_config(cfg)
document.getElementById('result').textContent = out;
}
function simulateInput() {
init();
var cfg = cfgEl.value;
var sim = simInput.value;
var out = simulate(cfg, sim)
out = out.replaceAll(/^t:/gm, 't: ');
out = out.replaceAll('↓(press) ', ' ↓(press) ');
out = out.replaceAll('↑(release) ', ' ↑(release) ');
if (!out.trim()) {
out = 'No output events from simulation.\nMake sure to simulate time via t:<number>';
}
document.getElementById('result').textContent = out;
}
var recordingPress = null;
var recordingRelease = null;
var recordingEndFn = null;
function beginRecording() {
beginBtn.style.display = 'none';
stopBtn.style.display = 'block';
var prevT = null;
var inputStr = "";
var itemCount = 0;
var pressedKey = null;
var recordFnPress = (event) => {
event.preventDefault();
if (prevT != null) {
inputStr = inputStr.concat(`t:${Date.now() - prevT} `);
itemCount++;
if (itemCount >= 8) {
inputStr = inputStr.concat('\n');
itemCount = 0;
}
}
itemCount++;
var code = event.code;
if (!code) {
code = event.key;
}
var action = 'd';
if (pressedKey === code) {
// check for key repeat
action = 'r';
}
inputStr = inputStr.concat(`${action}:${code} `);
prevT = Date.now();
simInput.value = inputStr;
pressedKey = code;
};
var recordFnRelease = (event) => {
event.preventDefault();
if (prevT != null) {
inputStr = inputStr.concat(`t:${Date.now() - prevT} `);
itemCount++;
if (itemCount >= 8) {
inputStr = inputStr.concat('\n');
itemCount = 0;
}
}
itemCount++;
var code = event.code;
if (!code) {
code = event.key;
}
inputStr = inputStr.concat(`u:${code} `);
prevT = Date.now();
simInput.value = inputStr;
pressedKey = null;
};
recordingPress = recordFnPress;
recordingRelease = recordFnRelease;
addEventListener('keydown', recordFnPress);
addEventListener('keyup', recordFnRelease);
// Add extra ending time to help kanata complete pending states.
recordingEndFn = () => {
inputStr = inputStr.concat(`t:9000`);
simInput.value = inputStr;
};
setTimeout(() => stopRecording(recordFnPress, recordFnRelease), 30000);
}
function stopRecording(recordFnPress, recordFnRelease) {
if ( !recordFnPress
|| !recordFnRelease
|| (recordFnPress === recordingPress
&& recordFnRelease === recordingRelease))
{
beginBtn.style.display = 'block';
stopBtn.style.display = 'none';
removeEventListener('keydown', recordingPress);
removeEventListener('keyup', recordingRelease);
recordingPress = null;
recordingRelease = null;
recordingEndFn();
localStorage.setItem('siminput', simInput.value);
}
}
window.testConfig = testConfig;
window.simulateInput = simulateInput;
window.beginRecording = beginRecording;
window.stopRecording = stopRecording;