您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Python監控鍵盤按什么鍵的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Python如何監控鍵盤按了什么鍵
1、安裝pynput
pip install pynput # conda or py3
2、程序功能介紹
這個程序是為了實現監聽鍵盤操作,記錄鍵盤輸入的值,獲取
1 擊鍵行為特征:
第一個鍵釋放到第二個鍵按下的時間
第一個鍵按下到第二個鍵釋放的時間
按下一個鍵盤到釋放的時間
2 停頓特征:
停頓是兩次敲擊鍵盤是時間差超過規定的停頓閾限,根據已有的研究,這里將停頓閾限定為 2s。本文提取停頓次數、最長停頓、停頓位置等特征。
示例代碼:
# -*- 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文件,這里主要是為了實現當退格鍵按下后, #對比退格前后文本的差異,這里可以自己延伸 def com_str(path, file1, file2): path2 = os.path.join(path, file1) path3 = os.path.join(path, file2) with open(path2, 'r', encoding='utf-8') as f: file1 = f.readlines() content1 = [str.strip().split() for str in file1] with open(path3, '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) # 監聽按壓 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) #停頓次數 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)) # 監聽釋放 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("已經釋放:", get_key_name(key)) if key == Key.esc: # 停止監聽 return False # 開始監聽 def start_listen(): with Listener(on_press=on_press, on_release=on_release) as listener: listener.join() if __name__ == '__main__': # 開始監聽,按esc退出監聽 start_listen() print(dict)
關于Python監控鍵盤按什么鍵的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。