I am using pynput to send my mouse position every 0.2 seconds. I tried timing it:
from pynput.mouse import Button, Listener as MouseListener
def on_move(x,y):
global start_time
time_passed = start_time - time.time()
if time_passed >= 0.2:
command = f'{None}\n{x}\n{y}'
commands_q.put(command)
start_time = time.time()
mouse_listener = MouseListener(on_move=on_move)
mouse_listener.start()
mouse_listener.join()
This code doesn't work. It crushes my program mid running (the program send the location of the mouse using sockets)
Any tips?
I'm not sure what you are trying to achieve, but a simple code like this can work for you :
from pynput.mouse import Controller
import time
mouse = Controller()
while True :
command = mouse.position
commands_q.put(command)
time.sleep(0.2)
For two listener mouse and keyboard both, use this kind of code :
from pynput.mouse import Controller as mouse_control
from pynput import keyboard as key_control
mouse = mouse_control() #mouse controller
def on_press (key) :
print(key.char)
listener = key_control.Listener(on_press=on_press) #keyboard listener
listener.start()
while True :
command = mouse.position
print(command)
time.sleep(0.2)