Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux hotkey can not be canceled correctly #15

Open
data-niklas opened this issue Apr 30, 2022 · 2 comments
Open

Linux hotkey can not be canceled correctly #15

data-niklas opened this issue Apr 30, 2022 · 2 comments

Comments

@data-niklas
Copy link
Contributor

data-niklas commented Apr 30, 2022

Description

The Linux C code event loop only returns after the hotkey was pressed. The hotkey can not be canceled while waitHotkey waits for a new hotkey. hk.Unregister() can be successfully called, but the hotkey is only unregistered after it was triggered.

Steps to reproduce

  1. Register a simple hotkey on Linux
  2. Unregister the hotkey

Notes

hk := h.New([]h.Modifier{h.ModCtrl}, h.KeyM)
hk.Register()
go func() {
  time.Sleep(time.Second * 1)
  fmt.Println("Hotkey will be unregistered")
  hk.Unregister()
  fmt.Println("Hotkey unregistered")
  hk.Register()
  fmt.Println("Registered again")
}()
<-hk.Keydown()

This code blocks. I would expect the chan to be closed and the program to exit.

hotkey/hotkey_linux.c

Lines 64 to 77 in a5dde31

while(1) {
XNextEvent(d, &ev);
switch(ev.type) {
case KeyPress:
hotkeyDown(hkhandle);
continue;
case KeyRelease:
hotkeyUp(hkhandle);
XUngrabKey(d, keycode, mod, DefaultRootWindow(d));
XCloseDisplay(d);
return 0;
}
}
}

I would expect that the above loop checks, if the hotkey was unregistered. XNextEvent also blocks until the next event, but that should be fine as long as a lot of events are triggered. Else something like XPending could be used to check, if new events can be processed.

@changkun
Copy link
Member

This is a bit tricky. Semantically, the hotkey is unregistered, not triggered. That's why the Keydown is not triggered and blocks forever.

@data-niklas
Copy link
Contributor Author

data-niklas commented Apr 30, 2022

Updated the example code. I tested a bit. The Unregister itself also blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants