Private
Public Access
1
0
Files
u-desk/internal/hotkey/hotkey.go

42 lines
1.1 KiB
Go

package hotkey
import (
"fmt"
"syscall"
)
var (
moduser32 = syscall.NewLazyDLL("user32.dll")
procRegisterHotKey = moduser32.NewProc("RegisterHotKey")
procUnregisterHotKey = moduser32.NewProc("UnregisterHotKey")
procPostMessage = moduser32.NewProc("PostMessageW")
)
const (
ModAlt = 0x0001
ModControl = 0x0002
ModShift = 0x0004
ModWin = 0x0008
WM_HOTKEY = 0x0312
WM_APP_HOTKEY = 0x8001 // 自定义消息:在主线程触发热键注册
)
func Register(hwnd uintptr, id int32, modifiers uint32, vk uint32) error {
ret, _, _ := procRegisterHotKey.Call(hwnd, uintptr(id), uintptr(modifiers), uintptr(vk))
if ret == 0 {
return fmt.Errorf("RegisterHotKey failed for id=%d", id)
}
return nil
}
func Unregister(hwnd uintptr, id int32) bool {
ret, _, _ := procUnregisterHotKey.Call(hwnd, uintptr(id))
return ret != 0
}
// PostMessage 向窗口投递异步消息(用于跨线程调度到主线程)
func PostMessage(hwnd uintptr, msg uint32, wParam, lParam uintptr) {
procPostMessage.Call(hwnd, uintptr(msg), wParam, lParam)
}