2

I am trying to make a windows analog of the Chat-wheel from Dota 2 or Battlefield with some fast commands like this.

How it should be: While the G-key is pressed - the wheel with some sections appears at the centre of the screen, with the mouse centered on it and if I move the mouse to side_1 and then release the G-key -> func_1 executes. If it was on side_2 -> func_2. If I release the G-key at the centre -> wheel disappears and nothing happens.

So to monitor the X Y coordinates of the mouse I use following code:

from pynput import mouse
    
def on_move(x, y):
    print(x, y)
    
    
# Collect events until released
with mouse.Listener(on_move=on_move) as listener:
    listener.join()

It is spamming coordinates when I move the mouse. But I am stuck in the part where i need:

  1. to start the listener from another .py file and take these coordinates for the visual part of the program (like highlight the side of the wheel with command descriptions),

  2. to close the listener from another .py file and take the last coordinates of the mouse when the G-key is released to compare with tge range of coordinates prescribed for functions.

Ewran
  • 328
  • 4
  • 15
LogaN4ik
  • 71
  • 8

2 Answers2

0

You want something like this:

from pynput import mouse, keyboard
import time


def on_press(key):
    print("pressed {}".format(key))

def on_release(key):
    print("released {}".format(key))

def on_move(x,y):
    print((x,y))


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

key_listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release)
key_listener.start()

# prevent the script from exiting immediately
time.sleep(10)
meowulf
  • 367
  • 1
  • 5
  • 14
mmiron
  • 164
  • 5
0
import time
import keyboard

from pynput.mouse import Controller

def get_xy():
    mouse = Controller()
    mouse_coord = str(mouse.position)
    mouse_coord = mouse_coord.replace("(", "")
    mouse_coord = mouse_coord.replace(")", "")
    mouse_coord = mouse_coord.replace(",", "")
    mouse_coord = mouse_coord.split(" ")
    mouse_x = int(mouse_coord[0])
    mouse_y = int(mouse_coord[1])
    print(mouse_x, mouse_y)

while True:
    if keyboard.is_pressed('b'):
        get_xy()
        time.sleep(0.01)

Output example: 654 326

LogaN4ik
  • 71
  • 8