From 7d4650296ebc61909e46cac58f280ce26a3e58f0 Mon Sep 17 00:00:00 2001 From: taojy123 Date: Mon, 19 Oct 2020 15:59:05 +0800 Subject: [PATCH] upgrade to 3.1 --- Frame1.py | 24 +++++++++++++++++------- KeymouseGo.py | 50 ++++++++++++++++++++++++++++++++++++++++++-------- README.md | 10 +++++++++- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/Frame1.py b/Frame1.py index ce33f41..e713a6d 100644 --- a/Frame1.py +++ b/Frame1.py @@ -19,6 +19,9 @@ import ctypes +VERSION = '3.1' + + MOUSE_MOVE_INTERVAL_MS = 200 # 录制鼠标轨迹的精度,数值越小越精准,但同时可能产生大量的冗余 wx.NO_3D = 0 @@ -66,7 +69,7 @@ def _init_ctrls(self, prnt): wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, pos=wx.Point(506, 283), size=wx.Size(366, 201), style=wx.STAY_ON_TOP | wx.DEFAULT_FRAME_STYLE, - title='Keymouse Go') + title='KeymouseGo v%s' % VERSION) self.SetClientSize(wx.Size(350, 205)) self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self, @@ -483,12 +486,19 @@ def run_script_once(cls, script_path, thd=None): if event_type == 'EM': x, y = action - # ctypes.windll.user32.SetCursorPos(x, y) - # win32api.SetCursorPos([x, y]) + if action == [-1, -1]: + # 约定 [-1, -1] 表示鼠标保持原位置不动 + pass + else: + # 挪动鼠标 普通做法 + # ctypes.windll.user32.SetCursorPos(x, y) + # or + # win32api.SetCursorPos([x, y]) - nx = int(x * 65535 / sw) - ny = int(y * 65535 / sh) - win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_MOVE, nx, ny, 0, 0) + # 更好的兼容 win10 屏幕缩放问题 + nx = int(x * 65535 / sw) + ny = int(y * 65535 / sh) + win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_MOVE, nx, ny, 0, 0) if message == 'mouse left down': win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) @@ -555,7 +565,7 @@ def OnTaskBarLeftDClick(self, event): self.frame.Raise() def OnAbout(self, event): - wx.MessageBox('https://github.com/taojy123/KeymouseGo', 'KeymouseGo v3.0') + wx.MessageBox('https://github.com/taojy123/KeymouseGo', 'KeymouseGo v%s' % VERSION) event.Skip() def OnCloseshow(self, event): diff --git a/KeymouseGo.py b/KeymouseGo.py index da39973..3fb20b2 100644 --- a/KeymouseGo.py +++ b/KeymouseGo.py @@ -1,11 +1,17 @@ #!/usr/bin/env python #Boa:App:BoaApp -import Frame1 -import wx import time +import os import sys import json +import threading + +import wx +import pyWinhook +import pythoncom + +import Frame1 modules = {'Frame1': [1, 'Main frame of Application', u'Frame1.py']} @@ -24,13 +30,41 @@ def main(): application.MainLoop() + def single_run(script_path, run_times=1): - j = 0 - while j < run_times or run_times == 0: - j += 1 - print('===========', j, '==============') - Frame1.RunScriptClass.run_script_once(script_path) - print('script run finish!') + + t = HookThread() + t.start() + + try: + j = 0 + while j < run_times or run_times == 0: + j += 1 + print('===========', j, '==============') + Frame1.RunScriptClass.run_script_once(script_path) + print('script run finish!') + except Exception as e: + raise e + finally: + os._exit(0) + + +class HookThread(threading.Thread): + + def run(self): + + def on_keyboard_event(event): + key_name = event.Key.lower() + stop_name = 'f9' + if key_name == stop_name: + print('break exit!') + os._exit(0) + return True + + hm = pyWinhook.HookManager() + hm.KeyAll = on_keyboard_event + hm.HookKeyboard() + pythoncom.PumpMessages() if __name__ == '__main__': diff --git a/README.md b/README.md index 1850ecd..9e830b9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# KeymouseGo v3.0 +# KeymouseGo v3.1 功能:记录用户的鼠标键盘操作,通过触发按钮自动执行之前记录的操作,可设定执行的次数,可以理解为 `精简绿色版` 的 `按键精灵`。 @@ -132,6 +132,14 @@ 暂时没法打包 `x86` 版本,32 位系统的同学请自行源码编译,或 [下载v1.5老版本](https://github.com/taojy123/KeymouseGo/releases/tag/v1.5) 使用 +## v3.1 + +针对这个 issue(https://github.com/taojy123/KeymouseGo/issues/39) 增加了两个功能点 + ++ 命令行启动模式中可以随时按下 `F9` 热键,来终止脚本运行 ++ 模拟鼠标点击的脚本语句中可以设定坐标点为 `[-1, -1]`, 用以表示在鼠标当前位置直接点击 + + ## v3.0 因为兼容 macOS 遇到的很大的阻碍,最终放弃跨平台,血泪史可参看这两个 issue: