init commit
This commit is contained in:
61
hotkey_windows.go
Normal file
61
hotkey_windows.go
Normal file
@@ -0,0 +1,61 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
goruntime "runtime"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const (
|
||||
wmHotkey = 0x0312
|
||||
modWin = 0x0008
|
||||
modAlt = 0x0001
|
||||
vkV = 0x56
|
||||
hotkeyID = 1
|
||||
)
|
||||
|
||||
var (
|
||||
user32 = windows.NewLazySystemDLL("user32.dll")
|
||||
procRegisterHotKey = user32.NewProc("RegisterHotKey")
|
||||
procGetMessage = user32.NewProc("GetMessageW")
|
||||
procTranslateMsg = user32.NewProc("TranslateMessage")
|
||||
procDispatchMsg = user32.NewProc("DispatchMessageW")
|
||||
)
|
||||
|
||||
type winMSG struct {
|
||||
HWND uintptr
|
||||
Message uint32
|
||||
WParam uintptr
|
||||
LParam uintptr
|
||||
Time uint32
|
||||
PtX int32
|
||||
PtY int32
|
||||
}
|
||||
|
||||
func (a *App) registerHotkey() {
|
||||
go func() {
|
||||
goruntime.LockOSThread()
|
||||
defer goruntime.UnlockOSThread()
|
||||
|
||||
r, _, _ := procRegisterHotKey.Call(0, hotkeyID, modWin|modAlt, vkV)
|
||||
if r == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var msg winMSG
|
||||
for {
|
||||
r, _, _ := procGetMessage.Call(uintptr(unsafe.Pointer(&msg)), 0, 0, 0)
|
||||
if r == 0 || r == ^uintptr(0) {
|
||||
break
|
||||
}
|
||||
if msg.Message == wmHotkey && msg.WParam == hotkeyID {
|
||||
a.toggleVPN()
|
||||
}
|
||||
procTranslateMsg.Call(uintptr(unsafe.Pointer(&msg)))
|
||||
procDispatchMsg.Call(uintptr(unsafe.Pointer(&msg)))
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user