简体   繁体   中英

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!

The e variable in print_event is used in a way I haven't seen before. 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

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.

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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