python记录鼠标键盘操作自动执行重复工作

3 篇文章 0 订阅
2 篇文章 0 订阅

系列文章目录


前言

github项目原地址

这个仓库仅是用来学习的, 项目不是个人写的!!!!!!!

1、记录用户的鼠标键盘操作,通过触发按钮自动执行之前记录的操作,可设定执行的次数。
2、在进行某些操作简单、单调重复的工作时,使用本软件就可以很省力了。自己只要做一遍,然后接下来就让电脑来做。

一、执行方法

执行文件如下:
在这里插入图片描述

python .\KeymouseGo.py 

在这里插入图片描述

二、python录制

1.Frame1.py

代码如下(示例):

#Boa:Frame:Frame1

import os
import sys
import wx
import time
import threading
import datetime
import json
import traceback

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

PY2 = PY3 = False
try:
    import StringIO
    from wx import TaskBarIcon as wxTaskBarIcon
    from wx import EVT_TASKBAR_LEFT_DCLICK
    PY2 = True
except:
    import io
    from wx.adv import TaskBarIcon as wxTaskBarIcon
    from wx.adv import EVT_TASKBAR_LEFT_DCLICK
    PY3 = True


# # anther way to resolve DPI Scaling on win10
# try:
#     import ctypes
#     ctypes.c_int()
#     ctypes.windll.shcore.SetProcessDpiAwareness(2)
# except Exception as e:
#     print(e)


wx.NO_3D = 0


KEYS = ['F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12']


def GetMondrianStream():
    if PY2:
        data = '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x06\x00\x00\x00szz\xf4\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00qIDATX\x85\xed\xd6;\n\x800\x10E\xd1{\xc5\x8d\xb9r\x97\x16\x0b\xad$\x8a\x82:\x16o\xda\x84pB2\x1f\x81Fa\x8c\x9c\x08\x04Z{\xcf\xa72\xbcv\xfa\xc5\x08 \x80r\x80\xfc\xa2\x0e\x1c\xe4\xba\xfaX\x1d\xd0\xde]S\x07\x02\xd8>\xe1wa-`\x9fQ\xe9\x86\x01\x04\x10\x00\\(Dk\x1b-\x04\xdc\x1d\x07\x14\x98;\x0bS\x7f\x7f\xf9\x13\x04\x10@\xf9X\xbe\x00\xc9 \x14K\xc1<={\x00\x00\x00\x00IEND\xaeB`\x82'
        stream = StringIO.StringIO(data)
    else:
        data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x06\x00\x00\x00szz\xf4\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00qIDATX\x85\xed\xd6;\n\x800\x10E\xd1{\xc5\x8d\xb9r\x97\x16\x0b\xad$\x8a\x82:\x16o\xda\x84pB2\x1f\x81Fa\x8c\x9c\x08\x04Z{\xcf\xa72\xbcv\xfa\xc5\x08 \x80r\x80\xfc\xa2\x0e\x1c\xe4\xba\xfaX\x1d\xd0\xde]S\x07\x02\xd8>\xe1wa-`\x9fQ\xe9\x86\x01\x04\x10\x00\\(Dk\x1b-\x04\xdc\x1d\x07\x14\x98;\x0bS\x7f\x7f\xf9\x13\x04\x10@\xf9X\xbe\x00\xc9 \x14K\xc1<={\x00\x00\x00\x00IEND\xaeB`\x82'
        stream = io.BytesIO(data)
    return stream


def GetMondrianBitmap():
    stream = GetMondrianStream()
    image = wx.ImageFromStream(stream)
    return wx.BitmapFromImage(image)


def GetMondrianIcon():
    icon = wx.EmptyIcon()
    icon.CopyFromBitmap(GetMondrianBitmap())
    return icon


def create(parent):
    return Frame1(parent)


[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, 
 wxID_FRAME1STATICTEXT3, wxID_FRAME1STATICTEXT4, wxID_FRAME1STIMES, 
 wxID_FRAME1TEXTCTRL1, wxID_FRAME1TEXTCTRL2, wxID_FRAME1TNUMRD, 
 wxID_FRAME1TSTOP, wxID_FRAME1STATICTEXT5, wxID_FRAME1TEXTCTRL3,
] = [wx.NewId() for _init_ctrls in range(20)]


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, 201),
              style=wx.STAY_ON_TOP | wx.DEFAULT_FRAME_STYLE,
              title='Keymouse Go')
        self.SetClientSize(wx.Size(350, 205))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(350, 205),
              style=wx.NO_3D | wx.CAPTION)

        self.btrecord = wx.Button(id=wxID_FRAME1BTRECORD, label=u'\u5f55\u5236',
              name='btrecord', parent=self.panel1, pos=wx.Point(202, 12),
              size=wx.Size(56, 32), style=0)
        self.btrecord.Bind(wx.EVT_BUTTON, self.OnBtrecordButton,
              id=wxID_FRAME1BTRECORD)

        self.btrun = wx.Button(id=wxID_FRAME1BTRUN, label=u'\u542f\u52a8',
              name='btrun', parent=self.panel1, pos=wx.Point(274, 12),
              size=wx.Size(56, 32), style=0)
        self.btrun.Bind(wx.EVT_BUTTON, self.OnBtrunButton, id=wxID_FRAME1BTRUN)

        # 暂停/继续 功能不适合用按钮的形式来做,所以暂时隐去
        # self.btpause = wx.Button(id=wxID_FRAME1BTPAUSE, label=u'\u6682\u505c',
        #       name='btpause', parent=self.panel1, pos=wx.Point(274, 141),
        #       size=wx.Size(56, 32), style=0)
        # self.btpause.Bind(wx.EVT_BUTTON, self.OnBtpauseButton, id=wxID_FRAME1BTPAUSE)

        self.tnumrd = wx.StaticText(id=wxID_FRAME1TNUMRD, label=u'ready..',
              name='tnumrd', parent=self.panel1, pos=wx.Point(17, 175),
              size=wx.Size(100, 36), style=0)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label=u'test',
              name='button1', parent=self.panel1, pos=wx.Point(128, 296),
              size=wx.Size(75, 24), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

        self.tstop = wx.StaticText(id=wxID_FRAME1TSTOP,
              label=u'If you want to stop it, Press F12', name='tstop',
              parent=self.panel1, pos=wx.Point(25, 332), size=wx.Size(183, 18),
              style=0)
        self.tstop.Show(False)

        self.stimes = wx.SpinCtrl(id=wxID_FRAME1STIMES, initial=0, max=1000,
              min=0, name='stimes', parent=self.panel1, pos=wx.Point(206, 101),
              size=wx.Size(45, 18), style=wx.SP_ARROW_KEYS)
        self.stimes.SetValue(1)

        self.label_run_times = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
              label=u'\u6267\u884c\u6b21\u6570(0\u4e3a\u65e0\u9650\u5faa\u73af)',
              name='label_run_times', parent=self.panel1, pos=wx.Point(203, 61),
              size=wx.Size(136, 26), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self.panel1, pos=wx.Point(24, 296), size=wx.Size(40, 22),
              style=0, value='119')

        self.textCtrl2 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2, name='textCtrl2',
              parent=self.panel1, pos=wx.Point(80, 296), size=wx.Size(36, 22),
              style=0, value='123')

        self.label_script = wx.StaticText(id=wxID_FRAME1STATICTEXT3,
              label=u'\u811a\u672c', name='label_script', parent=self.panel1,
              pos=wx.Point(17, 20), size=wx.Size(40, 32), style=0)

        self.choice_script = wx.Choice(choices=[], id=wxID_FRAME1CHOICE_SCRIPT,
              name=u'choice_script', parent=self.panel1, pos=wx.Point(79, 15),
              size=wx.Size(108, 25), style=0)

        self.label_start_key = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label=u'\u542f\u52a8\u70ed\u952e', name='label_start_key',
              parent=self.panel1, pos=wx.Point(16, 63), size=wx.Size(56, 24),
              style=0)

        self.label_stop_key = wx.StaticText(id=wxID_FRAME1STATICTEXT4,
              label=u'\u7ec8\u6b62\u70ed\u952e', name='label_stop_key',
              parent=self.panel1, pos=wx.Point(16, 102), size=wx.Size(56, 32),
              style=0)

        self.choice_start = wx.Choice(choices=[], id=wxID_FRAME1CHOICE_START,
              name=u'choice_start', parent=self.panel1, pos=wx.Point(79, 58),
              size=wx.Size(108, 25), style=0)
        self.choice_start.SetLabel(u'')
        self.choice_start.SetLabelText(u'')
        self.choice_start.Bind(wx.EVT_CHOICE, self.OnChoice_startChoice,
              id=wxID_FRAME1CHOICE_START)

        self.choice_stop = wx.Choice(choices=[], id=wxID_FRAME1CHOICE_STOP,
              name=u'choice_stop', parent=self.panel1, pos=wx.Point(79, 98),
              size=wx.Size(108, 25), style=0)
        self.choice_stop.Bind(wx.EVT_CHOICE, self.OnChoice_stopChoice,
              id=wxID_FRAME1CHOICE_STOP)

        # ===== if use SetProcessDpiAwareness, comment below =====
        self.label_scale = wx.StaticText(id=wxID_FRAME1STATICTEXT5,
              label=u'\u5c4f\u5e55\u7f29\u653e', name='staticText5',
              parent=self.panel1, pos=wx.Point(16, 141), size=wx.Size(56, 32),
              style=0)
        self.text_scale = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL3, name='textCtrl3',
              parent=self.panel1, pos=wx.Point(79, 138), size=wx.Size(108, 22),
              style=0, value='100%')
        # =========================================================

    def __init__(self, parent):

        self._init_ctrls(parent)
        
        self.SetIcon(GetMondrianIcon())
        self.taskBarIcon = TaskBarIcon(self) 
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Bind(wx.EVT_ICONIZE, self.OnIconfiy)
        
        if not os.path.exists('scripts'):
            os.mkdir('scripts')
        self.scripts = os.listdir('scripts')[::-1]
        
        self.choice_script.SetItems(self.scripts)
        self.scripts = list(filter(lambda s: s.endswith('.txt'), self.scripts))
        if self.scripts:
            self.choice_script.SetSelection(0)
                
        self.choice_start.SetItems(KEYS)
        self.choice_start.SetSelection(3)
        
        self.choice_stop.SetItems(KEYS)
        self.choice_stop.SetSelection(6)

        self.running = False
        self.recording = False
        self.record = []
        self.ttt = self.now_ts

        # for pause-resume feature
        self.paused = False
        self.pause_event = threading.Event()

        # =========== create mouse listener for record ===========
        def on_move(x, y):
            if not self.recording or self.running:
                return True

        def on_scroll(x, y, dx, dy):
            if not self.recording or self.running:
                return True

        def on_click(x, y, button, pressed):

            if not self.recording or self.running:
                return True

            # ===== if use SetProcessDpiAwareness, comment below =====
            try:
                scale = self.text_scale.GetValue()
                scale = scale.replace('%', '').replace('-', '').strip()
                scale = float(scale)
                scale = scale / 100.0
            except:
                scale = 1
            x = int(x / scale)
            y = int(y / scale)
            # =========================================================

            print('mouse click:', x, y, button.name, pressed)

            delay = self.now_ts - self.ttt
            self.ttt = self.now_ts
            if not self.record:
                delay = 0

            pos = (x, y)
            if button.name == 'left':
                message = 'mouse left '
            elif button.name == 'right':
                message = 'mouse right '
            else:
                return True
            if pressed:
                message += 'down'
            else:
                message += 'up'
            self.record.append([delay, 'EM', message, pos])
            text = self.tnumrd.GetLabel()
            text = text.replace(' actions recorded','')
            text = str(eval(text)+1)
            text = text + ' actions recorded'
            self.tnumrd.SetLabel(text)
            return True

        # =========== create keyboard listener for record ===========
        def key_event(key, is_press):
            if not self.recording or self.running:
                return True

            if is_press:
                print('keyboard press:', key)
                message = 'key down'
            else:
                print('keyboard release:', key)
                message = 'key up'

            if isinstance(key, Key):
                print('Key:', key.name, key.value.vk)
                name = key.name
            elif isinstance(key, KeyCode):
                print('KeyCode:', key.char, key.vk)
                name = key.char
            else:
                assert False


            delay = self.now_ts - self.ttt
            self.ttt = self.now_ts
            if not self.record:
                delay = 0

            self.record.append([delay, 'EK', message, name])

            text = self.tnumrd.GetLabel()
            text = text.replace(' actions recorded', '')
            text = str(eval(text) + 1)
            text = text + ' actions recorded'
            self.tnumrd.SetLabel(text)
            return True

        def on_press(key):
            return key_event(key, True)

        def on_release(key):
            print('=====',key)
            if not self.recording:
                # listen for start/stop script
                start_name = 'f6'
                stop_name = 'f9'
                start_index = self.choice_start.GetSelection()
                start_name = KEYS[start_index].lower()
                stop_index = self.choice_stop.GetSelection()
                stop_name = KEYS[stop_index].lower()

                print(start_name, stop_name, key)

                if not isinstance(key, Key):
                    return True

                if key.name == start_name and not self.running:
                    print('script start')
                    t = RunScriptClass(self, self.pause_event)
                    t.start()
                elif key.name == stop_name and self.running:
                    print('script stop')
                    self.tnumrd.SetLabel('broken')
            return key_event(key, False)

        self.mouse_listener = mouse.Listener(
            on_move=on_move,
            on_scroll=on_scroll,
            on_click=on_click
        )
        self.keyboard_listener = keyboard.Listener(
            on_press=on_press,
            on_release=on_release)
        
        self.mouse_listener.start()
        self.keyboard_listener.start()

    @property
    def now_ts(self):
        return int(time.time() * 1000)

    def get_script_path(self):
        i = self.choice_script.GetSelection()
        if i < 0:
            return ''
        script = self.scripts[i]
        path = os.path.join(os.getcwd(), 'scripts', script)
        print(path)
        return path
        
    def new_script_path(self):
        now = datetime.datetime.now()
        script = '%s.txt' % now.strftime('%m%d_%H%M')
        if script in self.scripts:
            script = '%s.txt' % now.strftime('%m%d_%H%M%S')
        self.scripts.insert(0, script)
        self.choice_script.SetItems(self.scripts)
        self.choice_script.SetSelection(0)
        return self.get_script_path()

    def OnHide(self, event):
        self.Hide()
        event.Skip()
        
    def OnIconfiy(self, event):
        self.Hide()
        event.Skip()
        
    def OnClose(self, event):
        try:
            self.mouse_listener.stop()
            self.keyboard_listener.stop()
        except:
            pass
        self.taskBarIcon.Destroy()
        self.Destroy()
        event.Skip()

    def OnButton1Button(self, event):
        event.Skip()

    def OnBtrecordButton(self, event):

        if self.recording:
            print('record stop')
            self.recording = False
            del self.record[-2]
            del self.record[-1]
            output = json.dumps(self.record, indent=1)
            output = output.replace('\r\n', '\n').replace('\r', '\n')
            output = output.replace('\n   ', '').replace('\n  ', '')
            output = output.replace('\n ]', ']')
            open(self.new_script_path(), 'w').write(output)
            self.btrecord.SetLabel(u'\u5f55\u5236')
            self.tnumrd.SetLabel('finished')
            self.record = []
        else:
            print('record start')
            self.recording = True
            self.ttt = self.now_ts
            status = self.tnumrd.GetLabel()
            if 'running' in status or 'recorded' in status:
                return
            self.btrecord.SetLabel(u'\u7ed3\u675f') # 结束
            self.tnumrd.SetLabel('0 actions recorded')
            self.choice_script.SetSelection(-1)
            self.record = []
        
        event.Skip()

    def OnBtrunButton(self, event):
        print('script start by btn')
        t = RunScriptClass(self, self.pause_event)
        t.start()
        event.Skip()

    def OnBtpauseButton(self, event):
        print('script pause button pressed')
        if self.paused:
            print('script is resumed')
            self.pause_event.set()
            self.paused = False
            self.btpause.SetLabel(u'\u6682\u505c') # 暂停
        else:
            print('script is paused')
            self.pause_event.clear()
            self.paused = True
            self.btpause.SetLabel(u'\u7ee7\u7eed') # 继续
        event.Skip()

    def OnChoice_startChoice(self, event):
        event.Skip()

    def OnChoice_stopChoice(self, event):
        event.Skip()


class RunScriptClass(threading.Thread):

    def __init__(self, frame: Frame1, event: threading.Event):
        self.frame = frame
        self.event = event
        self.event.set()
        super(RunScriptClass, self).__init__()

    def run(self):

        status = self.frame.tnumrd.GetLabel()
        if self.frame.running or self.frame.recording:
            return

        if 'running' in status or 'recorded' in status:
            return

        script_path = self.frame.get_script_path()
        if not script_path:
            self.frame.tnumrd.SetLabel('script not found, please self.record first!')
            return

        self.frame.running = True

        try:
            s = open(script_path, 'r').read()
            s = json.loads(s)
            steps = len(s)
            run_times = self.frame.stimes.Value

            running_text = '%s running..' % script_path.split('/')[-1].split('\\')[-1]
            self.frame.tnumrd.SetLabel(running_text)
            self.frame.tstop.Shown = True

            mouse_ctl = mouse.Controller()
            keyboard_ctl = keyboard.Controller()

            j = 0
            while j < run_times or run_times == 0:
                j += 1

                if self.frame.tnumrd.GetLabel() == 'broken' or self.frame.tnumrd.GetLabel() == 'finished':
                    self.frame.running = False
                    break
                
                for i in range(steps):
                    self.event.wait()
                    print(s[i])

                    # for old style script
                    if isinstance(s[i][0], str) and isinstance(s[i][3], int):
                        s[i].insert(0, s[i][3])

                    delay = s[i][0]
                    event_type = s[i][1]
                    message = s[i][2]
                    action = s[i][3]

                    message = message.lower()
                    
                    time.sleep(delay / 1000.0)
                    
                    if self.frame.tnumrd.GetLabel() == 'broken' or self.frame.tnumrd.GetLabel() == 'finished':
                        break
                            
                    text = '%s  [%d/%d %d/%d]' % (running_text, i+1, steps, j, run_times)
                    self.frame.tnumrd.SetLabel(text)

                    if event_type == 'EM':
                        x, y = action
                        mouse_ctl.position = (x, y)
                        if message == 'mouse left down':
                            mouse_ctl.press(Button.left)
                        elif message == 'mouse left up':
                            mouse_ctl.release(Button.left)
                        elif message == 'mouse right down':
                            mouse_ctl.press(Button.right)
                        elif message == 'mouse right up':
                            mouse_ctl.release(Button.right)
                        else:
                            print('unknow mouse event:', message)

                    elif event_type == 'EK':
                        key_name = action

                        if len(key_name) == 1:
                            key = key_name
                        else:
                            key = getattr(Key, key_name)

                        if message == 'key down':
                            keyboard_ctl.press(key)
                        elif message == 'key up':
                            keyboard_ctl.release(key)
                        else:
                            print('unknow keyboard event:', message)

            self.frame.tnumrd.SetLabel('finished')
            self.frame.tstop.Shown = False
            self.frame.running = False
            print('script run finish!')
            
        except Exception as e:
            print('run error', e)
            traceback.print_exc()
            self.frame.tnumrd.SetLabel('failed')
            self.frame.tstop.Shown = False
            self.frame.running = False


class TaskBarIcon(wxTaskBarIcon):
    ID_About = wx.NewId()
    ID_Closeshow = wx.NewId()

    def __init__(self, frame):
        wxTaskBarIcon.__init__(self)
        self.frame = frame
        self.SetIcon(GetMondrianIcon())
        self.Bind(EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick)
        self.Bind(wx.EVT_MENU, self.OnAbout, id=self.ID_About)
        self.Bind(wx.EVT_MENU, self.OnCloseshow, id=self.ID_Closeshow)

    def OnTaskBarLeftDClick(self, event):
        if self.frame.IsIconized():
            self.frame.Iconize(False)
        if not self.frame.IsShown():
            self.frame.Show(True)
        self.frame.Raise()

    def OnAbout(self, event):
        wx.MessageBox('https://github.com/taojy123/KeymouseGo', 'KeymouseGo')
        event.Skip()

    def OnCloseshow(self, event):
        self.frame.Close(True)
        event.Skip()

    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(self.ID_About, 'About')
        menu.Append(self.ID_Closeshow, 'Exit')
        return menu


2.KeymouseGo.py

代码如下(示例):

#!/usr/bin/env python
#Boa:App:BoaApp

import Frame1
import wx
import time
import sys
import json

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


modules = {'Frame1': [1, 'Main frame of Application', u'Frame1.py']}


class BoaApp(wx.App):
    def OnInit(self):
        self.main = Frame1.create(None)
        self.main.Show()
        self.SetTopWindow(self.main)
        return True


def main():
    application = BoaApp(0)
    application.MainLoop()


def single_run(script_path, run_times=1):

    # python KeymouseGo.py scripts/0416_2342.txt 10
    # KeymouseGo.exe scripts\0416_2342.txt

    s = open(script_path, 'r').read()
    s = json.loads(s)
    steps = len(s)

    mouse_ctl = mouse.Controller()
    keyboard_ctl = keyboard.Controller()

    j = 0
    while j < run_times or run_times == 0:
        j += 1

        # Keep the same with Frame1.py:455, and remove code include `self`
        for i in range(steps):

            print(s[i])

            # for old style script
            if isinstance(s[i][0], str) and isinstance(s[i][3], int):
                s[i].insert(0, s[i][3])

            delay = s[i][0]
            event_type = s[i][1]
            message = s[i][2]
            action = s[i][3]

            message = message.lower()
            
            time.sleep(delay / 1000.0)

            if event_type == 'EM':
                x, y = action
                mouse_ctl.position = (x, y)
                if message == 'mouse left down':
                    mouse_ctl.press(Button.left)
                elif message == 'mouse left up':
                    mouse_ctl.release(Button.left)
                elif message == 'mouse right down':
                    mouse_ctl.press(Button.right)
                elif message == 'mouse right up':
                    mouse_ctl.release(Button.right)
                else:
                    print('unknow mouse event:', message)

            elif event_type == 'EK':
                key_name = action

                if len(key_name) == 1:
                    key = key_name
                else:
                    key = getattr(Key, key_name)

                if message == 'key down':
                    keyboard_ctl.press(key)
                elif message == 'key up':
                    keyboard_ctl.release(key)
                else:
                    print('unknow keyboard event:', message)

    print('script run finish!')


if __name__ == '__main__':

    print(sys.argv)

    if len(sys.argv) > 1:
        script_path = sys.argv[1]
        run_times = int(sys.argv[2]) if len(sys.argv) > 2 else 1
        single_run(script_path, run_times)
    else:
        main()


三、说明书

KeymouseGo v2.2

功能:记录用户的鼠标键盘操作,通过触发按钮自动执行之前记录的操作,可设定执行的次数,可以理解为 精简绿色版按键精灵

用途:在进行某些操作简单、单调重复的工作时,使用本软件就可以很省力了。自己只要做一遍,然后接下来就让电脑来做。

基本操作:

1、点击 录制 按钮,开始录制。

2、在计算机上进行任意操作,如点击鼠标、键盘输入,这部分的动作会被记录下来。

3、点击 结束 按钮,结束录制。

4、点击 启动 按钮,计算机会重复执行一遍第2步中所录制的动作。

提示:

1、可设置脚本重复执行的次数,如果为 0 即为无限循环。

2、默认启动热键为 F6,功能等同于 启动 按钮;默认终止热键为 F9,按下后将会停止正在运行的脚本。

3、录制时只记录鼠标点击动作和键盘动作,不记录鼠标移动轨迹。

4、每次录制结束后都会在 scripts 目前下生成一个新的脚本文件。

5、运行前可以在列表中选择一个需要执行的脚本。

6、scripts 下的脚本文件内容可以修改,修改时可参考如下所述 脚本格式说明

脚本格式说明:

[
 [3000, "EM", "mouse left down", [100, 200]], 
 [50,   "EM", "mouse left up", [100, 200]], 
 [1000, "EK", "key down", "f"], 
 [50,   "EK", "key up", "f"], 
 [2000, "EM", "mouse right down", [300, 400]], 
 [50,   "EM", "mouse right up", [300, 400]]
]

每一行代表一次动作:

  • 每行的第 1 个元素表示时间间隔,指的是本次动作与上一次动作之间相隔的时间,单位为毫秒。
  • 每行的第 2 个元素表示鼠标动作或是键盘动作:EM 为鼠标,EK 为键盘。
  • 每行的第 3 个元素表示动作的类型:mouse left down 为鼠标左键按下,mouse left up 为鼠标左键抬起,mouse right down 为鼠标右键按下,mouse right up 为鼠标右键抬起,key down 键盘按键按下,key up 键盘按键抬起。
  • 每行的第 4 个元素表示具体的动作参数,当为鼠标动作时,由两个子元素构成,分别为鼠标所在的屏幕位置的横纵坐标;键盘动作时为按下或抬起的按键名称。
  • 修改时请严格遵守格式,否则可能导致脚本无法运行,建议修改前先备份一下。

综上所述,示例中的脚本运行后的效果为:

  • 开始运行 3000ms 后,在屏幕坐标 (100,200)按下鼠标左键
  • 等待 50ms 后在相同位置 抬起鼠标左键
  • 等待 1000ms按下f键
  • 等待 50ms抬起f键
  • 等待 2000ms 后,在屏幕坐标 (300,400)按下鼠标左键
  • 等待 50ms 后在相同位置 抬起鼠标左键

使用命令行运行:

直接运行指定脚本:

> KeymouseGo.exe scripts/0314_1452.txt

运行指定脚本3次:

> KeymouseGo.exe scripts/0314_1452.txt 3

总结

分享:
人生,与其不断追求而无法满足,不如先沉淀自己,看清内心深处真正的需求。只要愿意打开封闭的心,去体会、去拥抱眼前的幸福,就会比别人活得更富足,更开心。

  • 16
    点赞
  • 122
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若竹之心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值