如何在OSX上的python中将字符串转换为一组击键 [英] How to convert a string into a set of keystrokes in python on OSX

查看:88
本文介绍了如何在OSX上的python中将字符串转换为一组击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个字符串,我希望能够发送一组击键来键入该字符串,并且我希望能够在OSX上的python中进行操作(在python中,因为它已经是一个较大项目的一部分了在OSX上以python编写,因为我正尝试将其移植到OSX.

Given a string, I'd like to be able to send a set of keystrokes to type that string and I'd like to be able to do it in python on OSX (in python because it's part of a larger project already written in python, on OSX because I am trying to port it to OSX).

我现在可以像这样使用pyobj来做到这一点(这有所简化):

I'm able to do this now using pyobj like so (this is somewhat simplified):

from Quartz import *

CHAR_TO_SEQUENCE = {
    'a':[(0, True), (0, False)]
}

def send_string(string):
    for c in string:
        sequence = CHAR_TO_SEQUENCE[c]
        for keycode, key_down in sequence:
            CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(None, keycode, key_down))

并且我充实了CHAR_TO_SEQUENCE以便包含我可以在键盘上键入的大部分内容,这花了一段时间并且很乏味.

and I've fleshed out CHAR_TO_SEQUENCE to include most of what I can type on my keyboard, which took a while and was tedious.

与此有关的问题是:
-仅在键盘具有ANSI I标准布局时才有效.例如,如果有人使用法语键盘,则会输入错误的内容.
-它需要这张荒谬的桌子.

The problems with this are:
- It only works while the keyboard has the ANSII standard layout. If someone uses a french keyboard, for example, it will type the wrong things.
- It requires this ridiculous table.

我找到了OSX的通用解决方案,但不知道如何将其应用于python: 如何将ASCII字符转换为CGKeyCode?

I found this general solution for OSX but couldn't figure out how to apply it to python: How to convert ASCII character to CGKeyCode?

提到的API似乎无法通过pyobj使用(或者我只是无法弄清楚导入路径).

The API mentioned there doesn't seem to be available via pyobj (or maybe I just couldn't figure out the import path).

我已经看到一些建议,将CGEventKeyboardSetUnicodeString设置为所需的字符串,而不必担心键码.但是我无法弄清楚如何从python调用CGEventKeyboardSetUnicodeString(我无法正确获取参数),而且尚不清楚这是否行得通,因为该文档说应用程序可以选择忽略它,而使用键码.

I've seen some suggestions to set CGEventKeyboardSetUnicodeString to the desired string and not worry about the keycode. But I wasn't able to figure out how to call CGEventKeyboardSetUnicodeString from python (I can't get the arguments right) and it's not clear that this will work because the documentation says that applications can choose to ignore this in favor of the keycode.

有没有办法在OSX上的python中做到这一点?

Is there a way to do this in python on OSX?

推荐答案

Carbon模块似乎没有包装TIS *函数,并且也没有其他功能.

It looks like the Carbon modules don't wrap the TIS* functions, and neither does anything else.

您可以扩展PyObjC,但是构建一个简单的扩展模块来包装您实际需要的两个功能要简单得多.

You could extend PyObjC, but it's much simpler to just build a trivial extension module that wraps the two functions you actually need.

因为这样做比解释方法要快,所以可以从 https:/中获取它./github.com/abarnert/pykeycode ,只需执行通常的"python setup.py build_ext --inplace"或"sudo python setup.py install",然后查看test.py以了解如何使用它.

Since it was faster to just do it than to explain how to do it, you can get it from https://github.com/abarnert/pykeycode and just do the usual "python setup.py build_ext --inplace" or "sudo python setup.py install", then look at test.py to see how to use it.

这篇关于如何在OSX上的python中将字符串转换为一组击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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