这里来展示一个比较有意思的python功能模块:pynput,这个模块里面有关于鼠标和键盘的一些操作,这里给大家分享的是监听键盘操作。

安装

pip install pynput # conda or py3

程序功能介绍

【####这个程序没有调试好,但是监听部分可以用,另外关于一些值得获取要改正下,因为这里直接让结果给打印出来了,其实还可以做一系列细致的统计####】

这个程序是为了实现监听键盘操作,记录键盘输入的值,获取
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):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 = 0indexs = []count = 0for i, j in zip(str1, str2):if i != j:indexs.append(index)count += 1if index < origanl_len - 1:print("the before...")else:print("the after...")index += 1if 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.charelse: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_dictfun_start = time.time()if count == 0:count_dict['first_time'] = fun_startif index == 0 or index == 1:time_interval = fun_start - startif 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 += 1print("正在按压:", get_key_name(key))# 监听释放
def on_release(key):global start,fun_start, time_interval, index,count,count_dictcount+=1if 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 = 1print("已经释放:", 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实现键盘监听相关推荐

  1. python全局键盘监听(pynput快捷键);利用pywin32快速截屏并生成视频

    python全局键盘监听(pynput快捷键):利用pywin32快速截屏并生成视频 第一次在CSDN写博客,有点小紧张(/ω\) 以下内容完全个人理解,有错误请指出~ 最近在用python做一个小工 ...

  2. python opencv键盘监听

    原文:https://bbs.csdn.net/topics/391993951?page=1 for file in files:a = cv2.imread(path+file)cv2.imsho ...

  3. python pynput库 自动按键 鼠标键盘 监听控制插件 可以制作按键精灵

    这是一个跨平台,使用简单的鼠标键盘监听控制库. 安装 环境 mac os + python 3.6 pip install pynput 监听,控制 import time from pynput i ...

  4. python通过鼠标键盘监听自动桌面截图调用百度文字识别工具

    垃圾代码,没有优化,重点是能直接运行,修改也方便 from pynput import mouse,keyboard from collections import deque import thre ...

  5. 一个简单的键盘监听木马dawenxi的制作

    事实上,我最初是没想到这学期的物联网安全课程会以答辩形式作为考核的,更没想到的是老师竟然让我们自己设计一个针对物联网的病毒或者针对物联网的漏洞,还要求不能被查杀.这难度,属实大?_? .可是,我不想挂 ...

  6. python-将文本复制到剪贴板与键盘监听

    文章目录 将文本复制到剪贴板 键盘监听 控制鼠标,监听和点击 控制键盘,监听和点击 将文本复制到剪贴板 python十分方便的就是导包,神器 import pyperclip # 复制到粘贴板 pyp ...

  7. java swing button和键盘监听冲突问题

    原因: 点击button会让jframe失去焦点,然后键盘监听不起作用 解决: 让jframe重新获取焦点就行了 jf.setFocusable(true); // JFrame jf = new J ...

  8. qmainwindow键盘监听

    class MyWidget(QtGui.QmainWindow):     keyPressed = QtCore.pyqtSignal() def keyPressEvent(self, even ...

  9. C#全局键盘监听(Hook)的使用(转载)

    一.为什么需要全局键盘监听? 在某些情况下应用程序需要实现快捷键执行特定功能,例如大家熟知的QQ截图功能Ctrl+Alt+A快捷键,只要QQ程序在运行(无论是拥有焦点还是处于后台运行状态),都可以按下 ...

最新文章

  1. 2、Android应用程序基本特性
  2. python3 时间、日期、时间戳的转换
  3. 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
  4. PHP中问号?和冒号: 的作用
  5. beautifulsoup解析动态页面div未展开_实战|Python轻松实现动态网页爬虫(附详细源码)...
  6. php增加html元素,使用php将appendChild($ element)添加到现有的html元素中
  7. wall 广播发送信息给所有user
  8. layui数据表格接口_layui后台管理—table 数据表格详细讲解
  9. 华为鸿蒙事件真相揭秘,鸿蒙咋来的?华为董事长揭秘
  10. 概念模型向逻辑模型的转换规则
  11. 吃着火锅唱着歌,我了解到海底捞的信息化太厉害了
  12. 人脸测温门禁 传感器_测温人脸门禁什么牌子好
  13. html点击热力图还原,网站页面点击热力图的SEO工具说明
  14. 石家庄地铁售票系统源代码
  15. 货币时间价值(学习笔记)
  16. ST-Link的红灯一直异常闪烁
  17. linux命令进入bios设置,linux系统肿么进入bios
  18. NRF52832学习笔记(2)—— 添加DFU功能(基于SDK15.3)
  19. Greenplum数据库源码学习——FTS简介
  20. xcode请求日志在哪里看_如何查看运行日志

热门文章

  1. 计算机为什么找不到摄像头,为何电脑里找不到摄像头设备?
  2. 高阶数据结构 —— 红黑树(较平衡搜索树)
  3. taro Can‘t resolve ‘./style/index.scss‘ in ‘D:\projects\dev\self\myApp\node_modules\taro-ui\dist\wea
  4. python中如何解决类互相调用问题_python中同一个类,带参的方法直接如何相互调用...
  5. 时间与太阳、月亮和地球的关系
  6. Qt使用qtwebapp编写http服务的步骤
  7. R语言和医学统计学(6):重复测量方差分析
  8. 科普:什么是图灵机?
  9. 最纯洁的科研工作者——狄拉克
  10. javascript实现图片滚动效果