hotkey

package module
v0.0.0-...-fab615c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 3 Imported by: 0

README

go-hotkey

golang 全局热键

修改自 https://github.com/golang-design/hotkey

只适用于windows

参数 说明
w win
c ctrl
s shift
a alt

使用方法:

  • 不接受大写

  • 第一个参数为热键的组合,winctrlshiftalt 无顺序要求

    csa_zctrl_shift_alt_z, 如有必要, 可修改分隔符: hotkey.SetSplitStr("+")

  • 第二个参数执行时机

    可选值: downuppress, 默认为 down

    • down 立刻执行
    • up 按键松开时执行
    • press 可绑定 按下松开 时的方法
// 注册热键
hotkey.Register("cs_z", "up", func() {
	fmt.Println(time.Now(), "cs_z up")
})

// 解除热键
hotkey.Unregister("cs_z", "up")

示例:

package main

import (
	"fmt"
	"time"

	"github.com/ZeronoFreya/go-hotkey"
)

func main() {

	done := make(chan bool)
    // 默认打印热键: ctrl shift z down
	hotkey.Register("cs_z", "")
    // 或
	hotkey.Register("cs_s", "up", func() {
		fmt.Println(time.Now(), "cs_s up")
	})
    // esc 注销热键绑定
	hotkey.Register("esc", "", func() {
		hotkey.Unregister("cs_z", "")
		done <- false
	})
    // 关于 press
    // 可传递第二个方法来接收 松开 事件
    // down 与 up 不会响应第二个方法
    hotkey.Register("cs_d", "press", func() {
		fmt.Println("cs_d 按下")
	}, func() {
		fmt.Println("cs_d 松开")
	})
	fmt.Println("Start!")
	<-done
}

Documentation

Overview

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.

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 mainthread package. is possible. It is uncessary or applications based on other GUI frameworks, such as fyne, ebiten, or Gio. See the "examples" 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)`.

THe following is a minimum example:

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)
	}

	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)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(modifier, key string, callbacks ...func()) error

func SetSplitStr

func SetSplitStr(str string)

func Unregister

func Unregister(modifier, key string) error

Types

type Event

type Event struct{}

Event represents a hotkey event

type Hotkey

type Hotkey struct {
	Signal    string
	Callbacks []func()
	// contains filtered or unexported fields
}

Hotkey is a combination of modifiers and key to trigger an event

func New

func New(mods []Modifier, key Key) *Hotkey

New creates a new hotkey for the given modifiers and keycode.

func (*Hotkey) String

func (hk *Hotkey) String() string

String returns a string representation of the hotkey.

type Modifier

type Modifier uint8

Modifier represents a modifier. https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey

const (
	ModAlt   Modifier = 0x1
	ModCtrl  Modifier = 0x2
	ModShift Modifier = 0x4
	ModWin   Modifier = 0x8
)

All kinds of Modifiers

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL