简体   繁体   English

Python 键盘侦听器

[英]Python keyboard listener

I have used this code and it runs fine.我已经使用了这段代码,它运行良好。 However, there is something weird about it, it's like it's not Python!然而,它有一些奇怪的地方,就像它不是 Python!

The e variable in print_event is used in a way I haven't seen before. print_event 中的 e 变量以我以前从未见过的方式使用。 It's a regular function that prints whatever is passed to it, but the problem is how it's used, even the event variable that's supposed to be passed as an argument to the parameter e这是一个常规函数,它打印传递给它的任何内容,但问题在于它是如何使用的,甚至是应该作为参数传递给参数 e 的事件变量

If you don't pay attention, it seems like the append function returns added values to print_event, instead of appending them, like what the append does in Python.The whole function gets appended to the list of handlers once and then it keeps running until the program terminates,like it's a while True loop.如果您不注意,似乎 append 函数将添加的值返回给 print_event,而不是附加它们,就像 append 在 Python 中所做的那样。整个函数被附加到处理程序列表一次,然后它一直运行直到程序终止,就像一个 while True 循环。

The code basically starts a keyboard listener and keeps recording key pressed keys, but what happens to the keys is the quesion.该代码基本上启动了一个键盘侦听器并不断记录按键按下的键,但是键发生了什么是问题。 The for loop in low level listener doesn't make sense, why iterate through the handlers if it's supposed to record the keys, not read them.低级侦听器中的 for 循环没有意义,如果应该记录键而不是读取它们,为什么要遍历处理程序。 Besides, why pass the event?此外,为什么要通过事件? Handlers is a list, not a function, i'm only aware of the assignement operator for initializing variables处理程序是一个列表,而不是一个函数,我只知道用于初始化变量的赋值运算符

Also, if handlers is initialize empty, how does it assign values to items and through them if their memory space isn't allocated and doesn't exist?此外,如果处理程序初始化为空,如果它们的内存空间未分配且不存在,它如何为项目分配值并通过它们分配值?

I don't seeing any buffer function being called, so how's it working?我没有看到任何缓冲函数被调用,它是如何工作的? Python shouldn't look like that Python 不应该是那样的

What i'm trying to do is to access the handlers list in real time and process the events我想要做的是实时访问处理程序列表并处理事件

An explanation would be appreciated.一个解释将不胜感激。 Thanks in advance提前致谢

Are you asking about Function Variables?你问的是函数变量吗?

If yes, you can pass around functions like any other variable, and call them later by a different name.如果是,您可以像传递任何其他变量一样传递函数,然后用不同的名称调用它们。

EG:例如:

def hi(string):
    print(string)

fns = [hi, hi]

for fn in fns:
    fn('hello')

If that remains puzzling, perhaps you could step through it with a debugger to make the idea seem more concrete.如果这仍然令人费解,也许您可​​以使用调试器逐步完成,以使这个想法看起来更具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM