Skip to content

Commit

Permalink
add feature input words
Browse files Browse the repository at this point in the history
  • Loading branch information
taojy123 committed Sep 7, 2020
1 parent 3a5c379 commit 91ca24a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
36 changes: 27 additions & 9 deletions Frame1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
import traceback
import io

import pyperclip
import wx
from wx.adv import TaskBarIcon as wxTaskBarIcon
from wx.adv import EVT_TASKBAR_LEFT_DCLICK

import pyWinhook
import win32ui,win32con,pythoncom,win32gui,win32process,win32api
import win32con
import win32api
import ctypes

from pynput import mouse
from pynput import keyboard
from pynput.mouse import Button
from pynput.keyboard import Key, KeyCode

MOUSE_MOVE_INTERVAL_MS = 200 # 录制鼠标轨迹的精度,数值越小越精准,但同时可能产生大量的冗余

wx.NO_3D = 0
HOT_KEYS = ['F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12']
Expand Down Expand Up @@ -219,13 +218,17 @@ def on_mouse_event(event):
return True

message = event.MessageName
all_messages = ('mouse left down', 'mouse left up', 'mouse right down', 'mouse right up')
all_messages = ('mouse left down', 'mouse left up', 'mouse right down', 'mouse right up', 'mouse move')
if message not in all_messages:
return True

pos = win32gui.GetCursorPos()
pos = win32api.GetCursorPos()

delay = current_ts() - self.ttt

if message == 'mouse move' and delay < MOUSE_MOVE_INTERVAL_MS:
return True

self.ttt = current_ts()
if not self.record:
delay = 0
Expand Down Expand Up @@ -453,7 +456,7 @@ def run(self):
@classmethod
def run_script_once(cls, script_path, thd=None):
s = open(script_path, 'r').read()
s = json.loads(s)
s = json.loads(s.replace('],\n]', ']\n]'))
steps = len(s)

sw = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
Expand All @@ -464,7 +467,7 @@ def run_script_once(cls, script_path, thd=None):
print(s[i])

delay = s[i][0]
event_type = s[i][1]
event_type = s[i][1].upper()
message = s[i][2].lower()
action = s[i][3]

Expand Down Expand Up @@ -496,6 +499,8 @@ def run_script_once(cls, script_path, thd=None):
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
elif message == 'mouse right up':
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
elif message == 'mouse move':
pass
else:
print('unknow mouse event:', message)

Expand All @@ -517,6 +522,19 @@ def run_script_once(cls, script_path, thd=None):
else:
print('unknow keyboard event:', message)

elif event_type == 'EX':

if message == 'input':
text = action
pyperclip.copy(text)
# Ctrl+V
win32api.keybd_event(162, 0, 0, 0) # ctrl
win32api.keybd_event(86, 0, 0, 0) # v
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(162, 0, win32con.KEYEVENTF_KEYUP, 0)
else:
print('unknow extra event:', message)


class TaskBarIcon(wxTaskBarIcon):
ID_About = wx.NewId()
Expand Down
52 changes: 31 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,40 @@

```
[
[3000, "EM", "mouse left down", [100, 200]],
[50, "EM", "mouse left up", [100, 200]],
[1000, "EK", "key down", (70, 'F', 0)],
[50, "EK", "key up", (70, 'F', 0)],
[2000, "EM", "mouse right down", [300, 400]],
[50, "EM", "mouse right up", [300, 400]]
[3000, "EM", "mouse right down", [100, 200]],
[50, "EM", "mouse right up", [100, 200]],
[1000, "EK", "key down", (70, 'F', 0)],
[50, "EK", "key up", (70, 'F', 0)],
[100, "EM", "mouse left down", [500, 500]],
[100, "EM", "mouse move", [500, 600]],
[100, "EM", "mouse left down", [600, 600]],
[100, "EX", "input", "你好 world"],
]
```

每一行代表一次动作:
+ 每行的第 1 个元素表示时间间隔,指的是本次动作与上一次动作之间相隔的时间,单位为毫秒。
+ 每行的第 2 个元素表示鼠标动作或是键盘动作:`EM` 为鼠标,`EK` 为键盘。
+ 每行的第 3 个元素表示动作的类型:`mouse left down` 为鼠标左键按下,`mouse left up` 为鼠标左键抬起,`mouse right down` 为鼠标右键按下,`mouse right up` 为鼠标右键抬起,`key down` 键盘按键按下,`key up` 键盘按键抬起。
+ 每行的第 4 个元素表示具体的动作参数,当为鼠标动作时,由两个子元素构成,分别为鼠标所在的屏幕位置的横纵坐标;
+ 当为键盘动作时,由三个子元素构成,分别是(按键编号, 按键名, 拓展标记)。
+ 每行的第 2 个元素表示鼠标动作或是键盘动作:`EM` 为鼠标,`EK` 为键盘,`EX` 为其他拓展动作。
+ 每行的第 3 个元素表示动作的类型:
+ `mouse left down` 为鼠标左键按下,`mouse left up` 为鼠标左键抬起,
+ `mouse right down` 为鼠标右键按下,`mouse right up` 为鼠标右键抬起,
+ `key down` 为键盘按键按下,`key up` 为键盘按键抬起,
+ `mouse move` 为鼠标滑过,`input` 输入文字。
+ 每行的第 4 个元素表示具体的动作参数
+ 当为鼠标动作时,由两个子元素构成,分别为鼠标所在的屏幕位置的横纵坐标,
+ 当为键盘动作时,由三个子元素构成,分别是(按键编号, 按键名, 拓展标记),
+ 当为输入文字动作时,为要输入的文字内容。
+ 修改时请严格遵守格式,否则可能导致脚本无法运行,建议修改前先备份一下。

综上所述,示例中的脚本运行后的效果为:
+ 开始运行 `3000ms` 后,在屏幕坐标 `(100,200)``按下鼠标左键`
+ 等待 `50ms` 后在相同位置 `抬起鼠标左键`
+ 等待 `1000ms``按下f键`
+ 等待 `50ms``抬起f键`
+ 等待 `2000ms` 后,在屏幕坐标 `(300,400)``按下鼠标左键`
+ 等待 `50ms` 后在相同位置 `抬起鼠标左键`
1. 开始运行 `3000ms` 后,在屏幕坐标 `(100,200)``按下鼠标右键`
2. 等待 `50ms` 后在相同位置 `抬起鼠标右键`
3. 等待 `1000ms``按下f键`
4. 等待 `50ms``抬起f键`
5. 等待 `100ms` 后,在屏幕坐标 `(500, 500)``按下鼠标左键`
6. 等待 `100ms` 后,鼠标移动至 `(500, 600)` 位置;
7. 等待 `100ms` 后,在屏幕坐标 `(600, 600)``抬起鼠标左键`
8. 等待 `100ms` 后,在当前位置输入 `你好 world` 文字。


# 使用命令行运行:
Expand Down Expand Up @@ -128,12 +138,12 @@
https://github.com/taojy123/KeymouseGo/issues/24
https://github.com/moses-palmer/pynput/issues/55

+ 改回使用 `win32api` 库,只支持 windows 系统
+ 因为使用了 `win32api`,屏幕缩放比例不再需要手动设置
+ 录制脚本语法有部分改动,不向前兼容
+ 改回使用 `win32api` 来模拟事件,只支持 windows 系统
+ 解决了 `shift` + `上下左右` 的回放问题,见 https://github.com/taojy123/KeymouseGo/issues/27
+ 录制鼠标路径 todo
+ 文字输入功能 todo
+ 增加了录制鼠标路径功能,需求来源 https://github.com/taojy123/KeymouseGo/issues/33
+ 增加了文字输入功能,需求来源 https://github.com/taojy123/KeymouseGo/issues/34
+ 因为使用了 `win32api`,不需要再手动设置屏幕缩放比例了
+ 录制脚本语法有部分改动,不向前兼容


## v2.2
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
wxPython>=3,<5
pywin32==228
pyWinhook==1.6.2
pyperclip==1.8.0

0 comments on commit 91ca24a

Please sign in to comment.