go - Robotgo无法收听多个事件

标签 go mingw robot robotgo

我正在使用robotgo来监听键盘事件。我想为每个按键添加自己的功能,但是我无法让它触发超过1个按键。

因此主要功能如下:

func main() {
    go addKeyListen("l")
    go addKeyListen("k")
}

包装函数:
func addKeyListen(key string) {
    for {
        if ok := robotgo.AddEvent(key); ok {
            fmt.Println("Pressed "+key)
        }
    }
}

L后,我得到的只是Pressed l多次,但是我可以用一个标志来解决。按K不会打印任何内容。

尝试了以下内容:
for {
    if okA := robotgo.AddEvent("k"); okA {
        fmt.Println("Pressed k")
    }
    if okB := robotgo.AddEvent("l"); okB {
        fmt.Println("Pressed l")
    }
}

先按K再按L,然后一遍又一遍地重复此操作似乎会触发事件,但如果我更改按键顺序则不会触发。因此,如果我先从L开始,然后从K开始,则什么也不会发生。

PD:使用MinGW64版本x86_64-8.1.0-posix-seh-rt_v6-rev0从Windows 10进行测试

从4开始还尝试了不同版本的MinGW,结果相同...

最佳答案

根据在Mac 10.14上测试过的robotgo和lib,尝试使用类似的方法:

package main

import (
    "fmt"
    "github.com/go-vgo/robotgo"
    gohook "github.com/robotn/gohook"
)

func main() {
    eventHook := robotgo.Start()
    var e gohook.Event
    var key string

    for e = range eventHook {
        if e.Kind == gohook.KeyDown {
            key = string(e.Keychar)
            switch key {
            case "k":
                fmt.Println("pressed k")
            case "l":
                fmt.Println("pressed l")
            default:
                fmt.Printf("pressed %s \n", key)
            }
        }
    }
}

:)

关于go - Robotgo无法收听多个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61684526/

相关文章:

go - Go 中的多行缓冲读取

http - 确定在 http.Handler 中将 HTTP 请求发送到的 IP

Java 机器人类 mouseclick 可以使用浮点值吗?

php - 允许 Google 绕过 CAPTCHA 验证 - 明智与否?

regex - 如何使用正则表达式在 golang 的括号内获取所有内容

go - 从 channel 附加 slice

c++ - 如何在 C++ 中使用没有 header 的 DLL

c++ - 有没有人在 Windows 7 机器上成功编译过 Vowpal Wabbit?

c++ - 我怎样才能让 C++0x 和 __STRICT_ANSI__ 相处融洽?

.htaccess - mod_rewrite 所有 robots.txt 请求到默认文件,服务器范围