golang-design / hotkey Goto Github PK
View Code? Open in Web Editor NEW⌨️ cross-platform hotkey package
Home Page: https://golang.design/x/hotkey
License: MIT License
⌨️ cross-platform hotkey package
Home Page: https://golang.design/x/hotkey
License: MIT License
I want to make an autoclick in Go. Looking through you readme, i still dont understand how to use it and what everything is for.
Please update your documention for clarity
(also, what does for range triggered
do? why for range
and not if triggered
There seems to be a TODO for Darwin and an UnregisterHotKey already for Windows. Any plans on adding implementation for Darwin and using the one available for Windows?
I have a very simple feature request: implement a map or function to translate string to a hotkey.
This is very helpful when we use a custom hotkey configuration.
I'd like to save the hotkey struct to a datastore on file so the user dosn't have to re enter the desired hotkey.
Seems like I need to store the hotkey via string function and reconstruct it at runtime?
Would be better to have the fields declared public so I can easily export them
I hit this issue and then I saw that it is noted in the code:
Line 82 in 258b6bc
Posting here for visibility.
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.
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.
Lines 64 to 77 in a5dde31
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.Similar to golang-design/clipboard#10 (comment).
Hotkey register causes the app to crash with the following output:
ventsi@localhost:/media/sf_prosper> go run app.go
X Error of failed request: BadAccess (attempt to access private resource denied)
Major opcode of failed request: 33 (X_GrabKey)
Serial number of failed request: 10
Current serial number in output stream: 11
exit status 1
Commenting out the code to register hotkey fixes the issue:
This is openSUSE Leap 15.3 running in VirtualBox.
I am trying to use this library with wails
GUI library and everytime I get the error message that a hotkey is registered
ERROR: hotkey: 109+4+1 is registered
X Error of failed request: BadAccess (attempt to access private resource denied)
Major opcode of failed request: 33 (X_GrabKey)
Serial number of failed request: 10
Current serial number in output stream: 11
exit status 1
no matter which key I am using with any modifier
not even sure where to look for some additional debuggin info, would you have any advice / help?
hotkey package runs an internal application that will take over the mainthread.
This limits the package usage, which cannot collaborate with other GUI packages, such as fyne, gio, ebiten, and etc.
Figure out a way to provide native OS features in an isolated package, such as app
, so that users can choose to use that package or not.
Hotkey shortcut monitoring will hijack the information of the shortcut key. When I run the following code, I copy a piece of text and paste it in the browser's search box. The information is hijacked, and it can only be paused when I end the program.
package main
import (
"golang.design/x/clipboard"
"log"
"sync"
"golang.design/x/hotkey"
"golang.design/x/hotkey/mainthread"
)
func main() {
mainthread.Init(fn)
}
func fn() {
err := clipboard.Init()
if err != nil {
panic(err)
}
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
for {
err := listenHotkey(hotkey.KeyC, hotkey.ModCtrl)
if err != nil {
log.Println(err)
}
}
}()
go func() {
defer wg.Done()
for {
err := listenHotkey(hotkey.KeyV, hotkey.ModCtrl)
if err != nil {
log.Println(err)
}
//imageData := clipboard.Read(clipboard.FmtImage)
//base64String := base64.StdEncoding.EncodeToString(imageData)
//log.Println(base64String)
textData := clipboard.Read(clipboard.FmtText)
log.Println(string(textData))
}
}()
wg.Wait()
}
func listenHotkey(key hotkey.Key, mods ...hotkey.Modifier) (err error) {
var ms []hotkey.Modifier
ms = append(ms, mods...)
hk := hotkey.New(ms, key)
err = hk.Register()
if err != nil {
return
}
<-hk.Keydown()
log.Printf("hotkey: %v is down\n", hk)
<-hk.Keyup()
log.Printf("hotkey: %v is up\n", hk)
hk.Unregister()
return
}
Should I be able to use the library to make command+control be a hotkey combo on MacOS?
The program is just stuck on err := hk.Register()
, no error raised.
But if I downgrade to version 0.2.1, it works.
Affected Versions: 0.3.0, 0.4.0
OS: macOS 13.0
CPU: M1 Pro
The .Register() call succeeds without returning an error, but the shortcuts I'm trying (Ctrl + Shift + O and Ctrl + Shift + P) just aren't receiving any events. Using sudo showkey
I see the same keycodes for ctrl (29), shift (42), and O (24) as I do in a Ubuntu 22 VM where the hotkey does work. Are there dependencies that may not be present in Ubuntu 18? Any ideas on how to debug?
As subject goes, can we use other keys to listen for - for example, Alt (Linux / Windows) and Space ?
I tried to pass the key code myself instead of key.KeyS
for example, but got an error about integer overflow, which is expected.
For example, when tried to use Alt from the keysymdef.h
file
#define XK_Alt_L 0xffe9 /* Left alt */
and passing it down to hotkey.New()
, you get the error about int overflow
because
type Key uint8
What is the reason for this limitation and would it be possible to workaround / change it?
Thanks for the library!
I'm try to detect key which press for long time under the windows platform, but it's seem like no ways.
When I press key, and keep on, until I release the key after a while. And the log tell me that the Keydown and Keyup trigger many times.
So are there any ways to detect key holding press? Thx.
The current Hotkey.Listen API can only trigger hotkey's keydown events. This lacks the ability to listen to keyup events.
We need to break the current API to enable this ability in the next version.
I want to use a backtick/grave (`), but I want it to work across all platforms, how do I achieve this? Thanks.
Thanks for the library, it works like a charm.
Just wanted to ask why Space
key is not available with this package?
I could make a PR to add the space key here if wanted
Would it be possible to add the ability to recognize media key events? (PlayPause / Prev / Next)? Especially on Windows this would be very useful. (Windows's native media key API doesn't really work for non-UWP apps)
For example, listening to Ctrl+1 and Ctrl+2 at the same time.
my test failed on OSX, is this not allowed, or is it used incorrectly?
package main
import (
"context"
"golang.design/x/hotkey"
"golang.design/x/mainthread"
)
func register(tag chan bool, mods []hotkey.Modifier, k hotkey.Key, output string) {
go func() {
hk, err := hotkey.Register(mods, k)
if err != nil {
panic("hotkey registration failed")
}
triggered := hk.Listen(context.Background())
<-tag
for range triggered {
println("hotkey", output)
}
}()
}
func fn() {
c := make(chan bool, 1)
c <- true
register(c, []hotkey.Modifier{hotkey.ModCtrl}, hotkey.Key1, "1")
c <- true
register(c, []hotkey.Modifier{hotkey.ModCtrl}, hotkey.Key2, "2")
}
func main() {
mainthread.Init(fn)
}
There seems to be an error in hotkey_windows.go at line 26. It always gives KeyS as the key instead of the key from hk.
I am still fairly new to golang, so I am not sure if how I think it should be fixed would work.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.