I'm trying to use This script from pynput to monitor my mouse, but its too resource-intensive.
Tried to import time
and add time.sleep(1)
after on_move(x, y)
function but when you run it your mouse drives crazy.
Here's the overall code:
import time
def on_move(x, y):
print('Pointer moved to {0}'.format((x, y)))
time.sleep(1) # <<< Tried to add it over here cuz it takes most of the process.
def on_click(x, y, button, pressed):
print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
if not pressed:
return False
def on_scroll(x, y, dx, dy):
print('Scrolled {0}'.format((x, y)))
with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
listener.join()
You could use a thread to run your code when do some task which will block your code.(In your code, sleep(1)
will block the code),Whatever, this works fine on my PC:
from pynput.mouse import Listener
import time
import threading
def task(): # this is what you want to do.
time.sleep(1) # <<< Tried to add it over here cuz it takes most of the process.
print("After sleep 1 second")
def on_move(x, y):
print('Pointer moved to {0}'.format((x, y)))
threading.Thread(target=task).start() # run some tasks here.
def on_click(x, y, button, pressed):
print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
if not pressed:
return False
def on_scroll(x, y, dx, dy):
print('Scrolled {0}'.format((x, y)))
with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
listener.join()