函数 CGEventKeyboardSetUnicodeString 采用 UniCharCount 和 const UniChar unicodeString[]。我无法弄清楚如何使用 pyobjc 从 python 调用它。理想情况下,我希望能够只传入一个 python unicode 字符串。有没有办法修改这个函数的元数据,所以我可以这样做?或者,有没有办法让我将 python unicode 字符串转换为 UNICHAR 数组和使用 pyobjc 的长度?
澄清:
我正在使用 2.5.1 版的 pyobjc
CGEventKeyboardSetUnicodeString 的元数据:
>>> pprint(CGEventKeyboardSetUnicodeString.__metadata__())
{'arguments': ({'already_cfretained': False,
'already_retained': False,
'null_accepted': True,
'type': '^{__CGEvent=}'},
{'already_cfretained': False,
'already_retained': False,
'type': 'L'},
{'already_cfretained': False,
'already_retained': False,
'null_accepted': True,
'type': '^T'}),
'retval': {'already_cfretained': False,
'already_retained': False,
'type': 'v'},
'variadic': False}
我尝试了以下方法:
>>> CGEventKeyboardSetUnicodeString(event, 1, 'a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: depythonifying 'pointer', got 'str'
>>> CGEventKeyboardSetUnicodeString(event, 1, u'a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: depythonifying 'pointer', got 'unicode'
>>> CGEventKeyboardSetUnicodeString(event, 1, [ord('a')])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: depythonifying 'pointer', got 'list'
>>> Quartz.CGEventKeyboardSetUnicodeString(event, 1, struct.unpack('>{}H'.format(1), u'a'.encode('utf-16-le')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: depythonifying 'pointer', got 'tuple'