@@ -10,6 +10,7 @@ import {
10
10
import screenCapture from '@/core/screen-capture' ;
11
11
import localConfig from '@/main/common/initLocalConfig' ;
12
12
import winPosition from './getWinPosition' ;
13
+ import { uIOhook , UiohookKey } from 'uiohook-napi' ;
13
14
14
15
const registerHotKey = ( mainWindow : BrowserWindow ) : void => {
15
16
// 设置开机启动
@@ -57,27 +58,43 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
57
58
}
58
59
} ;
59
60
61
+ // 显示主窗口
62
+ function mainWindowPopUp ( ) {
63
+ const currentShow = mainWindow . isVisible ( ) && mainWindow . isFocused ( ) ;
64
+ if ( currentShow ) return mainWindow . hide ( ) ;
65
+ const { x : wx , y : wy } = winPosition . getPosition ( ) ;
66
+ mainWindow . setAlwaysOnTop ( false ) ;
67
+ mainWindow . setVisibleOnAllWorkspaces ( true , { visibleOnFullScreen : true } ) ;
68
+ mainWindow . focus ( ) ;
69
+ mainWindow . setVisibleOnAllWorkspaces ( false , {
70
+ visibleOnFullScreen : true ,
71
+ } ) ;
72
+ mainWindow . setPosition ( wx , wy ) ;
73
+ mainWindow . show ( ) ;
74
+ }
75
+
60
76
const init = async ( ) => {
61
77
await setAutoLogin ( ) ;
62
78
await setDarkMode ( ) ;
63
79
await setTheme ( ) ;
64
80
const config = await localConfig . getConfig ( ) ;
65
81
globalShortcut . unregisterAll ( ) ;
82
+
66
83
// 注册偏好快捷键
67
- globalShortcut . register ( config . perf . shortCut . showAndHidden , ( ) => {
68
- const currentShow = mainWindow . isVisible ( ) && mainWindow . isFocused ( ) ;
69
- if ( currentShow ) return mainWindow . hide ( ) ;
70
- const { x : wx , y : wy } = winPosition . getPosition ( ) ;
71
- mainWindow . setAlwaysOnTop ( false ) ;
72
- mainWindow . setVisibleOnAllWorkspaces ( true , { visibleOnFullScreen : true } ) ;
73
- mainWindow . focus ( ) ;
74
- mainWindow . setVisibleOnAllWorkspaces ( false , {
75
- visibleOnFullScreen : true ,
84
+ // 处理显示/隐藏快捷键的注册
85
+ const doublePressShortcuts = [ 'Ctrl+Ctrl' , 'Option+Option' , 'Shift+Shift' , 'Command+Command' ] ;
86
+ const isDoublePressShortcut = doublePressShortcuts . includes ( config . perf . shortCut . showAndHidden ) ;
87
+
88
+ if ( isDoublePressShortcut ) {
89
+ // 双击快捷键(如 Ctrl+Ctrl)详见 uIOhookRegister 函数实现
90
+ } else {
91
+ // 注册普通快捷键(如 Ctrl+Space、F8 等)
92
+ globalShortcut . register ( config . perf . shortCut . showAndHidden , ( ) => {
93
+ mainWindowPopUp ( ) ;
76
94
} ) ;
77
- mainWindow . setPosition ( wx , wy ) ;
78
- mainWindow . show ( ) ;
79
- } ) ;
95
+ }
80
96
97
+ // 截图快捷键
81
98
globalShortcut . register ( config . perf . shortCut . capture , ( ) => {
82
99
screenCapture ( mainWindow , ( data ) => {
83
100
data &&
@@ -107,9 +124,48 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
107
124
} ) ;
108
125
} ) ;
109
126
} ;
127
+
128
+ uIOhookRegister ( mainWindowPopUp ) ;
110
129
init ( ) ;
111
130
ipcMain . on ( 're-register' , ( ) => {
112
131
init ( ) ;
113
132
} ) ;
114
133
} ;
115
134
export default registerHotKey ;
135
+
136
+ function uIOhookRegister ( callback : ( ) => void ) {
137
+ let lastModifierPress = Date . now ( ) ;
138
+ uIOhook . on ( 'keydown' , async ( uio_event ) => {
139
+ const config = await localConfig . getConfig ( ) ; // 此处还有优化空间
140
+
141
+ if (
142
+ ! [
143
+ 'Ctrl+Ctrl' ,
144
+ 'Option+Option' ,
145
+ 'Shift+Shift' ,
146
+ 'Command+Command' ,
147
+ ] . includes ( config . perf . shortCut . showAndHidden )
148
+ ) {
149
+ return ;
150
+ }
151
+
152
+ // 双击快捷键,如 Ctrl+Ctrl
153
+ const modifers = config . perf . shortCut . showAndHidden . split ( '+' ) ;
154
+ const showAndHiddenKeyStr = modifers . pop ( ) ; // Ctrl
155
+ const keyStr2uioKeyCode = {
156
+ Ctrl : UiohookKey . Ctrl ,
157
+ Shift : UiohookKey . Shift ,
158
+ Option : UiohookKey . Alt ,
159
+ Command : UiohookKey . Comma ,
160
+ } ;
161
+
162
+ if ( uio_event . keycode === keyStr2uioKeyCode [ showAndHiddenKeyStr ] ) {
163
+ const currentTime = Date . now ( ) ;
164
+ if ( currentTime - lastModifierPress < 300 ) {
165
+ callback ( ) ; // 调用 mainWindowPopUp
166
+ }
167
+ lastModifierPress = currentTime ;
168
+ }
169
+ } ) ;
170
+ uIOhook . start ( ) ;
171
+ }
0 commit comments