如何在Mac上的Python中使用CGEventCreateKeyboardEvent? [英] How to use CGEventCreateKeyboardEvent in Python on Mac?

查看:171
本文介绍了如何在Mac上的Python中使用CGEventCreateKeyboardEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了pyobjc(与Quartz一起使用),我想知道如何使用CGEventCreateKeyboardEvent正确创建键盘事件?请?我根本无法在互联网上找到它,而且我什至不知道要导入什么.

I have installed the pyobjc (with it Quartz), and I would like to know how one would properly create a keyboard event with CGEventCreateKeyboardEvent? Please? I can not find it at all on the internet, and plus I even have no idea what to import.

一个示例代码会很不错,告诉我要导入什么以及要在python中放入什么

An example code would be nice, telling me what to import and what to put into python

有人知道Mac中CGEventCreateKeyboardEvent的 FN(功能键) 所需的代码吗?

Does anyone know the required code for the FN (FUNCTION KEY) in mac for CGEventCreateKeyboardEvent??

推荐答案

evt = Quartz.CGEventCreateKeyboardEvent(None, vkey, True)

仅此而已.

如果您可以在C中找到示例,例如

And if you can find examples in C, like this one in the docs, it's trivial to map them to Python.

C:

CGEventRef event1, event2, event3, event4;
event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true);
event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, true);
event3 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, false);
event4 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);

Python:

events = [Quartz.CGEventCreateKeyboardEvent(None, 56, True),
          Quartz.CGEventCreateKeyboardEvent(None, 6, True),
          Quartz.CGEventCreateKeyboardEvent(None, 6, False),
          Quartz.CGEventCreateKeyboardEvent(None, 56, False)]

对于导入内容",如果不太明显:import Quartz.

As for "what to import", if it's not obvious: import Quartz.

如果要将键映射到键代码,则C文档可以类似地转换为Python,但是这个简单的库封装低级函数并将其公开给Python.

If you want to map keys to key codes, the C docs can similarly be translated to Python, but this simple library wraps up the low-level functions and exposes them to Python.

如果您想以一种很好的图形方式来了解正在通过系统发送哪些事件,请尝试事件抽头测试台.如果您使用的是Maverick,则必须阅读有关Mavericks兼容性的说明,否则它将不起作用.无论如何,运行它,添加一个按键,例如向下键",向上键"和已更改标志",单击当前事件"或事件历史记录",然后观察键码飞过.

If you want a nice graphical way to find out what events are being sent through your system, try Event Taps Testbench. If you're on Maverick, you must read the note on Mavericks compatibility or it will not work. Anyway, run it, Add a tap on, e.g., Key Down, Key Up, and Flags Changed, click Current Event or Event History, and watch the key codes fly by.

这篇关于如何在Mac上的Python中使用CGEventCreateKeyboardEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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