golang-design/hotkey

Are there any ways to detect key holding press?

Closed this issue · 5 comments

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 package already supports KeyDown, KeyUp event, which you can observe holding press when you have not received KeyUp.

If I remember correctly, Windows automatically releases key presses, meaning that you will have to configure the system (somewhere in the OS settings) not to do that.

Thanks for your reply.

Detecting key holding pressed or not by KeyDown and KeyUp seem working. But, there is a problem with KeyDown and KeyUp. Program will be blocked when holding press until releases key, and it's will cost long time to release the blocked.

Here is my code:

package main

import (
	"golang.design/x/hotkey"
	"golang.design/x/hotkey/mainthread"
	"log"
)

func main() {
	mainthread.Init(fn)
} // Not necessary when use in Fyne, Ebiten or Gio.

func fn() {
	hk := hotkey.New([]hotkey.Modifier{}, hotkey.KeyF1)
	err := hk.Register()
	if err != nil {
		log.Fatalf("hotkey: failed to register hotkey: %v", err)
		return
	}
	defer hk.Unregister()

	log.Printf("hotkey: %v is registered\n", hk)
	for {
		select {
		case <-hk.Keydown():
			log.Printf("hotkey: %v is down\n", hk)
			break
		case <-hk.Keyup():
			log.Printf("hotkey: %v is up\n", hk)
			break
		default:
			//log.Printf("default")
		}
	}
	log.Printf("hotkey: %v is unregistered\n", hk)
	return
}

Does your PR solves this issue?

Does your PR solves this issue?

Yes, the PR solved this issue.

Thanks! I am closing this now...