use ahook keypress
This commit is contained in:
1 parent
d462958c1b
commit
3be15ee116
6 files changed
+132
-174
No files matched your search
+28
-43
@@ -1,12 +1,5 @@
|
||||
import { BrowserWindow } from "@electron/remote";
|
||||
import { MutableRefObject } from "react";
|
||||
import {
|
||||
HotkeyCallback,
|
||||
Keys,
|
||||
isHotkeyPressed as _isHotkeyPressed,
|
||||
useHotkeys as _useHotkeys,
|
||||
} from "react-hotkeys-hook";
|
||||
import { OptionsOrDependencyArray, RefType } from "react-hotkeys-hook/dist/types";
|
||||
import useKeyPress, { KeyFilter, KeyType, Options, Target } from "ahooks/lib/useKeyPress";
|
||||
import { Key } from "ts-key-enum";
|
||||
|
||||
export const isMacos = process.platform === "darwin";
|
||||
@@ -20,49 +13,41 @@ const hotkey = (key: string) => {
|
||||
return key.toLowerCase();
|
||||
};
|
||||
|
||||
export const isHotkeyPressed = (key: string) => {
|
||||
if (!key) {
|
||||
return false;
|
||||
} else if (key.indexOf("ctrl") >= 0 && isMacos) {
|
||||
key = key.replace("ctrl", Key.Meta);
|
||||
}
|
||||
return _isHotkeyPressed(key.toLowerCase(), "+");
|
||||
export const useKeyUp = (
|
||||
keyFilter: KeyFilter,
|
||||
target: Target,
|
||||
eventHandler: (event: KeyboardEvent, key: KeyType) => void,
|
||||
option?: Options
|
||||
) => {
|
||||
option = option || {};
|
||||
option.events = ["keyup"];
|
||||
option.target = target;
|
||||
option.exactMatch = true;
|
||||
return useKeyPress(keyFilter, eventHandler, option);
|
||||
};
|
||||
|
||||
export function useHotkeys<T extends HTMLElement>(
|
||||
keys: Keys,
|
||||
callback: HotkeyCallback,
|
||||
options?: OptionsOrDependencyArray,
|
||||
dependencies?: OptionsOrDependencyArray
|
||||
): MutableRefObject<RefType<T>> {
|
||||
if (keys instanceof Array) {
|
||||
keys = keys.filter((v) => !!v);
|
||||
}
|
||||
return _useHotkeys(keys, callback, options, dependencies);
|
||||
}
|
||||
|
||||
export const Hotkey = {
|
||||
Backspace: Key.Backspace,
|
||||
Build: hotkey("ctrl+b"),
|
||||
CloseEditor: hotkey("ctrl+w"),
|
||||
Copy: hotkey("ctrl+c"),
|
||||
Cut: hotkey('ctrl+x'),
|
||||
Build: hotkey("ctrl.b"),
|
||||
CloseEditor: hotkey("ctrl.w"),
|
||||
Copy: hotkey("ctrl.c"),
|
||||
Cut: hotkey("ctrl.x"),
|
||||
Delete: Key.Delete,
|
||||
Duplicate: hotkey("ctrl+d"),
|
||||
Duplicate: hotkey("ctrl.d"),
|
||||
Enter: Key.Enter,
|
||||
Escape: Key.Escape,
|
||||
F2: Key.F2,
|
||||
Insert: Key.Insert,
|
||||
JumpNode: hotkey("ctrl+g"),
|
||||
MacDelete: isMacos ? hotkey("ctrl+backspace") : "",
|
||||
Paste: hotkey("ctrl+v"),
|
||||
Redo: isMacos ? hotkey("shift+ctrl+z") : hotkey("ctrl+y"),
|
||||
Replace: hotkey("shift+ctrl+v"),
|
||||
Save: hotkey("ctrl+s"),
|
||||
SearchTree: hotkey("ctrl+p"),
|
||||
SearchNode: hotkey("ctrl+f"),
|
||||
SelectAll: hotkey('ctrl+a'),
|
||||
Undo: hotkey("ctrl+z"),
|
||||
JumpNode: hotkey("ctrl.g"),
|
||||
MacDelete: isMacos ? hotkey("ctrl.backspace") : "",
|
||||
Paste: hotkey("ctrl.v"),
|
||||
Redo: isMacos ? hotkey("shift.ctrl.z") : hotkey("ctrl.y"),
|
||||
Replace: hotkey("shift.ctrl.v"),
|
||||
Save: hotkey("ctrl.s"),
|
||||
SearchTree: hotkey("ctrl.p"),
|
||||
SearchNode: hotkey("ctrl.f"),
|
||||
SelectAll: hotkey("ctrl.a"),
|
||||
Undo: hotkey("ctrl.z"),
|
||||
};
|
||||
|
||||
export const setInputFocus = (target: HTMLElement) => {
|
||||
@@ -74,7 +59,7 @@ export const sendInputEvent = (key: string) => {
|
||||
if (webContents) {
|
||||
let keyCode: string | undefined;
|
||||
const modifiers: Electron.InputEvent["modifiers"] = [];
|
||||
key.split("+").forEach((v) => {
|
||||
key.split(".").forEach((v) => {
|
||||
if (v.indexOf("ctrl") >= 0) {
|
||||
modifiers.push("ctrl");
|
||||
} else if (v.indexOf("shift") >= 0) {
|
||||
|
||||
Reference in new issue
Block a user