Skip to content

Commit 9afab3a

Browse files
committed
all: revise documentations
1 parent 4bbd887 commit 9afab3a

File tree

19 files changed

+384
-214
lines changed

19 files changed

+384
-214
lines changed

README.md

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,16 @@ import "golang.design/x/hotkey"
1313

1414
## API Usage
1515

16-
Package `hotkey` provides the basic facility to register a system-level
17-
hotkey so that the application can be notified if a user triggers the
18-
desired hotkey. By definition, a hotkey is a combination of modifiers
19-
and a single key, and thus register a hotkey that contains multiple
20-
keys is not supported at the moment. Furthermore, because of OS
21-
restriction, hotkey events must be handled on the main thread.
16+
Package hotkey provides the primary facility to register a system-level global hotkey shortcut to notify an application if a user triggers the desired hotkey. A hotkey must be a combination of modifiers and a single key.
2217

23-
Therefore, in order to use this package properly, here is a complete
24-
example that corporates the mainthread:
25-
package:
26-
27-
```go
28-
package main
29-
30-
import (
31-
"context"
32-
33-
"golang.design/x/hotkey"
34-
"golang.design/x/hotkey/mainthread"
35-
)
36-
37-
// initialize mainthread facility so that hotkey can be
38-
// properly registered to the system and handled by the
39-
// application.
40-
func main() { mainthread.Run(fn) }
41-
func fn() { // Use fn as the actual main function.
42-
var (
43-
mods = []hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}
44-
k = hotkey.KeyS
45-
)
46-
47-
// Register a desired hotkey.
48-
hk := hotkey.New(mods, k)
49-
if err := hk.Register() err != nil {
50-
panic("hotkey registration failed")
51-
}
52-
53-
// Start listen hotkey event whenever you feel it is ready.
54-
for range hk.Listen() {
55-
println("hotkey ctrl+shift+s is triggered")
56-
}
57-
}
58-
```
18+
Note a platform-specific detail on `macOS` due to the OS restriction (other platforms do not have this restriction): hotkey events must be handled on the "main thread". Therefore, to use this package properly, one must call the `(*Hotkey).Register` method on the main thread, and an OS app main event loop must be established. One can use the provided `golang.design/x/hotkey/mainthread` for self-contained applications. For applications based on other GUI frameworks, one has to use their provided ability to run the `(*Hotkey).Register` on the main thread. See the [examples](./examples) folder for more examples.
5919

6020
## Who is using this package?
6121

6222
The main purpose of building this package is to support the
6323
[midgard](https://changkun.de/s/midgard) project.
6424

65-
To know more projects, check our [wiki](https://github.com/golang-design/clipboard/wiki) page.
25+
To know more projects, check our [wiki](https://github.com/golang-design/hotkey/wiki) page.
6626

6727
## License
6828

example/main.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module golang.design/x/hotkey
22

33
go 1.17
4+
5+
require golang.design/x/mainthread v0.3.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
golang.design/x/mainthread v0.3.0 h1:UwFus0lcPodNpMOGoQMe87jSFwbSsEY//CA7yVmu4j8=
2+
golang.design/x/mainthread v0.3.0/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvHeu259L0=
3+
golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd h1:WgqgiQvkiZWz7XLhphjt2GI2GcGCTIZs9jqXMWmH+oc=
4+
golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

hotkey.go

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,19 @@
55
// Written by Changkun Ou <changkun.de>
66

77
// Package hotkey provides the basic facility to register a system-level
8-
// hotkey so that the application can be notified if a user triggers the
9-
// desired hotkey. By definition, a hotkey is a combination of modifiers
10-
// and a single key, and thus register a hotkey that contains multiple
11-
// keys is not supported at the moment. Furthermore, because of OS
12-
// restriction, hotkey events must be handled on the main thread.
8+
// global hotkey shortcut so that an application can be notified if a user
9+
// triggers the desired hotkey. A hotkey must be a combination of modifiers
10+
// and a single key.
1311
//
14-
// Therefore, in order to use this package properly, here is a complete
15-
// example that corporates the golang.design/x/hotkey/mainthread package:
16-
//
17-
// package main
18-
//
19-
// import (
20-
// "context"
21-
//
22-
// "golang.design/x/hotkey"
23-
// "golang.design/x/hotkey/mainthread"
24-
// )
25-
//
26-
// // initialize mainthread facility so that hotkey can be
27-
// // properly registered to the system and handled by the
28-
// // application.
29-
// func main() { mainthread.Run(fn) }
30-
// func fn() { // Use fn as the actual main function.
31-
// var (
32-
// mods = []hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}
33-
// k = hotkey.KeyS
34-
// )
35-
//
36-
// // Register a desired hotkey.
37-
// hk := hotkey.New(mods, k)
38-
// if err := hk.Register(); err != nil {
39-
// panic("hotkey registration failed")
40-
// }
41-
//
42-
// // Start listen hotkey event whenever you feel it is ready.
43-
// for range hk.Listen() {
44-
// println("hotkey ctrl+shift+s is triggered")
45-
// }
46-
// }
12+
// Note a platform specific detail on "macOS" due to the OS restriction (other
13+
// platforms does not have this restriction), hotkey events must be handled
14+
// on the "main thread". Therefore, in order to use this package properly,
15+
// one must call the "(*Hotkey).Register" method on the main thread, and an
16+
// OS app main event loop must be established. For self-contained applications,
17+
// collaborating with the provided golang.design/x/hotkey/mainthread is possible.
18+
// For applications based on other GUI frameworks, one has to use their provided
19+
// ability to run the "(*Hotkey).Register" on the main thread. See the "./examples"
20+
// folder for more examples.
4721
package hotkey
4822

4923
// Event represents a hotkey event
@@ -68,13 +42,19 @@ func New(mods []Modifier, key Key) *Hotkey {
6842
// Register registers a combination of hotkeys. If the hotkey has
6943
// registered. This function will invalidates the old registration
7044
// and overwrites its callback.
71-
func (hk *Hotkey) Register() error {
72-
return hk.register()
73-
}
45+
//
46+
// For macOS, this method must be called on the main thread, and
47+
// an OS main event loop also must be running. This can be done when
48+
// collaborating with the golang.design/x/hotkey/mainthread package.
49+
func (hk *Hotkey) Register() error { return hk.register() }
7450

75-
// Listen handles a hotkey event and triggers a call to fn.
76-
// The hotkey listen hook terminates when the context is canceled.
77-
func (hk *Hotkey) Listen() <-chan Event { return hk.out }
51+
// Keydown returns a channel that receives a signal when hotkey is triggered.
52+
func (hk *Hotkey) Keydown() <-chan Event { return hk.out }
53+
54+
// Keyup returns a channel that receives a signal when the hotkey is released.
55+
func (hk *Hotkey) Keyup() <-chan Event {
56+
panic("hotkey: unimplemented")
57+
}
7858

7959
// Unregister unregisters the hotkey.
8060
func (hk *Hotkey) Unregister() error {

hotkey_darwin.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"errors"
2525
"runtime/cgo"
2626
"sync"
27-
28-
"golang.design/x/hotkey/mainthread"
2927
)
3028

3129
// Hotkey is a combination of modifiers and key to trigger an event
@@ -53,9 +51,7 @@ func (hk *Hotkey) register() error {
5351
}
5452

5553
var ret C.int
56-
mainthread.Call(func() {
57-
ret = C.registerHotKey(C.int(mod), C.int(hk.key), C.uintptr_t(h), &hk.hkref)
58-
})
54+
ret = C.registerHotKey(C.int(mod), C.int(hk.key), C.uintptr_t(h), &hk.hkref)
5955
if ret == C.int(-1) {
6056
return errors.New("failed to register the hotkey")
6157
}

hotkey_darwin_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"golang.design/x/hotkey"
18+
"golang.design/x/hotkey/mainthread"
1819
)
1920

2021
// TestHotkey should always run success.
@@ -28,7 +29,9 @@ func TestHotkey(t *testing.T) {
2829
ctx, cancel := context.WithTimeout(context.Background(), tt)
2930
go func() {
3031
hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyS)
31-
if err := hk.Register(); err != nil {
32+
33+
var err error
34+
if mainthread.Call(func() { err = hk.Register() }); err != nil {
3235
t.Errorf("failed to register hotkey: %v", err)
3336
return
3437
}
@@ -38,15 +41,17 @@ func TestHotkey(t *testing.T) {
3841
cancel()
3942
done <- struct{}{}
4043
return
41-
case <-hk.Listen():
44+
case <-hk.Keydown():
4245
fmt.Println("triggered ctrl+shift+s")
4346
}
4447
}
4548
}()
4649

4750
go func() {
4851
hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModOption}, hotkey.KeyS)
49-
if err := hk.Register(); err != nil {
52+
53+
var err error
54+
if mainthread.Call(func() { err = hk.Register() }); err != nil {
5055
t.Errorf("failed to register hotkey: %v", err)
5156
return
5257
}
@@ -57,7 +62,7 @@ func TestHotkey(t *testing.T) {
5762
cancel()
5863
done <- struct{}{}
5964
return
60-
case <-hk.Listen():
65+
case <-hk.Keydown():
6166
fmt.Println("triggered ctrl+option+s")
6267
}
6368
}

hotkey_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestHotkey(t *testing.T) {
3434
select {
3535
case <-ctx.Done():
3636
return
37-
case <-hk.Listen():
37+
case <-hk.Keydown():
3838
fmt.Println("triggered")
3939
}
4040
}

hotkey_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ import (
1616
// The test cannot be run twice since the mainthread loop may not be terminated:
1717
// go test -v -count=1
1818
func TestMain(m *testing.M) {
19-
mainthread.Run(func() { os.Exit(m.Run()) })
19+
mainthread.Init(func() { os.Exit(m.Run()) })
2020
}

0 commit comments

Comments
 (0)