项目作者: golang-design

项目描述 :
⌨️ cross-platform hotkey package
高级语言: Go
项目地址: git://github.com/golang-design/hotkey.git
创建时间: 2021-02-24T21:44:17Z
项目社区:https://github.com/golang-design/hotkey

开源协议:MIT License

下载


hotkey PkgGoDev hotkey

cross platform hotkey package in Go

  1. import "golang.design/x/hotkey"

Features

  • Cross platform supports: macOS, Linux (X11), and Windows
  • Global hotkey registration without focus on a window

API Usage

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.

  1. package main
  2. import (
  3. "log"
  4. "golang.design/x/hotkey"
  5. "golang.design/x/hotkey/mainthread"
  6. )
  7. func main() { mainthread.Init(fn) } // Not necessary when use in Fyne, Ebiten or Gio.
  8. func fn() {
  9. hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.ModShift}, hotkey.KeyS)
  10. err := hk.Register()
  11. if err != nil {
  12. log.Fatalf("hotkey: failed to register hotkey: %v", err)
  13. return
  14. }
  15. log.Printf("hotkey: %v is registered\n", hk)
  16. <-hk.Keydown()
  17. log.Printf("hotkey: %v is down\n", hk)
  18. <-hk.Keyup()
  19. log.Printf("hotkey: %v is up\n", hk)
  20. hk.Unregister()
  21. log.Printf("hotkey: %v is unregistered\n", hk)
  22. }

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. It is uncessary or applications based on other GUI frameworks,
    such as fyne, ebiten, or Gio. 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.
  • On Linux (X11), some keys may be mapped to multiple Mod keys. To
    correctly register the key combination, one must use the correct
    underlying keycode combination. For example, a regular Ctrl+Alt+S
    might be registered as: Ctrl+Mod2+Mod4+S.
  • 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).

Examples

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

Who is using this package?

The main purpose of building this package is to support the
midgard project.

To know more projects, check our wiki page.

License

MIT | © 2021 The golang.design Initiative Authors, written by Changkun Ou.