forked from AllenDang/w32
-
Notifications
You must be signed in to change notification settings - Fork 1
/
registerhotkeyunregisterhotkey.go
66 lines (62 loc) · 1.58 KB
/
registerhotkeyunregisterhotkey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"github.com/xackery/w32"
)
//Wait for a hotkey event to occur.
func waitForInputLoop() (err error) {
var msg w32.MSG
quit := false
for !quit {
msg, err = w32.GetMessage(0, 0, 0)
if err != nil {
fmt.Println("Error with GetMessage:", err.Error())
return
}
switch msg.Message {
case w32.WM_HOTKEY:
if msg.WParam == 1 {
fmt.Println("1 pressed")
} else if msg.WParam == 2 {
fmt.Println("2 pressed")
} else if msg.WParam == 3 {
fmt.Println("Q pressed")
quit = true
}
}
}
return
}
//This shows how to register and unregister a hotkey
func main() {
var err error
err = w32.RegisterHotKey(0, 1, w32.MOD_NOREPEAT, 0x31) //Bind the 1 key to the global hotkey 1
if err != nil {
fmt.Println("Error registering 1:", err.Error())
return
}
err = w32.RegisterHotKey(0, 2, w32.MOD_NOREPEAT, 0x32) //Bind the 2 key to the global hotkey 2
if err != nil {
fmt.Println("Error registering 2:", err.Error())
return
}
err = w32.RegisterHotKey(0, 3, w32.MOD_NOREPEAT, 0x51) //Bind the Q key Globally to the global hotkey 3
if err != nil {
fmt.Println("Error registering Q:", err.Error())
return
}
fmt.Println("Waiting for Hotkey of 1, 2 or Q (quit)...")
err = waitForInputLoop()
if err != nil {
fmt.Println("Error registering Q:", err.Error())
return
}
w32.UnregisterHotKey(0, 2) //remove the global hotkey 2, removing 2 key
fmt.Println("Unregistered 2 key, 1 and Q (quit) should still work...")
err = waitForInputLoop()
if err != nil {
fmt.Println("Error registering Q:", err.Error())
return
}
fmt.Println("Exited!")
}