robotgo
robotgo copied to clipboard
KeyToggle usage is fuzzy
- Robotgo version (or commit ref): 0.90.2
- Go version: 1.12.5
- Gcc version: 8.1.0
- Operating system and bit: Windows 10 64bit
- Resolution: 1920 * 1080
- Can you reproduce the bug at Examples:
- [ ] Yes (provide example code)
- [ ] No
- [x] Not relevant
- Provide example code:
if add != "" {
robotgo.KeyToggle(keyMap[rnode], "down") // When I found that "j" is pressed for a short time, I add this line like what example/key/main.go writes
err := robotgo.KeyToggle(keyMap[rnode], "down", add)
if err != "" {
fmt.Println(err)
}
robotgo.MilliSleep(time)
robotgo.KeyToggle(keyMap[rnode], "up", add)
}
- Log gist:
Description
I don't quite understand how to use .KeyToggle. When I want to enter ctrl + j for about 1200ms, I use KeyToggle like above code, however, it seems that only ctrl button is pressed for a long time and j button is only pressed for a very short time. Firstly I only wrote
robotgo.KeyToggle(keyMap[rnode], "down", add)
When I found it's not working, I added robotgo.KeyToggle(keyMap[rnode], "down") above just like what example/key/main.go did, but it's still not working. the keyMap[rnode] button is still pressed for a very short time, and the err is empty when I want to catch it if KeyToggle error happened. I can make sure that add="ctrl" because I used fmt.Println(add) to ensure it and the keyMap[rnode] is what I want. So is it because I'm confused with .KeyToggle's usage?
Hi there, about your confused:
- How to use .KeyToggle
// press 'j'
robotgo.KeyToggle("j", "down")
robotgo.KeyToggle("j", "up")
// press 'ctrl+j'
robotgo.KeyToggle("j", "down", "ctrl")
robotgo.KeyToggle("j", "up", "ctrl")
- Why pressed for a very short time
// press 'j'
robotgo.KeyToggle("j", "down")
robotgo.MilliSleep(5*1000) // the key 'j' is pressed, but not equal to press many times
robotgo.KeyToggle("j", "up")
// press 'ctrl+j'
robotgo.KeyToggle("j", "down", "ctrl")
robotgo.MilliSleep(5*1000) // the key `ctrl`and 'j' is pressed, but not equal to press many times
robotgo.KeyToggle("j", "up", "ctrl")
Because of winapi: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput
- How to enter ctrl + j for about 1200ms
You can use
go channel
, for example:
package main
import (
"fmt"
"github.com/go-vgo/robotgo"
)
func main() {
done := make(chan bool)
go func() {
for {
select {
case <-done:
robotgo.KeyToggle("j", "up", "ctrl")
return
default:
robotgo.KeyToggle("j", "down", "ctrl")
robotgo.MilliSleep(10) // It's better to sleep 10 milliseconds
}
}
}()
robotgo.MilliSleep(1200)
done <- true
fmt.Println("done...")
}