Initial clean project import
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import {app, BrowserWindow, globalShortcut} from "electron";
|
||||
import {AppsMain} from "../app/main";
|
||||
|
||||
const eventListeners = {};
|
||||
|
||||
// 连续点击的快捷键
|
||||
let continuousKeys = [];
|
||||
const addKeyInput = (key: string, expire = 1000) => {
|
||||
let now = Date.now();
|
||||
continuousKeys.push({key, expire: now + expire});
|
||||
continuousKeys = continuousKeys.filter(item => item.expire > now);
|
||||
for (let i = continuousKeys.length - 1; i >= 0; i--) {
|
||||
const key = continuousKeys
|
||||
.filter((o, oIndex) => oIndex >= i)
|
||||
.map(o => o.key)
|
||||
.join("|");
|
||||
if (eventListeners[key]) {
|
||||
eventListeners[key]();
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const addMultiKeyListener = (keys: string[], callback: Function) => {
|
||||
if (!Array.isArray(keys)) {
|
||||
keys = [keys];
|
||||
}
|
||||
const key = keys.join("|");
|
||||
eventListeners[key] = callback;
|
||||
};
|
||||
|
||||
const createKeyInputListener = (key: string) => {
|
||||
return () => {
|
||||
addKeyInput(key);
|
||||
};
|
||||
};
|
||||
|
||||
const keyMap = {
|
||||
"CommandOrControl+Shift+H": createKeyInputListener("CommandOrControl+Shift+H"),
|
||||
};
|
||||
|
||||
const ready = () => {
|
||||
register();
|
||||
};
|
||||
|
||||
const destroy = () => {
|
||||
globalShortcut.unregisterAll();
|
||||
};
|
||||
|
||||
const register = () => {
|
||||
globalShortcut.unregisterAll();
|
||||
|
||||
app.on("browser-window-focus", () => {
|
||||
for (let key in keyMap) {
|
||||
globalShortcut.register(key, keyMap[key]);
|
||||
}
|
||||
});
|
||||
|
||||
app.on("browser-window-blur", () => {
|
||||
for (let key in keyMap) {
|
||||
globalShortcut.unregister(key);
|
||||
}
|
||||
});
|
||||
|
||||
addMultiKeyListener(["CommandOrControl+Shift+H", "CommandOrControl+Shift+H", "CommandOrControl+Shift+H"], () => {
|
||||
let focusedWindow = BrowserWindow.getFocusedWindow();
|
||||
if (focusedWindow) {
|
||||
if (focusedWindow.webContents.isDevToolsOpened()) {
|
||||
focusedWindow.webContents.closeDevTools();
|
||||
} else {
|
||||
focusedWindow.webContents.openDevTools({
|
||||
mode: "detach",
|
||||
activate: false,
|
||||
title: "FocusedWindow",
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const KeysMain = {
|
||||
register,
|
||||
};
|
||||
|
||||
export default {
|
||||
ready,
|
||||
destroy,
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
export enum HotkeyMouseButtonEnum {
|
||||
LEFT = 1,
|
||||
RIGHT = 2,
|
||||
}
|
||||
|
||||
export type HotkeyKeyItem = {
|
||||
key: string;
|
||||
// Alt Option
|
||||
altKey: boolean;
|
||||
// Ctrl Control
|
||||
ctrlKey: boolean;
|
||||
// Command Win
|
||||
metaKey: boolean;
|
||||
// Shift
|
||||
shiftKey: boolean;
|
||||
times: number;
|
||||
};
|
||||
|
||||
export type HotkeyKeySimpleItem = {
|
||||
type: "Ctrl" | "Alt" | "Meta";
|
||||
times: number;
|
||||
};
|
||||
|
||||
export type HotkeyMouseItem = {
|
||||
button: HotkeyMouseButtonEnum;
|
||||
type: "click" | "longPress";
|
||||
clickTimes?: number;
|
||||
};
|
||||
Reference in New Issue
Block a user