Files
kettuRay/hotkey_windows.go
2026-03-31 14:40:03 +03:00

62 lines
1.2 KiB
Go

//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)))
}
}()
}