golang-design / hotkey

⌨️ cross-platform hotkey package
https://golang.design/x/hotkey
MIT License
215 stars 10 forks source link

doc: document the correct usage of X11 hotkey registration #17

Closed wsy998 closed 1 year ago

wsy998 commented 2 years ago

I can’t use it,is it my problem? (system:ubuntu 22.04) image

changkun commented 2 years ago

I guess the support for Linux OS was not well enough. If the registered hotkey conflicts with an existing hotkey, it won't have any effect.

Generally you can run this example and see if it works on your system:

https://github.com/golang-design/hotkey/blob/a5dde31e1341de42c328de537041eddd79995ff5/examples/fyne/main.go#L15-L34

wsy998 commented 2 years ago

I checked that my hotkeys (including SYSTEM, INPUT METHOD, IDE) are not conflicting. The sample also failed to run successfully.

changkun commented 2 years ago

It might be useful if you could find a second device to test the environment.

changkun commented 2 years ago

@wsy998 OK. Now I recall the issue on Linux systems.

In general, Ubuntu maps multiple Mod keys to represent a single mod key. To be able to correctly register the key combination, you need figure out what is the correct underlying keycode combination for the platform. For example, a regular Ctrl+Alt+S should be registered as: Ctrl+Mod2+Mod4+S.

This example works on my Ubuntu 20.04 (Ctrl+Alt+S):

package main

import (
    "fmt"

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

func main() { mainthread.Init(fn) }
func fn() {
    hk := hotkey.New([]hotkey.Modifier{hotkey.ModCtrl, hotkey.Mod2, hotkey.Mod4}, 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)
}
wsy998 commented 2 years ago

OK,Let me try.

mwalkerr commented 1 year ago

How did you find the other mod keys that are mapped?