• 苏苏12323
    了解作者
  • Python
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2020-06-18 09:28
    上传日期
使用Python语言,基于pynput模块开发的,按键记录和重播的简易按键精灵。
键盘.rar
  • 键盘.py
    10.9KB
内容介绍
import json import threading import time import tkinter from pynput import keyboard, mouse from pynput.keyboard import Controller as KeyBoardController, KeyCode from pynput.mouse import Button, Controller as MouseController # 键盘动作模板 def keyboard_action_template(): return { "name": "keyboard", "event": "default", "vk": "default" } # 鼠标动作模板 def mouse_action_template(): return { "name": "mouse", "event": "default", "target": "default", "action": "default", "location": { "x": "0", "y": "0" } } # 倒计时监听,更新UI触发自定义线程对象 class UIUpdateCutDownExecute(threading.Thread): def __init__(self, cut_down_time, custom_thread_list): super().__init__() self.cut_down_time = cut_down_time self.custom_thread_list = custom_thread_list def run(self): while self.cut_down_time > 0: for custom_thread in self.custom_thread_list: if custom_thread['obj_ui'] is not None: custom_thread['obj_ui']['text'] = str(self.cut_down_time) custom_thread['obj_ui']['state'] = 'disabled' self.cut_down_time = self.cut_down_time - 1 time.sleep(1) else: for custom_thread in self.custom_thread_list: if custom_thread['obj_ui'] is not None: custom_thread['obj_ui']['text'] = custom_thread['final_text'] custom_thread['obj_ui']['state'] = 'disabled' if custom_thread['obj_thread'] is not None: custom_thread['obj_thread'].start() time.sleep(1) # 键盘动作监听 class KeyboardActionListener(threading.Thread): def __init__(self, file_name='keyboard.action'): super().__init__() self.file_name = file_name def run(self): with open(self.file_name, 'w', encoding='utf-8') as file: # 键盘按下监听 def on_press(key): template = keyboard_action_template() template['event'] = 'press' try: template['vk'] = key.vk except AttributeError: template['vk'] = key.value.vk finally: file.writelines(json.dumps(template) + "\n") file.flush() # 键盘抬起监听 def on_release(key): if key == keyboard.Key.esc: # 停止监听 startListenerBtn['text'] = '开始录制' startListenerBtn['state'] = 'normal' keyboardListener.stop() return False template = keyboard_action_template() template['event'] = 'release' try: template['vk'] = key.vk except AttributeError: template['vk'] = key.value.vk finally: file.writelines(json.dumps(template) + "\n") file.flush() # 键盘监听 with keyboard.Listener(on_press=on_press, on_release=on_release) as keyboardListener: keyboardListener.join() # 键盘动作执行 class KeyboardActionExecute(threading.Thread): def __init__(self, file_name='keyboard.action', execute_count=0): super().__init__() self.file_name = file_name self.execute_count = execute_count def run(self): while self.execute_count > 0: with open(self.file_name, 'r', encoding='utf-8') as file: keyboard_exec = KeyBoardController() line = file.readline() while line: obj = json.loads(line) if obj['name'] == 'keyboard': if obj['event'] == 'press': keyboard_exec.press(KeyCode.from_vk(obj['vk'])) time.sleep(0.01) elif obj['event'] == 'release': keyboard_exec.release(KeyCode.from_vk(obj['vk'])) time.sleep(0.01) line = file.readline() startExecuteBtn['text'] = '开始回放' startExecuteBtn['state'] = 'normal' self.execute_count = self.execute_count - 1 # 鼠标动作监听 class MouseActionListener(threading.Thread): def __init__(self, file_name='mouse.action'): super().__init__() self.file_name = file_name def run(self): with open(self.file_name, 'w', encoding='utf-8') as file: # 鼠标移动事件 def on_move(x, y): template = mouse_action_template() template['event'] = 'move' template['location']['x'] = x template['location']['y'] = y file.writelines(json.dumps(template) + "\n") file.flush() # 鼠标点击事件 def on_click(x, y, button, pressed): template = mouse_action_template() template['event'] = 'click' template['target'] = button.name template['action'] = pressed template['location']['x'] = x template['location']['y'] = y file.writelines(json.dumps(template) + "\n") file.flush() # 鼠标滚动事件 def on_scroll(x, y, x_axis, y_axis): template = mouse_action_template() template['event'] = 'scroll' template['location']['x'] = x_axis template['location']['y'] = y_axis file.writelines(json.dumps(template) + "\n") file.flush() with mouse.Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as mouseListener: mouseListener.join() # 鼠标动作执行 class MouseActionExecute(threading.Thread): def __init__(self, file_name='mouse.action', execute_count=0): super().__init__() self.file_name = file_name self.execute_count = execute_count def run(self): while self.execute_count > 0: with open(self.file_name, 'r', encoding='utf-8') as file: mouse_exec = MouseController() line = file.readline() while line: obj = json.loads(line) if obj['name'] == 'mouse': if obj['event'] == 'move': mouse_exec.position = (obj['location']['x'], obj['location']['y']) time.sleep(0.01) elif obj['event'] == 'click': if obj['action']: if obj['target'] == 'left': mouse_exec.press(Button.left) else: mouse_exec.press(Button.right) else: if obj['target'] == 'left': mouse_exec.release(Button.left) else: mouse_exec.release(Button.right) time.sleep(0.01) elif obj['event'] == 'scroll': mouse_exec.scroll(obj['location']['x'], obj['location']['y']) time.sleep(0.01) line = file.readline() def command_adapter(action): if action == 'listener': if
评论
    相关推荐
    • C# 键盘记录源码.rar
      C# 键盘记录源码 ( 记录按键,记录时间 )。
    • delphi编写键盘记录
      // 键盘钩子退出 end; procedure TForm1.Button1Click(Sender: TObject); // 键盘钩子有效 begin memo1.Clear; if EnableHotKeyHooK=false then showmessage('安装钩子失败!'); end; procedure TForm1.Button2...
    • 键盘记录 C
      C语言实现键盘记录,本程序只供研究请勿用于 非法用途
    • java 版键盘记录
      java 版键盘记录 内附说明 请勿以此攻击别人,妥善使用,后果自负! java 版键盘记录 内附说明 请勿以此攻击别人,妥善使用,后果自负!
    • VC++键盘记录源码
      VC++键盘记录源码VC++键盘记录源码VC++键盘记录源码VC++键盘记录源码VC++键盘记录源码
    • 键盘记录
      利用PIC16F684单片机编写PS2协议,桥接与键盘和电脑之间,记录键盘的所有活动,并按需要输出记录的内容,用于学习PS2协议规范。资料内部包含全部的硬件设计资料和源代码,以及相关的记录数据读取资料。
    • C语言键盘记录程序
      c语言键盘记录源程序,可以记录键盘输入的内容,然后保存为txt文件!
    • c++ 键盘记录
      c++ 键盘记录,测试可用 ,杀软也不报病毒
    • c# 键盘记录
      该工具可以记录键盘的按键。并记录在日志中,。如果开发什么系统监控软件,很好用的
    • 键盘记录程序源码
      利用C#钩子实现的记录键盘的程序源代码