Pynput键盘侦听器无法在Mac OS X上检测到按键 [英] pynput keyboard listener does not detect keys on Mac OS X

查看:294
本文介绍了Pynput键盘侦听器无法在Mac OS X上检测到按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pynput通过OS X Mojave上的Listener记录击键.我将Pycharm用作运行代码的IDE.

I am using pynput to record keystrokes via Listener on OS X Mojave. I am using Pycharm as my IDE for running the code.

在pynput网站上使用相同的示例,我无法使其正常工作.

I was not able to get it to work using the same example from the pynput site.

from pynput.keyboard import Listener as key_listener

class recorder:

    def on_press(self, key):
        print(key)

    def on_release(self, key):
        print(key)


if __name__ == "__main__":
    testme = recorder()

    with key_listener(on_press=testme.on_press, on_release=testme.on_release) as listener:
        listener.join()

我确实完成了所有步骤,并且没有任何错误(除非我将with语句放在函数中而不是在主函数中,但这是Mojave中线程处理的一个已知问题,根据我在搜索后可以发现的内容)该错误),但一切都在.join()语句处停止,并且在按下并释放键盘上的键后,我什么也没打印.

I did step through it and I get no errors (unless I put the with statement in a function, instead of in the main, but that's a known issue with threading in Mojave, from what I can tell after searching for that error), but everything stops at the .join() statement, and I get nothing printed when I press and release a key on my keyboard.

推荐答案

这可能有点晚了,但是答案是:

This is probably a bit late, but the answer is to go into:

  1. 设置-> Security & Privacy
  2. 点击Privacy标签
  3. 单击+并按住CMD + SHIFT + .(这样您就可以查看隐藏的内容 文件/文件夹)
  4. 导航到/usr/local/bin或安装Python的任何地方
  5. 单击okay.
  1. Settings -> Security & Privacy
  2. Click on the Privacy tab
  3. Click the + Hold down CMD + SHIFT + . (so that you can see hidden files/folders)
  4. Navigate to /usr/local/bin or wherever you have Python installed
  5. Click okay.

应该这样做.

注意

如果您尝试通过终端运行应用程序,则需要将terminal.app添加到允许的应用程序列表中,就像上面对Python所做的那样.

If you try to run your app via the terminal, you will need to add the terminal.app to the list of allowed apps, as done above for Python.

这篇关于Pynput键盘侦听器无法在Mac OS X上检测到按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆