@@ -19,41 +19,53 @@ triggers the desired hotkey. A hotkey must be a combination of modifiers
19
19
and a single key.
20
20
21
21
``` go
22
+ package main
23
+
22
24
import (
23
- " golang.design/x/hotkey"
24
- " golang.design/x/hotkey/mainthread"
25
+ " log"
26
+
27
+ " golang.design/x/hotkey"
28
+ " golang.design/x/hotkey/mainthread"
25
29
)
26
30
27
31
func main () { mainthread.Init (fn) } // Not necessary when use in Fyne, Ebiten or Gio.
28
32
func fn () {
29
- hk := hotkey.New ([]hotkey.Modifier {hotkey.ModCtrl , hotkey.ModShift }, hotkey.KeyS )
30
- err := hk.Register ()
31
- if err != nil {
32
- return
33
- }
34
- fmt.Printf (" hotkey: %v is registered\n " , hk)
35
- <- hk.Keydown ()
36
- fmt.Printf (" hotkey: %v is down\n " , hk)
37
- <- hk.Keyup ()
38
- fmt.Printf (" hotkey: %v is up\n " , hk)
39
- hk.Unregister ()
40
- fmt.Printf (" hotkey: %v is unregistered\n " , hk)
33
+ hk := hotkey.New ([]hotkey.Modifier {hotkey.ModCtrl , hotkey.ModShift }, hotkey.KeyS )
34
+ err := hk.Register ()
35
+ if err != nil {
36
+ log.Fatalf (" hotkey: failed to register hotkey: %v " , err)
37
+ return
38
+ }
39
+
40
+ log.Printf (" hotkey: %v is registered\n " , hk)
41
+ <- hk.Keydown ()
42
+ log.Printf (" hotkey: %v is down\n " , hk)
43
+ <- hk.Keyup ()
44
+ log.Printf (" hotkey: %v is up\n " , hk)
45
+ hk.Unregister ()
46
+ log.Printf (" hotkey: %v is unregistered\n " , hk)
41
47
}
42
48
```
43
49
44
50
Note platform specific details:
45
51
46
- - On macOS, due to the OS restriction (other
47
- platforms does not have this restriction), hotkey events must be handled
48
- on the "main thread". Therefore, in order to use this package properly,
49
- one must start an OS main event loop on the main thread, For self-contained
50
- applications, using [ golang.design/x/hotkey/mainthread] ( https://pkg.go.dev/golang.design/x/hotkey/mainthread ) is possible.
51
- For applications based on other GUI frameworks, such as fyne, ebiten, or Gio.
52
- This is not necessary. See the "[ ./examples] ( ./examples ) " folder for more examples.
52
+ - On macOS, due to the OS restriction (other platforms does not have this
53
+ restriction), hotkey events must be handled on the "main thread".
54
+ Therefore, in order to use this package properly, one must start an OS
55
+ main event loop on the main thread, For self-contained applications,
56
+ using [ golang.design/x/hotkey/mainthread] ( https://pkg.go.dev/golang.design/x/hotkey/mainthread )
57
+ is possible. It is uncessary or applications based on other GUI frameworks,
58
+ such as fyne, ebiten, or Gio. See the "[ ./examples] ( ./examples ) " folder
59
+ for more examples.
53
60
- On Linux (X11), when AutoRepeat is enabled in the X server, the Keyup
54
61
is triggered automatically and continuously as Keydown continues.
55
- - If this package did not include a desired key, one can always provide the keycode to the API.
56
- For example, if a key code is 0x15, then the corresponding key is ` hotkey.Key(0x15) ` .
62
+ - On Linux (X11), some keys may be mapped to multiple Mod keys. To
63
+ correctly register the key combination, one must use the correct
64
+ underlying keycode combination. For example, a regular Ctrl+Alt+S
65
+ might be registered as: Ctrl+Mod2+Mod4+S.
66
+ - If this package did not include a desired key, one can always provide
67
+ the keycode to the API. For example, if a key code is 0x15, then the
68
+ corresponding key is ` hotkey.Key(0x15) ` .
57
69
58
70
## Examples
59
71
@@ -65,6 +77,7 @@ Note platform specific details:
65
77
| A example to use in Fyne | [ fyne] ( ./examples/fyne/main.go ) |
66
78
| A example to use in Ebiten | [ ebiten] ( ./examples/ebiten/main.go ) |
67
79
| A example to use in Gio | [ gio] ( ./examples/gio/main.go ) |
80
+
68
81
## Who is using this package?
69
82
70
83
The main purpose of building this package is to support the
0 commit comments