mouselogger.py

script for logging all mouse input to logfile
This commit is contained in:
merijn 2021-03-23 10:36:11 +01:00
parent f817dafb66
commit 41dc50bf16
2 changed files with 68 additions and 0 deletions

13
mouselogger/README.md Normal file
View file

@ -0,0 +1,13 @@
# mouselogger.py
Script that timestamps and logs all mouse input to file.
## requirements
works on python3.9
pynput module `pip install pynput`
## usage
from the commandline:
+ `python mouselogger.py` will log all mouse input to default logfile `mouselog.log` in the same folder
+ `python mouselogger.py [user-specified logfile]` will instead log to user-specified logfile

View file

@ -0,0 +1,55 @@
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)