added special key tracking for android and its special way of defining keydown and keyup events

This commit is contained in:
Miodec 2023-04-05 13:09:46 +02:00
parent 8f0f63182e
commit d446fd5b66
2 changed files with 30 additions and 6 deletions

View file

@ -966,14 +966,22 @@ $(document).keydown(async (event) => {
});
$("#wordsInput").keydown((event) => {
if (event.originalEvent?.repeat) return;
setTimeout(() => {
TestInput.recordKeydownTime(event.code);
const isAndroid =
event.key === "Unidentified" && event.code === "" && event.which === 229;
TestInput.recordKeydownTime(isAndroid ? "Android" : event.code);
}, 0);
});
$("#wordsInput").keyup((event) => {
if (event.originalEvent?.repeat) return;
setTimeout(() => {
TestInput.recordKeyupTime(event.code);
const isAndroid =
event.key === "Unidentified" && event.code === "" && event.which === 229;
TestInput.recordKeyupTime(isAndroid ? "Android" : event.code);
}, 0);
});

View file

@ -50,6 +50,7 @@ const keysToTrack = [
"Period",
"Slash",
"Space",
"Android", //smells
];
interface Keypress {
@ -298,10 +299,18 @@ export function forceKeyup(): void {
}
}
let androidIndex = 0;
export function recordKeyupTime(key: string): void {
if (keyDownData[key] === undefined || !keysToTrack.includes(key)) {
return;
if (!keysToTrack.includes(key)) return;
if (key === "Android") {
androidIndex--;
key = "Android" + androidIndex;
}
if (keyDownData[key] === undefined) return;
const now = performance.now();
const diff = Math.abs(keyDownData[key].timestamp - now);
keypressTimings.duration.array[keyDownData[key].index] = diff;
@ -311,9 +320,15 @@ export function recordKeyupTime(key: string): void {
}
export function recordKeydownTime(key: string): void {
if (keyDownData[key] !== undefined || !keysToTrack.includes(key)) {
return;
if (!keysToTrack.includes(key)) return;
if (key === "Android") {
key = "Android" + androidIndex;
androidIndex++;
}
if (keyDownData[key] !== undefined) return;
keyDownData[key] = {
timestamp: performance.now(),
index: keypressTimings.duration.array.length,
@ -369,6 +384,7 @@ export function resetKeypressTimings(): void {
lastStartTime: -1,
};
keyDownData = {};
androidIndex = 0;
if (spacingDebug) console.clear();
}