• Stars
    star
    202
  • Rank 193,691 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 3 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⌨️ cross-platform hotkey package

hotkey PkgGoDev hotkey

cross platform hotkey package in Go

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.

package main

import (
	"log"

	"golang.design/x/hotkey"
	"golang.design/x/hotkey/mainthread"
)

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 {
		log.Fatalf("hotkey: failed to register hotkey: %v", err)
		return
	}

	log.Printf("hotkey: %v is registered\n", hk)
	<-hk.Keydown()
	log.Printf("hotkey: %v is down\n", hk)
	<-hk.Keyup()
	log.Printf("hotkey: %v is up\n", hk)
	hk.Unregister()
	log.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. 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.

More Repositories

1

go-questions

📖 Go 程序员面试笔试宝典 | 从问题切入,串连 Go 语言相关的所有知识,融会贯通。 https://golang.design/go-questions
Go
5,813
star
2

under-the-hood

📚 Go: Under The Hood | Go 语言原本 | https://golang.design/under-the-hood
Go
4,301
star
3

history

📝 Go: A Documentary | https://golang.design/history
Go
1,059
star
4

clipboard

📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)
Go
574
star
5

go2generics

🧪 A chunk of experiments and demos about Go 2 generics design (type parameter & type set)
Go
150
star
6

lockfree

⚡️ lock-free utilities in Go
Go
144
star
7

tgstore

An encrypted object storage system with unlimited space backed by Telegram.
Go
96
star
8

code2img

🖨️ A carbon-now API wrapper and iOS Shortcut
Go
79
star
9

ssaplayground

🃏 The Go SSA Playground: Exploring SSA IR | https://golang.design/gossa
Go
76
star
10

bench

⏱️ Reliable performance measurement for Go programs. All in one design.
Go
38
star
11

chann

a unified channel package for buffered, unbuffered, and unbounded channels
Go
37
star
12

mainthread

🔀 schedule functions on the main thread
Go
33
star
13

redir

🧭 a request redirector
Go
28
star
14

mkill

☠️ mkill limits the number of threads in a Go program, without crashing it.
Go
23
star
15

thread

🧵 thead facilities in Go
Go
15
star
16

research

🗞️ (Possibly) Naïve thoughts regarding Go.
Go
12
star
17

reflect

Package reflect implements the proposal https://go.dev/issue/51520.
Go
5
star
18

verbose

🤪 verbose repository for testing purpose | go get golang.design/x/verbose
Go
4
star
19

.github

Community health files for the @golang-design organization
3
star
20

iter

Iterator utils.
Go
2
star
21

website

🌐 website | https://golang.design
HTML
1
star
22

io

a replacement to the standard io package.
Go
1
star