当前位置:首页 > 云计算 > 正文内容

Python如何监控键盘按了什么键

2022-05-04 03:21:51云计算1

Python如何监控键盘按了什么键

1、安装pynput

pipinstallpynput#condaorpy3

2、程序功能介绍

这个程序是为了实现监听键盘操作,记录键盘输入的值,获取

1 击键行为特征:

第一个键释放到第二个键按下的时间

第一个键按下到第二个键释放的时间

按下一个键盘到释放的时间

2 停顿特征:

停顿是两次敲击键盘是时间差超过规定的停顿阈限,根据已有的研究,这里将停顿阈限定为 2s。本文提取停顿次数、最长停顿、停顿位置等特征。

示例代码:

#-*-coding:utf-8-*-ahelloworld
importsys,os
frompynput.keyboardimportController,Key,Listener
frompynputimportkeyboard
importtime
#fromtkinterimport*

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文件,这里主要是为了实现当退格键按下后,
#对比退格前后文本的差异,这里可以自己延伸
defcom_str(path,file1,file2):
path1=os.path.join(path,file1)
path2=os.path.join(path,file2)
withopen(path1,'r',encoding='utf-8')asf:
file1=f.readlines()
content1=[str.strip().split()forstrinfile1]
withopen(path2,'r',encoding='utf-8')asf:
file2=f.readlines()
content2=[str.strip().split()forstrinfile2]
#print("content1:",content1)
#print("content2:",content2)
str1=[]
str2=[]
forsub_listincontent1:
str1.extend(sub_list)
forsub_listincontent2:
str2.extend(sub_list)
#print("thestr1:",str1,"thelength:",len(str1))
#print("thestr2:",str2,"thelength:",len(str2))
origanl_len=len(str1)
print("extend",origanl_len)
iflen(str1)>len(str2):
str2.extend(['']*(len(str1)-len(str2)))
eliflen(str1)<len(str2):
str1.extend(['']*(len(str2)-len(str1)))
index=0
indexs=[]
count=0
fori,jinzip(str1,str2):
ifi!=j:
indexs.append(index)
count+=1
ifindex<origanl_len-1:
print("thebefore...")
else:
print("theafter...")
index+=1
ifcount==1:
print("single...")
elifcount>1:
print("thesentence...")
#得到键入的值
defget_key_name(key):
ifisinstance(key,keyboard.KeyCode):
withopen(r'C:\Users\admin\Desktop\key_record.txt','a',encoding='utf-8')asf:
f.write(key.char)
withopen(r'C:\Users\admin\Desktop\key_record1.txt','a',encoding='utf-8')asf:
f.write(key.char)
returnkey.char
else:
ifstr(key)in['Key.enter','Key.space','Key.tab']:
withopen(r'C:\Users\admin\Desktop\key_record.txt','a',encoding='utf-8')asf:
f.write(keyBoard_dict[str(key)])
withopen(r'C:\Users\admin\Desktop\key_record1.txt','a',encoding='utf-8')asf:
f.write(keyBoard_dict[str(key)])
ifstr(key)in['Key.backspace']:
com_str(r'C:\Users\admin\Desktop','key_record.txt','key_record1.txt')
returnstr(key)
#监听按压
defon_press(key):
globalfun_start,time_interval,index,dict,count,count_dict
fun_start=time.time()
ifcount==0:
count_dict['first_time']=fun_start
ifindex==0orindex==1:
time_interval=fun_start-start
ifindex==1andtime_interval>2.:
#停顿位置
dict["interval_location"].append(key)
#停顿次数
dict['interval_times']+=1
#最长停顿
dict['max_interval']=time_intervaliftime_interval>dict['max_interval']elsedict['max_interval']
index+=1

print("正在按压:",get_key_name(key))

#监听释放
defon_release(key):
globalstart,fun_start,time_interval,index,count,count_dict
count+=1
ifcount==2:
#第一个键按下到第二个键释放的时间
count_dict['first_p_to_second_r']=time.time()-count_dict['first_time']
count=0
#按下一个键盘到释放的时间
print("theintervalbetweenfirstpressandrelease:",
time.time()-start-time_interval)
start=time.time()
index=1
print("已经释放:",get_key_name(key))
ifkey==Key.esc:
#停止监听
returnFalse

#开始监听
defstart_listen():
withListener(on_press=on_press,on_release=on_release)aslistener:
listener.join()

if__name__=='__main__':
#开始监听,按esc退出监听
start_listen()
print(dict)

更多技术请关注Python视频教程。

本网站文章仅供交流学习 ,不作为商用, 版权归属原作者,部分文章推送时未能及时与原作者取得联系,若来源标注错误或侵犯到您的权益烦请告知,我们将立即删除.

本文链接:https://www.xibujisuan.cn/13928.html

标签: Python