auto-accept/mouselogger/mouselogger.py

55 lines
1.2 KiB
Python

import sys
import time
from pynput import mouse
# default logfile
logfile="mouselog.log"
try:
sys.argv[1]
except IndexError:
print("no logfile given, appending to %s" % logfile)
else:
logfile=sys.argv[1]
print("logfile set to %s" % logfile)
f = open(logfile, "a")
print("now logging all mouse input..")
def on_move(x, y):
f.write('{0:.7f} Pointer moved to {1}\n'.format(
time.time(), (x, y)))
def on_click(x, y, button, pressed):
f.write('{0:.7f} {1} {2} at {3}\n'.format(
time.time(),
'Pressed' if pressed else 'Released',
button,
(x, y)))
#if not pressed:
# Stop listener
# return False
def on_scroll(x, y, dx, dy):
f.write('{0:.7f} Scrolled {1} at {2}\n'.format(
time.time(),
'down' if dy < 0 else 'up',
(x, y)))
# # Collect events until released
# with mouse.Listener(
# on_move=on_move,
# on_click=on_click,
# on_scroll=on_scroll) as listener:
# listener.join()
# ...or, in a non-blocking fashion:
listener = mouse.Listener(
on_move=on_move,
on_click=on_click,
on_scroll=on_scroll)
listener.start()
input("press ENTER to stop logging")
print("ok thanks, logged all mouse input to %s" % logfile)