Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some new features and fix bug #82

Merged
merged 7 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 53 additions & 25 deletions Frame1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import ctypes
import pyperclip
from playsound import playsound
from playsound import PlaysoundException

import config

Expand Down Expand Up @@ -58,16 +59,6 @@ def current_ts():
return int(time.time() * 1000)


def play_start_sound():
path = os.path.join(os.getcwd(), 'sounds', 'start.mp3')
playsound(path)


def play_end_sound():
path = os.path.join(os.getcwd(), 'sounds', 'end.mp3')
playsound(path)


[wxID_FRAME1, wxID_FRAME1BTRECORD, wxID_FRAME1BTRUN, wxID_FRAME1BTPAUSE, wxID_FRAME1BUTTON1,
wxID_FRAME1CHOICE_SCRIPT, wxID_FRAME1CHOICE_START, wxID_FRAME1CHOICE_STOP,
wxID_FRAME1PANEL1, wxID_FRAME1STATICTEXT1, wxID_FRAME1STATICTEXT2,
Expand All @@ -77,11 +68,15 @@ def play_end_sound():
] = [wx.NewId() for _init_ctrls in range(20)]


SW = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
SH = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)


class Frame1(wx.Frame):
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
pos=wx.Point(506, 283), size=wx.Size(366, 231),
pos=wx.Point(SW / 2 - 183, SH / 2 - 115.5), size=wx.Size(366, 231),
style=wx.STAY_ON_TOP | wx.DEFAULT_FRAME_STYLE,
title='KeymouseGo v%s' % VERSION)
self.SetClientSize(wx.Size(361, 235))
Expand Down Expand Up @@ -301,11 +296,17 @@ def on_mouse_event(event):
if not self.record:
delay = 0

print(delay, message, pos)
x, y = pos
tx = x / SW
ty = y / SH
tpos = (tx, ty)

self.record.append([delay, 'EM', message, pos])
print(delay, message, tpos)

self.record.append([delay, 'EM', message, tpos])
self.actioncount = self.actioncount + 1
text = '%d actions recorded' % self.actioncount

self.tnumrd.SetLabel(text)
return True

Expand Down Expand Up @@ -394,9 +395,8 @@ def on_keyboard_event(event):
print(delay, message, key_info)

self.record.append([delay, 'EK', message, key_info])
text = self.tnumrd.GetLabel()
action_count = text.replace(' actions recorded', '')
text = '%d actions recorded' % (int(action_count) + 1)
self.actioncount = self.actioncount + 1
text = '%d actions recorded' % self.actioncount
self.tnumrd.SetLabel(text)
return True

Expand Down Expand Up @@ -556,19 +556,18 @@ def run(self):
self.run_speed = self.frame.execute_speed.Value

self.j = 0
play_start_sound()
while self.j < self.run_times or self.run_times == 0:
self.j += 1
current_status = self.frame.tnumrd.GetLabel()
if current_status in ['broken', 'finished']:
self.frame.running = False
break
RunScriptClass.run_script_once(script_path, thd=self)
RunScriptClass.run_script_once(script_path, self.j, thd=self)

self.frame.tnumrd.SetLabel('finished')
self.frame.tstop.Shown = False
self.frame.running = False
play_end_sound()
PlayPromptTone.play_end_sound()
print('script run finish!')

except Exception as e:
Expand All @@ -582,7 +581,7 @@ def run(self):
self.frame.btrecord.Enable(True)

@classmethod
def run_script_once(cls, script_path, thd=None):
def run_script_once(cls, script_path, step, thd=None):

content = ''

Expand Down Expand Up @@ -613,9 +612,6 @@ def run_script_once(cls, script_path, thd=None):
s = json.loads(content)
steps = len(s)

sw = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
sh = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)

for i in range(steps):

print(s[i])
Expand All @@ -625,6 +621,10 @@ def run_script_once(cls, script_path, thd=None):
message = s[i][2].lower()
action = s[i][3]

if 1 == step and 0 == i:
play = PlayPromptTone(1, delay)
play.start()

time.sleep(delay / 1000.0)

if thd:
Expand All @@ -650,8 +650,8 @@ def run_script_once(cls, script_path, thd=None):
# win32api.SetCursorPos([x, y])

# 更好的兼容 win10 屏幕缩放问题
nx = int(x * 65535 / sw)
ny = int(y * 65535 / sh)
nx = int((x * SW) * 65535 / SW)
ny = int((y * SH) * 65535 / SH)
win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_MOVE, nx, ny, 0, 0)

if message == 'mouse left down':
Expand Down Expand Up @@ -745,3 +745,31 @@ def CreatePopupMenu(self):
menu.Append(self.ID_Closeshow, 'Exit')
return menu


class PlayPromptTone(threading.Thread):

def __init__(self, op, delay):
self._delay = delay
self._op = op
super().__init__()

def run(self):
if 1 == self._op:
if self._delay >= 1000:
time.sleep((self._delay - 500.0) / 1000.0)
self._play_start_sound()

def _play_start_sound(self):
try:
path = os.path.join(os.getcwd(), 'sounds', 'start.mp3')
playsound(path)
except PlaysoundException as e:
print(e)

@classmethod
def play_end_sound(cls):
try:
path = os.path.join(os.getcwd(), 'sounds', 'end.mp3')
playsound(path)
except PlaysoundException as e:
print(e)
2 changes: 1 addition & 1 deletion KeymouseGo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def single_run(script_path, run_times=1):
while j < run_times or run_times == 0:
j += 1
print('===========', j, '==============')
Frame1.RunScriptClass.run_script_once(script_path)
Frame1.RunScriptClass.run_script_once(script_path, j)
print('script run finish!')
except Exception as e:
raise e
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@


# 脚本语法说明:
> 演示屏幕分辨率为`1920 * 1080`

```
[
[3000, "EM", "mouse right down", [100, 200]], // 开始运行 `3000ms` 后,在屏幕坐标 `(100,200)` 处 `按下鼠标右键`;
[50, "EM", "mouse right up", [100, 200]], // 等待 `50ms` 后在相同位置 `抬起鼠标右键`;
[1000, "EK", "key down", (70, 'F', 0)], // 等待 `1000ms` 后 `按下f键`;
[50, "EK", "key up", (70, 'F', 0)], // 等待 `50ms` 后 `抬起f键`;
[100, "EM", "mouse left down", [500, 500]], // 等待 `100ms` 后,在屏幕坐标 `(500, 500)` 处 `按下鼠标左键`;
[100, "EM", "mouse move", [500, 600]], // 等待 `100ms` 后,鼠标移动至 `(500, 600)` 位置;
[100, "EM", "mouse left down", [600, 600]], // 等待 `100ms` 后,在屏幕坐标 `(600, 600)` 处 `抬起鼠标左键`;
[100, "EX", "input", "你好 world"], // 等待 `100ms` 后,在当前位置输入 `你好 world` 文字。
[3000, "EM", "mouse right down", [0.052083333333333336, 0.18518518518518517]], // 开始运行 `3000ms` 后,在屏幕相对坐标 `(0.052083333333333336, 0.18518518518518517)`即 `(100,200)` 处 `按下鼠标右键`;
[50, "EM", "mouse right up", [0.052083333333333336, 0.18518518518518517]], // 等待 `50ms` 后在相同位置 `抬起鼠标右键`;
[1000, "EK", "key down", (70, 'F', 0)], // 等待 `1000ms` 后 `按下f键`;
[50, "EK", "key up", (70, 'F', 0)], // 等待 `50ms` 后 `抬起f键`;
[100, "EM", "mouse left down", [0.2604166666666667, 0.46296296296296297]], // 等待 `100ms` 后,在屏幕相对坐标 `(0.2604166666666667, 0.46296296296296297)`即 `(500, 500)` 处 `按下鼠标左键`;
[100, "EM", "mouse move", [0.2604166666666667, 0.5555555555555556]], // 等待 `100ms` 后,鼠标移动至相对坐标 `(0.2604166666666667, 0.5555555555555556)`即 `(500, 600)` 位置;
[100, "EM", "mouse left down", [0.3125, 0.5555555555555556]], // 等待 `100ms` 后,在屏幕相对坐标 `(0.3125, 0.5555555555555556)`即 `(600, 600)` 处 `抬起鼠标左键`;
[100, "EX", "input", "你好 world"], // 等待 `100ms` 后,在当前位置输入 `你好 world` 文字。
]
```

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ wxPython>=3,<5
pywin32==301
pyWinhook==1.6.2
pyperclip==1.8.0
playsound==1.3.0
playsound==1.2.2