2021-06-29 17:15:07 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-10-07 15:31:49 +02:00
|
|
|
import * as tr from "./ftl";
|
2021-06-29 17:15:07 +02:00
|
|
|
import { isApplePlatform } from "./platform";
|
|
|
|
|
2021-06-29 18:32:37 +02:00
|
|
|
// those are the modifiers that Anki works with
|
2021-06-29 17:15:07 +02:00
|
|
|
export type Modifier = "Control" | "Alt" | "Shift" | "Meta";
|
|
|
|
const allModifiers: Modifier[] = ["Control", "Alt", "Shift", "Meta"];
|
|
|
|
|
2021-06-29 18:32:37 +02:00
|
|
|
const platformModifiers: string[] = isApplePlatform()
|
2021-06-29 17:15:07 +02:00
|
|
|
? ["Meta", "Alt", "Shift", "Control"]
|
|
|
|
: ["Control", "Alt", "Shift", "OS"];
|
|
|
|
|
2021-06-29 18:32:37 +02:00
|
|
|
function translateModifierToPlatform(modifier: Modifier): string {
|
|
|
|
return platformModifiers[allModifiers.indexOf(modifier)];
|
|
|
|
}
|
2021-06-29 17:15:07 +02:00
|
|
|
|
2021-11-18 10:18:39 +01:00
|
|
|
const GENERAL_KEY = 0;
|
|
|
|
const NUMPAD_KEY = 3;
|
|
|
|
|
|
|
|
export function checkIfInputKey(event: KeyboardEvent): boolean {
|
|
|
|
return event.location === GENERAL_KEY || event.location === NUMPAD_KEY;
|
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
export function keyboardEventIsPrintableKey(event: KeyboardEvent): boolean {
|
|
|
|
return event.key.length === 1;
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:15:07 +02:00
|
|
|
export const checkModifiers =
|
|
|
|
(required: Modifier[], optional: Modifier[] = []) =>
|
|
|
|
(event: KeyboardEvent): boolean => {
|
|
|
|
return allModifiers.reduce(
|
|
|
|
(
|
|
|
|
matches: boolean,
|
|
|
|
currentModifier: Modifier,
|
2021-10-19 01:06:00 +02:00
|
|
|
currentIndex: number,
|
2021-06-29 17:15:07 +02:00
|
|
|
): boolean =>
|
|
|
|
matches &&
|
|
|
|
(optional.includes(currentModifier as Modifier) ||
|
|
|
|
event.getModifierState(platformModifiers[currentIndex]) ===
|
|
|
|
required.includes(currentModifier)),
|
2021-10-19 01:06:00 +02:00
|
|
|
true,
|
2021-06-29 17:15:07 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-06-29 18:32:37 +02:00
|
|
|
const modifierPressed =
|
|
|
|
(modifier: Modifier) =>
|
|
|
|
(event: MouseEvent | KeyboardEvent): boolean => {
|
|
|
|
const translated = translateModifierToPlatform(modifier);
|
|
|
|
const state = event.getModifierState(translated);
|
|
|
|
return event.type === "keyup"
|
|
|
|
? state && (event as KeyboardEvent).key !== translated
|
|
|
|
: state;
|
|
|
|
};
|
2021-06-29 17:15:07 +02:00
|
|
|
|
2021-06-29 18:32:37 +02:00
|
|
|
export const controlPressed = modifierPressed("Control");
|
|
|
|
export const shiftPressed = modifierPressed("Shift");
|
2022-02-22 13:17:22 +01:00
|
|
|
export const altPressed = modifierPressed("Alt");
|
2021-06-29 17:15:07 +02:00
|
|
|
|
|
|
|
export function modifiersToPlatformString(modifiers: string[]): string {
|
|
|
|
const displayModifiers = isApplePlatform()
|
|
|
|
? ["^", "⌥", "⇧", "⌘"]
|
|
|
|
: [`${tr.keyboardCtrl()}+`, "Alt+", `${tr.keyboardShift()}+`, "Win+"];
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
for (const modifier of modifiers) {
|
|
|
|
result += displayModifiers[platformModifiers.indexOf(modifier)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-07-06 15:52:47 +02:00
|
|
|
|
|
|
|
export function keyToPlatformString(key: string): string {
|
|
|
|
switch (key) {
|
|
|
|
case "Backspace":
|
|
|
|
return "⌫";
|
|
|
|
case "Delete":
|
|
|
|
return "⌦";
|
|
|
|
case "Escape":
|
|
|
|
return "⎋";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|