Search code examples
python-3.xpython-2.7keyboard-eventskeylogger

Please explain the working of following code and some questions mentioned below


import keyboard
def fun1(r):
    print(r.name)    
keyboard.on_press(fun1)

the code is a simple key logger, what exactly is happening here?

what I understood so far is:

  1. import
  2. function definition
  3. keyboard.on_press is called

Please explain the following things

  1. what exactly is keyboard.on_press(fun1) is passing to fun1()
  2. why having a parameter is important for fun1
  3. what if i dont wanna make a function just wanna put my code in [keyboard.on_press("here")], why would it not be possible.

Few more questions

with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()
  1. whats up with "with" statement here?
  2. what does .join() {what it means t have it joined it to main thread}
  3. where have we written on_press=on_press {why not just once}

I dont know if this query is dependent on version of python or version of module. I am using all latest versions. So far I read the documentation on https://pynput.readthedocs.io/en/latest/keyboard.html and googles all my questions but could not find the easy explanation.


Solution

  • Here's some code with comments to help explain the syntax:

    import keyboard
    
    def somefunc(msg):
        print(msg)  # here
    
    def fun1(r):  # r is keyboard event
        print(type(r))   #  <class 'keyboard._keyboard_event.KeyboardEvent'>
        print(r.name)   # key text
        somefunc("here")   # call some other function
        
    keyboard.on_press(fun1) # pass a reference to the other function (on_press will call whatever function we pass), other function must have single parameter
    
    while True: pass  # keep script running
    

    The With keyword just ensures the the object is closed properly even if there is an error.

    with keyboard.Listener(
            on_press=on_press,   # named parameter is on_press, we are passing a reference to function on_press
            on_release=on_release) as listener:  # named parameter is on_release, we are passing a reference to function on_release
        listener.join()  # wait for listener thread to finish
    
    # Is shortcut syntax for:
    
    listener = keyboard.Listener(on_press=on_press, on_release=on_release)
    try:
        ........
    finally:
        listener.stop()  # always do this, even if error
    listener.join()
    

    About the double variable. It looks weird, I know. The code is using named parameters.

    # sample function
    def myfunc(x):   # the parameter name is x
        print(x)
        
    # We can call myfunc several ways:
    myfunc(1)  # pass value directly
    myfunc(x=1)  # specify parameter to assign
    
    x=1  # local variable
    myfunc(x)  # pass the local variable as parameter
    myfunc(x=x)  # specify parameter to assign, pass local variable # this is the double text you're seeing  
    

    Concerning the callback function, you can pass a function reference to another function.

    def MyCallback(a,b):  # custom function
        print(a,b) 
        
    def SomeEventHandler(f): # parameter is reference to another function 
        f(1,2)  # call passed in function, it must have these parameters
        
    SomeEventHandler(MyCallback)  # pass custom function to handler
    

    I hope this clears things up a bit.