Reputation: 51
I want to write a function that can get a bounded area of the screen by listening to the mouse and wait for 2 mouse clicks one for each of the corners of a bounded area on the screen, and return the coordinates for each point. I know how you can set you can set up pynput to act on events such as mouse button presses, but I want to enter a state where I listen for the event and then get its position.
def grabArea():
with Listener(on_click=on_click) as listener:
listener.join()
pos1 = mouse.position
with Listener(on_click=on_click) as listener:
listener.join()
pos2 = mouse.position
return pos1[0],pos1[1],pos2[0],pos2[1]
this is what I came up with but it just seems very clunky to me
Upvotes: 0
Views: 2431
Reputation: 367
This function will hold a list of 2 points clicked.
def grabArea():
points = []
def on_click(x, y, button, pressed):
if pressed:
points.append([x, y])
if not pressed and len(points) == 2:
return False
with Listener(on_click=on_click) as listener:
listener.join()
return points
positions = grabArea()
position1 = positions[0]
x1, y1 = position1
Upvotes: 2