4

I am using Python 3.9.7 with pynput, I wanted to retrieve the x and y position of mouse both clicking and releasing separately and saved it into variables outside of the function on_click (e.g: px = x position when pressed, rx = x position when released) for other function usage

The code are as follows , code are modified from Pynput documentations:

from pynput import mouse
import time

px = 0
py = 0
rx = 0
ry = 0

pressed_location = 0, 0
released_location = 0, 0

def on_click(x, y, button, pressed):
    global pressed_location
    global released_location
    global px,py,rx,ry
    
    if pressed:
        pressed_location = x, y
        px = x
        py = y
    else:
        released_location = x, y
        rx = x
        ry = y
        #debugging inside functions
        #print('You pressed at {0}x{1} and released at {2}x{3}'.format(*pressed_location, *released_location))
        print('Inside function pressed at {0}x{1}'.format(*pressed_location, *released_location))
        print('Inside function released at {2}x{3}'.format(*pressed_location, *released_location))
        return False
    
listener = mouse.Listener(on_click=on_click)
listener.start()

#debugging outside functions
print ("Outside function pressed: ", px , "x" ,py)
print ("Outside function released: ", rx , "x" , ry)  

However I am stumped as with the output (outside the functions as it still shows 0 whereas as the variable inside the function is showing actual values.)Sample results is as followed: I just wanted the values of variable inside the function can be "transferred" to the varible outside of the functions. Thanks in advance.

Outside function pressed:  0 x 0
Outside function released:  0 x 0
>>> Inside function pressed at 293x249
Inside function released at 768x815

rr

Alan Koh W.T
  • 413
  • 4
  • 19
  • define some global variables before your `def` and then instead of format use if else statement so if `pressed` then you can set your mx value and else set it to your mrx value – iѕєρєня Sep 06 '21 at 16:10
  • something like this: `mx = 0 mr = 0` and in the `def` use: `if pressed: mx = x else: mrx = x` – iѕєρєня Sep 06 '21 at 16:16

2 Answers2

1

There's probably a better method, but this works:

from pynput import mouse
import time

pressed_location = 0, 0
released_location = 0, 0

def on_click(x, y, button, pressed):
    global pressed_location
    global released_location
    if pressed:
        pressed_location = x, y
    else:
        released_location = x, y
        print('You pressed at {0}x{1} and released at {2}x{3}'.format(*pressed_location, *released_location))

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

while True:
    time.sleep(1)

I've created a global variable pressed_location, which we give a default value to. If the user presses the mouse, the location gets stored in there. If the user releases the mouse, we show the pressed location and the released location.

Example output:

You pressed at 103x412 and released at 299x559
You pressed at 207x478 and released at 207x478

In the first event above I moved the mouse after pressing. In the second event I just clicked.

Robson
  • 2,008
  • 2
  • 7
  • 26
0

After tinkering codes for quite a while, it works. Thank you to iѕєρєня and Robson for your help

from pynput import mouse
import time

global pressed_location
global released_location
#Initialize
px = py = rx = ry = 0

def on_click(x,y, button, pressed):
    global px
    global py
    global rx
    global ry
    
    if pressed:
        px, py = x, y

    else:
        rx , ry = x, y

    if not pressed:
        print("Inside function pressed x : " ,px , " ", py)
        print("Inside function released y : " ,rx, " ", ry)

        return False
        return px
        return py
        return rx
        return ry

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

# some times needed for function to finish register the mouse location
time.sleep(3)
print("Outside function pressed x : " ,px , " ", py)
print("Outside function released y : " ,rx, " ", ry)

Output as followed:

Inside function pressed x :  505   206
Inside function released y :  1084   892
Outside function pressed x :  505   206
Outside function released y :  1084   892
Alan Koh W.T
  • 413
  • 4
  • 19