Record a new Hotkey
>(new Set());
const isKeybindInputFocused = ref(false);
@@ -61,6 +67,7 @@ const lastBlurTime = ref(0);
const os = ref("");
const router = useRouter();
const keyboard = useKeyboard();
+const showEmptyKeybindError = ref(false);
const keyToDisplayMap: Record = {
" ": "Space",
@@ -115,16 +122,17 @@ const updateKeybind = () => {
const onBlur = () => {
isKeybindInputFocused.value = false;
lastBlurTime.value = Date.now();
+ showEmptyKeybindError.value = false;
};
const onFocus = () => {
isKeybindInputFocused.value = true;
activeModifiers.clear();
keybind.value = [];
+ showEmptyKeybindError.value = false;
};
const onKeyDown = (event: KeyboardEvent) => {
- event.preventDefault();
const key = event.code;
if (key === "Escape") {
@@ -142,57 +150,63 @@ const onKeyDown = (event: KeyboardEvent) => {
}
updateKeybind();
+ showEmptyKeybindError.value = false;
};
const saveKeybind = async () => {
- console.log("New:", keybind.value);
- const oldKeybind = await invoke("get_keybind");
- console.log("Old:", oldKeybind);
- await invoke("save_keybind", { keybind: keybind.value });
+ if (keybind.value.length > 0) {
+ console.log("Saving keybind", keybind.value);
+ await invoke("save_keybind", { keybind: keybind });
+ } else {
+ showEmptyKeybindError.value = true;
+ }
};
+os.value = platform();
+
onMounted(() => {
- os.value = platform();
-
- keyboard.down("MetaLeft+Enter", (event) => {
- if (os.value === "macos" && !isKeybindInputFocused.value) {
- console.log("Save on macOS")
- event.preventDefault()
- saveKeybind()
- }
- })
-
- keyboard.down("MetaRight+Enter", (event) => {
- if (os.value === "macos" && !isKeybindInputFocused.value) {
- console.log("Save on macOS")
- event.preventDefault()
- saveKeybind()
- }
- })
-
- keyboard.down("ControlLeft+Enter", (event) => {
- if (os.value !== "macos" && !isKeybindInputFocused.value) {
- console.log("Save on other OS")
- event.preventDefault()
- saveKeybind()
- }
- })
-
- keyboard.down("ControlRight+Enter", (event) => {
- if (os.value !== "macos" && !isKeybindInputFocused.value) {
- console.log("Save on other OS")
- event.preventDefault()
- saveKeybind()
- }
- })
-
- keyboard.down("Escape", (event) => {
- console.log("Escape");
- if (!isKeybindInputFocused.value) {
- event.preventDefault();
+ keyboard.down([Key.Escape], (event) => {
+ console.log("Escape key pressed");
+ if (isKeybindInputFocused.value) {
+ keybindInput.value?.blur();
+ } else {
router.push("/");
}
});
+
+ switch (os.value) {
+ case "macos":
+ keyboard.down([Key.LeftMeta, Key.Enter], (event) => {
+ if (!isKeybindInputFocused.value) {
+ saveKeybind();
+ }
+ });
+
+ keyboard.down([Key.RightMeta, Key.Enter], (event) => {
+ if (!isKeybindInputFocused.value) {
+ saveKeybind();
+ }
+ });
+ break;
+
+ case "linux" || "windows":
+ keyboard.down([Key.LeftControl, Key.Enter], (event) => {
+ if (!isKeybindInputFocused.value) {
+ saveKeybind();
+ }
+ });
+
+ keyboard.down([Key.RightControl, Key.Enter], (event) => {
+ if (!isKeybindInputFocused.value) {
+ saveKeybind();
+ }
+ });
+ break;
+ }
+});
+
+onUnmounted(() => {
+ keyboard.unregisterAll();
});