新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python如何監(jiān)控鍵盤按了什么鍵
python如何監(jiān)控鍵盤按了什么鍵

惠民ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
1、安裝pynput
pip install pynput # conda or py3
2、程序功能介紹
這個程序是為了實現(xiàn)監(jiān)聽鍵盤操作,記錄鍵盤輸入的值,獲取
1 擊鍵行為特征:
第一個鍵釋放到第二個鍵按下的時間
第一個鍵按下到第二個鍵釋放的時間
按下一個鍵盤到釋放的時間
2 停頓特征:
停頓是兩次敲擊鍵盤是時間差超過規(guī)定的停頓閾限,根據(jù)已有的研究,這里將停頓閾限定為 2s。本文提取停頓次數(shù)、最長停頓、停頓位置等特征。
示例代碼:
# -*- coding: utf-8 -*-ahello world
import sys, os
from pynput.keyboard import Controller, Key, Listener
from pynput import keyboard
import time
# from tkinter import *
start=time.time()
end=time.time()
fun_start=0
time_interval=0
index=0
dict={'interval_times':0,'max_interval':0.,'interval_location':[]}
count=0
count_dict={'first_time':0.,'first_p_to_second_r':0.}
keyBoard_dict={'Key.enter':'\n',
'Key.space':' ',
"Key.tab":'\t'}
#比較按鍵生成的兩個txt文件,這里主要是為了實現(xiàn)當(dāng)退格鍵按下后,
#對比退格前后文本的差異,這里可以自己延伸
def com_str(path, file1, file2):
path1 = os.path.join(path, file1)
path2 = os.path.join(path, file2)
with open(path1, 'r', encoding='utf-8') as f:
file1 = f.readlines()
content1 = [str.strip().split() for str in file1]
with open(path2, 'r', encoding='utf-8') as f:
file2 = f.readlines()
content2 = [str.strip().split() for str in file2]
#print("content1:", content1)
#print("content2:", content2)
str1 = []
str2 = []
for sub_list in content1:
str1.extend(sub_list)
for sub_list in content2:
str2.extend(sub_list)
# print("the str1:", str1, "the length:", len(str1))
#print("the str2:", str2, "the length:", len(str2))
origanl_len = len(str1)
print("extend", origanl_len)
if len(str1) > len(str2):
str2.extend([' '] * (len(str1) - len(str2)))
elif len(str1) < len(str2):
str1.extend([' '] * (len(str2) - len(str1)))
index = 0
indexs = []
count = 0
for i, j in zip(str1, str2):
if i != j:
indexs.append(index)
count += 1
if index < origanl_len - 1:
print("the before...")
else:
print("the after...")
index += 1
if count == 1:
print("single...")
elif count>1:
print("the sentence...")
#得到鍵入的值
def get_key_name(key):
if isinstance(key, keyboard.KeyCode):
with open(r'C:\Users\admin\Desktop\key_record.txt','a',encoding='utf-8') as f:
f.write(key.char)
with open(r'C:\Users\admin\Desktop\key_record1.txt','a',encoding='utf-8') as f:
f.write(key.char)
return key.char
else:
if str(key) in ['Key.enter','Key.space','Key.tab']:
with open(r'C:\Users\admin\Desktop\key_record.txt', 'a',encoding='utf-8') as f:
f.write(keyBoard_dict[str(key)])
with open(r'C:\Users\admin\Desktop\key_record1.txt', 'a',encoding='utf-8') as f:
f.write(keyBoard_dict[str(key)])
if str(key) in ['Key.backspace']:
com_str(r'C:\Users\admin\Desktop','key_record.txt','key_record1.txt')
return str(key)
# 監(jiān)聽按壓
def on_press(key):
global fun_start,time_interval,index,dict,count,count_dict
fun_start = time.time()
if count == 0:
count_dict['first_time'] = fun_start
if index == 0 or index == 1:
time_interval = fun_start - start
if index == 1 and time_interval > 2.:
#停頓位置
dict["interval_location"].append(key)
#停頓次數(shù)
dict['interval_times'] += 1
#最長停頓
dict['max_interval'] = time_interval if time_interval > dict['max_interval'] else dict['max_interval']
index += 1
print("正在按壓:", get_key_name(key))
# 監(jiān)聽釋放
def on_release(key):
global start,fun_start, time_interval, index,count,count_dict
count+=1
if count==2:
#第一個鍵按下到第二個鍵釋放的時間
count_dict['first_p_to_second_r']=time.time()-count_dict['first_time']
count=0
#按下一個鍵盤到釋放的時間
print("the interval between first press and release:",
time.time() - start-time_interval)
start = time.time()
index = 1
print("已經(jīng)釋放:", get_key_name(key))
if key == Key.esc:
# 停止監(jiān)聽
return False
# 開始監(jiān)聽
def start_listen():
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
if __name__ == '__main__':
# 開始監(jiān)聽,按esc退出監(jiān)聽
start_listen()
print(dict)更多技術(shù)請關(guān)注Python視頻教程。
分享標(biāo)題:創(chuàng)新互聯(lián)Python教程:Python如何監(jiān)控鍵盤按了什么鍵
路徑分享:http://m.fisionsoft.com.cn/article/dpeodio.html


咨詢
建站咨詢
