Skip to content

Commit 032d006

Browse files
committed
all: improve documentation
Fixes #17
1 parent 827d2c9 commit 032d006

File tree

3 files changed

+90
-54
lines changed

3 files changed

+90
-54
lines changed

README.md

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,53 @@ triggers the desired hotkey. A hotkey must be a combination of modifiers
1919
and a single key.
2020

2121
```go
22+
package main
23+
2224
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"
2529
)
2630

2731
func main() { mainthread.Init(fn) } // Not necessary when use in Fyne, Ebiten or Gio.
2832
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)
4147
}
4248
```
4349

4450
Note platform specific details:
4551

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.
5360
- On Linux (X11), when AutoRepeat is enabled in the X server, the Keyup
5461
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)`.
5769

5870
## Examples
5971

@@ -65,6 +77,7 @@ Note platform specific details:
6577
| A example to use in Fyne | [fyne](./examples/fyne/main.go) |
6678
| A example to use in Ebiten | [ebiten](./examples/ebiten/main.go) |
6779
| A example to use in Gio | [gio](./examples/gio/main.go) |
80+
6881
## Who is using this package?
6982

7083
The main purpose of building this package is to support the

examples/minimum/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
// Copyright 2022 The golang.design Initiative Authors.
22
// All rights reserved. Use of this source code is governed
33
// by a MIT license that can be found in the LICENSE file.
4-
54
package main
65

76
import (
8-
"fmt"
7+
"log"
98

109
"golang.design/x/hotkey"
1110
"golang.design/x/hotkey/mainthread"
1211
)
1312

14-
func main() { mainthread.Init(fn) }
13+
func main() { mainthread.Init(fn) } // Not necessary when use in Fyne, Ebiten or Gio.
1514
func fn() {
1615
hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyS)
1716
err := hk.Register()
1817
if err != nil {
18+
log.Fatalf("hotkey: failed to register hotkey: %v", err)
1919
return
2020
}
21-
fmt.Printf("hotkey: %v is registered\n", hk)
21+
22+
log.Printf("hotkey: %v is registered\n", hk)
2223
<-hk.Keydown()
23-
fmt.Printf("hotkey: %v is down\n", hk)
24+
log.Printf("hotkey: %v is down\n", hk)
2425
<-hk.Keyup()
25-
fmt.Printf("hotkey: %v is up\n", hk)
26+
log.Printf("hotkey: %v is up\n", hk)
2627
hk.Unregister()
27-
fmt.Printf("hotkey: %v is unregistered\n", hk)
28+
log.Printf("hotkey: %v is unregistered\n", hk)
2829
}

hotkey.go

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,54 @@
1111
//
1212
// Note platform specific details:
1313
//
14-
// - On "macOS" due to the OS restriction (other
15-
// platforms does not have this restriction), hotkey events must be handled
16-
// on the "main thread". Therefore, in order to use this package properly,
17-
// one must start an OS main event loop on the main thread, For self-contained
18-
// applications, using golang.design/x/hotkey/mainthread is possible.
19-
// For applications based on other GUI frameworks, such as fyne, ebiten, or Gio.
20-
// This is not necessary. See the "./examples" folder for more examples.
14+
// - On macOS, due to the OS restriction (other platforms does not have
15+
// this restriction), hotkey events must be handled on the "main thread".
16+
// Therefore, in order to use this package properly, one must start an
17+
// OS main event loop on the main thread, For self-contained applications,
18+
// using [golang.design/x/hotkey/mainthread](https://pkg.go.dev/golang.design/x/hotkey/mainthread)
19+
// is possible. It is uncessary or applications based on other GUI frameworks,
20+
// such as fyne, ebiten, or Gio. See the "[./examples](./examples)" folder
21+
// for more examples.
2122
//
22-
// - On Linux (X11), When AutoRepeat is enabled in the X server, the Keyup
23-
// is triggered automatically and continuously as Keydown continues.
23+
// - On Linux (X11), when AutoRepeat is enabled in the X server, the
24+
// Keyup is triggered automatically and continuously as Keydown continues.
2425
//
25-
// func main() { mainthread.Init(fn) } // not necessary when use in Fyne, Ebiten or Gio.
26-
// func fn() {
27-
// hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyS)
28-
// err := hk.Register()
29-
// if err != nil {
30-
// return
31-
// }
32-
// fmt.Printf("hotkey: %v is registered\n", hk)
33-
// <-hk.Keydown()
34-
// fmt.Printf("hotkey: %v is down\n", hk)
35-
// <-hk.Keyup()
36-
// fmt.Printf("hotkey: %v is up\n", hk)
37-
// hk.Unregister()
38-
// fmt.Printf("hotkey: %v is unregistered\n", hk)
39-
// }
26+
// - On Linux (X11), some keys may be mapped to multiple Mod keys. To
27+
// correctly register the key combination, one must use the correct
28+
// underlying keycode combination. For example, a regular Ctrl+Alt+S
29+
// might be registered as: Ctrl+Mod2+Mod4+S.
30+
//
31+
// - If this package did not include a desired key, one can always provide
32+
// the keycode to the API. For example, if a key code is 0x15, then the
33+
// corresponding key is `hotkey.Key(0x15)`.
34+
//
35+
// THe following is a minimum example:
36+
//
37+
// package main
38+
//
39+
// import (
40+
// "log"
41+
//
42+
// "golang.design/x/hotkey"
43+
// "golang.design/x/hotkey/mainthread"
44+
// )
45+
//
46+
// func main() { mainthread.Init(fn) } // Not necessary when use in Fyne, Ebiten or Gio.
47+
// func fn() {
48+
// hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyS)
49+
// err := hk.Register()
50+
// if err != nil {
51+
// log.Fatalf("hotkey: failed to register hotkey: %v", err)
52+
// }
53+
//
54+
// log.Printf("hotkey: %v is registered\n", hk)
55+
// <-hk.Keydown()
56+
// log.Printf("hotkey: %v is down\n", hk)
57+
// <-hk.Keyup()
58+
// log.Printf("hotkey: %v is up\n", hk)
59+
// hk.Unregister()
60+
// log.Printf("hotkey: %v is unregistered\n", hk)
61+
// }
4062
package hotkey
4163

4264
import (

0 commit comments

Comments
 (0)