pythonpython-3.xwinapipywin32pyhook

Record (get) mouse click position while key is pressed and stop recording when same key is released in python


I am creating a script where if user press f7 it will start recording mouse clicks and when he releases the button it should stop and this happens unless user closes the program.

The script is printing "None" inspite of pressing f7 and instead of showing click position and "f7", it is showing None.

In on_press function when we print the value, it is showing "f7" but when clicking the mouse button in on_click function, it is showing "None"

Here is the code

from pynput import mouse, keyboard
from pynput.keyboard import Key, Listener
import pickle

x_pos = []
y_pos = []
both_pos = []

pressed_key = None

def on_press(key):
    if (key==keyboard.Key.f7):
        pressed_key = "f7"
    else:
        pressed_key = None

def on_release(key):
    pass

def on_click(x, y, button, pressed):
    if pressed:
        #print ("{0} {1}".format(x,y))
        print(pressed_key)
        if pressed_key == "f7":
            x_pos.append("{0}".format(x,y))
            y_pos.append("{1}".format(x,y))
            #print("test" + x_pos + y_pos)
            print (x_pos + y_pos)
        #both_pos = x_pos, y_pos
        else:
            pass
        print (x_pos + y_pos)


mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()

with keyboard.Listener(on_press = on_press, on_release = on_release) as listener:
    try:
        #listener.start()
        listener.join()
    except MyException as e:
        print('Done'.format(e.args[0]))

Solution

  • Found the issue. Because in on_press i was not using global pressed_key so it was creating local variable.

    Here is the working code.

    from pynput import mouse, keyboard
    from pynput.keyboard import Key, Listener
    import pickle
    
    x_pos = []
    y_pos = []
    both_pos = []
    
    pressed_key  = None
    
    def on_press(key):
        global pressed_key
        if (key==keyboard.Key.f7):
            pressed_key = "f7"
            print(pressed_key)
        else:
            pressed_key = None
    
    def on_release(key):
        global pressed_key
        pressed_key = None
    
    def on_click(x, y, button, pressed):
        if pressed:
            #print ("{0} {1}".format(x,y))
            print(pressed_key)
            if pressed_key == "f7":
                x_pos.append("{0}".format(x,y))
                y_pos.append("{1}".format(x,y))
                #print("test" + x_pos + y_pos)
                print (x_pos + y_pos)
            #both_pos = x_pos, y_pos
            else:
                pass
            print (x_pos + y_pos)
    
    
    mouse_listener = mouse.Listener(on_click=on_click)
    mouse_listener.start()
    
    with keyboard.Listener(on_press = on_press, on_release = on_release) as listener:
        try:
            #listener.start()
            listener.join()
        except MyException as e:
            print('Done'.format(e.args[0]))