Browse Source

快捷键注册和使用

DESKTOP-MINPJAU\Administrator 3 years ago
parent
commit
57ac6b56ac
1 changed files with 31 additions and 0 deletions
  1. 31 0
      src/utils/xckeydown.js

+ 31 - 0
src/utils/xckeydown.js

@@ -0,0 +1,31 @@
+let registerShortcuts = null
+let shortcutFunction = null
+
+export const xcHotKey = (keyList) => {
+    registerShortcuts = keyList
+    shortcutFunction = document.onkeydown = function (event) {
+        shortcutTrigger(event)
+    }
+}
+
+export const logoutShortcut = () => {
+    shortcutFunction = null
+}
+
+/**
+ * 快捷键触发事件
+ * @param event
+ */
+const shortcutTrigger = (event) => {
+    for (let listKey in registerShortcuts) {
+        let data = registerShortcuts[listKey]
+        for (let key in data) {
+            // 同时按下
+            let pressSimultaneously = listKey + 'Key'
+            if (event[pressSimultaneously] && event.key === key) {
+                event.returnValue = false
+                data[key]()
+            }
+        }
+    }
+}