cross platform hotkey package in Go
import "golang.design/x/hotkey"
- Cross platform supports: macOS, Linux (X11), and Windows
- Global hotkey registration without focus on a window
Package hotkey provides the basic facility to register a system-level global hotkey shortcut so that an application can be notified if a user triggers the desired hotkey. A hotkey must be a combination of modifiers and a single key.
func main() { mainthread.Init(fn) } // not necessary when use in Fyne, Ebiten or Gio.
func fn() {
hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyS)
err := hk.Register()
if err != nil {
return
}
fmt.Printf("hotkey: %v is registered\n", hk)
<-hk.Keydown()
fmt.Printf("hotkey: %v is down\n", hk)
<-hk.Keyup()
fmt.Printf("hotkey: %v is up\n", hk)
hk.Unregister()
fmt.Printf("hotkey: %v is unregistered\n", hk)
}
Note platform specific details:
- On macOS, due to the OS restriction (other platforms does not have this restriction), hotkey events must be handled on the "main thread". Therefore, in order to use this package properly, one must start an OS main event loop on the main thread, For self-contained applications, using golang.design/x/hotkey/mainthread is possible. For applications based on other GUI frameworks, such as fyne, ebiten, or Gio. This is not necessary. See the "./examples" folder for more examples.
- On Linux (X11), when AutoRepeat is enabled in the X server, the Keyup is triggered automatically and continuously as Keydown continues.
- If this package did not include a desired key, one can always provide the keycode to the API.
For example, if a key code is 0x15, then the corresponding key is
hotkey.Key(0x15)
.
Description | Folder |
---|---|
A minimum example | minimum |
Register multiple hotkeys | multiple |
A example to use in GLFW | glfw |
A example to use in Fyne | fyne |
A example to use in Ebiten | ebiten |
A example to use in Gio | gio |
The main purpose of building this package is to support the midgard project.
To know more projects, check our wiki page.
MIT | © 2021 The golang.design Initiative Authors, written by Changkun Ou.