[英]Acquiring keysym from keycode in python
I am currently working on a python project to enable the detection and interpretation of key events as characters/names across platforms. 我目前正在研究一个python项目,以实现跨平台将关键事件检测和解释为字符/名称。 I have been working with python-xlib to provide support on X11 but I have recently run into difficulty in the process of converting the keycodes of key events to their appropriate keysyms.
我一直在使用python-xlib在X11上提供支持,但是最近在将键事件的键代码转换为相应的keyym的过程中遇到了困难。
A snapshot of my code is here: https://gist.github.com/SavinaRoja/7306962 我的代码的快照在这里: https : //gist.github.com/SavinaRoja/7306962
The trouble arises specifically in the use of the PyKeyboardEvent class, where I am capable of retrieving the event data for key events, but not interpreting their keycodes to keysyms. 麻烦尤其是在使用PyKeyboardEvent类时出现,在该类中,我能够检索关键事件的事件数据,但无法将其关键代码解释为keyyms。 Xlib.display's keycode_to_keysym function does not work appropriately.
Xlib.display的keycode_to_keysym函数无法正常工作。
In reading about X11, and confirmed by the xev utility source code, it seems that the appropriate Xlib function to utilize is XLookupString. 在阅读有关X11的信息并通过xev实用程序源代码确认后,似乎可以使用的适当Xlib函数是XLookupString。 Were I coding in C (a language I am only beginning to learn) using this function would be simple, however I am currently stumped with regards to employing the function from python.
如果我使用此函数以C(我才刚刚开始学习的一种语言)进行编码会很简单,但是目前我对使用python中的函数感到困惑。 I have considered two options: I may attempt to utilize and extend python-xlib's protocol to call XLookupString, or I might try to use ctypes to call XLookupString.
我考虑了两个选择:我可能尝试利用并扩展python-xlib的协议来调用XLookupString,或者我可能尝试使用ctypes来调用XLookupString。 My code includes an attempt at the latter (not functional, but shows where I am at currently):
我的代码包括对后者的尝试(不起作用,但显示了我目前所在的位置):
from ctypes import *
XLookupString = cdll.LoadLibrary('libX11.so').XLookupString
char_buffer = create_string_buffer(257)
keysym = c_int() # the important value
count = XLookupString(byref(event), # TypeError, not a ctypes instance
byref(char_buffer),
len(char_buffer),
keysym,
None)
I suspect that what I am missing is the ability to convert the event class from python-xlib to an appropriate C struct for XLookupString, or the ability to interpret the data received from the X server (which python-xlib parses into the event instance) into such a struct. 我怀疑我缺少的是将事件类从python-xlib转换为XLookupString的适当C结构的能力,或解释从X服务器接收的数据的能力(python-xlib解析为事件实例)。变成这样的结构。 Am I correct in this suspicion?
我对此怀疑是否正确? If so, what tools do I need to learn?
如果是这样,我需要学习哪些工具?
At this point I am probably missing a lot of basic information and skill regarding the interface between python and C, so I devoting time to fixing this gap in my general education. 在这一点上,我可能会缺少很多有关python和C之间的接口的基本信息和技能,因此我花了一些时间来弥补通识教育中的这一差距。 If anyone has insight in to my problem specifically or how I should conduct my research on relevant topics, I would love to hear it.
如果有人特别了解我的问题,或者我应该如何对相关主题进行研究,那么我很乐意听到。
If you look at the source code to python-xlib, it's actually not using xlib at all; 如果您查看python-xlib的源代码,实际上它根本就没有使用xlib。 it's a pure-Python implementation of the Xlib socket protocols.
它是Xlib套接字协议的纯Python实现。 You
event
object is a wrapper around one of the types defined in the rq
module (or a subclass defined in event
or elsewhere), which is an object that knows how to struct.pack
itself. event
对象是rq
模块(或在event
或其他地方定义的子类)中定义的一种类型的包装,这是一个知道如何struct.pack
本身的对象。
The simplest thing to do is probably to pack the value into a byte string, then cast it to a ctypes.Structure
. 最简单的操作可能是将值打包到一个字节字符串中,然后将其转换为
ctypes.Structure
。
But note that the xlib that ctypes
will be entirely independent of your python-xlib
; 但是请注意,
ctypes
的xlib将完全独立于python-xlib
; in particular, you'll be using a different X connection (which means you may have to auth separately, repeat any setup, etc.). 特别是,您将使用其他X连接(这意味着您可能必须分别进行身份验证,重复任何设置等)。 It would probably be a lot cleaner to do everything one way or the other, not mix and match.
以一种或另一种方式而不是混合搭配来完成所有工作可能会更清洁。
Downloading the libX11 source , it looks like XLookupString
is not a protocol function, but rather a wrapper (in the file KeyBind.c
) that runs at least partially on the client side. 下载
XLookupString
源代码 ,看起来XLookupString
不是协议功能,而是包装程序(在文件KeyBind.c
),该包装程序至少部分在客户端运行。 Assuming USE_OWN_COMPOSE
is not defined, it effectively calls XTranslateKey
to get the keysym
result, then calls XTranslateKeySym
to fill the string buffer. 假设
USE_OWN_COMPOSE
,它将有效地调用XTranslateKey
以获取keysym
结果,然后调用XTranslateKeySym
来填充字符串缓冲区。 Those functions are themselves mostly local functions. 这些功能本身主要是局部功能。 But they ultimately call
XKeyInitialize
, which calls at least one server-side function, XGetKeyboardMapping
—which python-xlib does support. 但是他们最终调用了
XKeyInitialize
,它调用了至少一个服务器端函数XGetKeyboardMapping
-python-xlib 确实支持。
That implies that using a separate Xlib
instance through ctypes
is not necessarily going to get the right keyboard mapping, unless you can ensure that the two connections have identical setup. 这意味着通过
ctypes
使用单独的Xlib
实例不一定会获得正确的键盘映射,除非您可以确保两个连接具有相同的设置。
That also implies that you may have to port all of that local C code in order to get the functionality of XLookupString
. 这也意味着您可能必须移植所有本地C代码才能获得
XLookupString
的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.