|
| 1 | +import win32api |
| 2 | +import win32con |
| 3 | +import time |
| 4 | +import pyautogui |
| 5 | +import random |
| 6 | + |
| 7 | + |
| 8 | +def writer(click): |
| 9 | + x, y = pyautogui.position() |
| 10 | + print('click recorded at: ' + str(x) + ', ' + str(y)) |
| 11 | + time.sleep(0.1) |
| 12 | + with open(cordFile+'.txt', 'a') as f: |
| 13 | + f.write(str(click) + ' ' + str(x) + ' ' + str(y) + ' 1\n') |
| 14 | + time.sleep(0.1) |
| 15 | + |
| 16 | + |
| 17 | +def recorder(action): |
| 18 | + print('program ready to record | press Shift to STOP') |
| 19 | + # get state from virtual-key-codes |
| 20 | + while action: |
| 21 | + if (win32api.GetKeyState(0x01)) < 0: |
| 22 | + writer(1) |
| 23 | + elif (win32api.GetKeyState(0x02)) < 0: |
| 24 | + writer(2) |
| 25 | + elif (win32api.GetKeyState(0x10)) < 0: |
| 26 | + print('program stopped') |
| 27 | + action = False |
| 28 | + |
| 29 | + |
| 30 | +def realTimer(action): |
| 31 | + print('program ready to record | press Shift to STOP') |
| 32 | + # get state from virtual-key-codes |
| 33 | + rTime = time.time() |
| 34 | + print(rTime) |
| 35 | + while action: |
| 36 | + if (win32api.GetKeyState(0x01)) < 0: |
| 37 | + x, y = pyautogui.position() |
| 38 | + pTime = int(time.time() - rTime) |
| 39 | + print('click recorded at: ' + str(x) + ', ' + str(y)) |
| 40 | + time.sleep(0.1) |
| 41 | + with open(cordFile+'.txt', 'a') as f: |
| 42 | + f.write('1 ' + str(x) + ' ' + str(y) + |
| 43 | + ' ' + str(pTime) + ' 1\n') |
| 44 | + time.sleep(0.1) |
| 45 | + elif (win32api.GetKeyState(0x02)) < 0: |
| 46 | + x, y = pyautogui.position() |
| 47 | + pTime = int(time.time() - rTime) |
| 48 | + print('click recorded at: ' + str(x) + ', ' + str(y)) |
| 49 | + time.sleep(0.1) |
| 50 | + with open(cordFile+'.txt', 'a') as f: |
| 51 | + f.write('2 ' + str(x) + ' ' + str(y) + |
| 52 | + ' ' + str(pTime) + ' 1\n') |
| 53 | + time.sleep(0.1) |
| 54 | + elif (win32api.GetKeyState(0x10)) < 0: |
| 55 | + print('program stopped') |
| 56 | + break |
| 57 | + |
| 58 | + |
| 59 | +def Rec(): |
| 60 | + # delete the file if it exists |
| 61 | + try: |
| 62 | + open(cordFile+'.txt', 'w').close() |
| 63 | + except: |
| 64 | + pass |
| 65 | + print(pyautogui.size()) # print screen size |
| 66 | + print('mode : [1] speed, [2] time-range, [3] real-time') |
| 67 | + mode = input('select mode : ') |
| 68 | + if mode == '1': |
| 69 | + print('speed mode selected') |
| 70 | + with open(cordFile+'.txt', 'a') as f: |
| 71 | + f.write('1 0 0\n') |
| 72 | + recorder(True) |
| 73 | + elif mode == '2': |
| 74 | + print('time range mode selected') |
| 75 | + sTime = input('start range (s) :') |
| 76 | + eTime = input('end range (s) :') |
| 77 | + timeRange = sTime + ' ' + eTime |
| 78 | + print('time range : ' + timeRange) |
| 79 | + with open(cordFile+'.txt', 'a') as f: |
| 80 | + f.write('2 ' + timeRange + '\n') |
| 81 | + recorder(True) |
| 82 | + elif mode == '3': |
| 83 | + print('real time mode selected') |
| 84 | + with open(cordFile+'.txt', 'a') as f: |
| 85 | + f.write('3 0 0\n') |
| 86 | + realTimer(True) |
| 87 | + else: |
| 88 | + print('error') |
| 89 | + exit() |
| 90 | + |
| 91 | + |
| 92 | +def rightCord(xCord=None, yCord=None, pressTimes=1): |
| 93 | + pyautogui.rightClick(xCord, yCord, pressTimes) |
| 94 | + |
| 95 | + |
| 96 | +def clickCord(xCord=None, yCord=None, pressTimes=1): |
| 97 | + pyautogui.click(xCord, yCord, pressTimes) |
| 98 | + |
| 99 | + |
| 100 | +def moveCord(xCord=None, yCord=None, delay=0.1): |
| 101 | + pyautogui.moveTo(xCord, yCord, delay) |
| 102 | + |
| 103 | + |
| 104 | +# read cordFile and loop through each line |
| 105 | +def Play(): |
| 106 | + with open(cordFile+'.txt') as f: |
| 107 | + mode, sRange, eRange = map(int, f.readline().split(' ')) |
| 108 | + if mode == 1: |
| 109 | + for line in f.readlines()[1:]: |
| 110 | + # print('hi') |
| 111 | + which, xCord, yCord, pressTimes = map(int, line.split()) |
| 112 | + if which == 1: |
| 113 | + clickCord(xCord, yCord, pressTimes) |
| 114 | + elif which == 2: |
| 115 | + rightCord(xCord, yCord, pressTimes) |
| 116 | + elif mode == 2: |
| 117 | + print(sRange, eRange) |
| 118 | + # interval random float from sRange to eRange |
| 119 | + for line in f.readlines()[1:]: |
| 120 | + interval = random.uniform(sRange, eRange) |
| 121 | + print(interval) |
| 122 | + which, xCord, yCord, pressTimes = map(int, line.split()) |
| 123 | + moveCord(xCord, yCord, interval) |
| 124 | + if which == 1: |
| 125 | + clickCord(xCord, yCord, pressTimes) |
| 126 | + elif which == 2: |
| 127 | + rightCord(xCord, yCord, pressTimes) |
| 128 | + elif mode == 3: |
| 129 | + reminder = 0 |
| 130 | + for line in f.readlines()[1:]: |
| 131 | + which, xCord, yCord, interval, pressTimes = map( |
| 132 | + int, line.split()) |
| 133 | + # 0.6 seconds is the default prefix time |
| 134 | + moveCord(xCord, yCord, interval - reminder - 0.6) |
| 135 | + if which == 1: |
| 136 | + clickCord(xCord, yCord, pressTimes) |
| 137 | + elif which == 2: |
| 138 | + rightCord(xCord, yCord, pressTimes) |
| 139 | + reminder = interval |
| 140 | + else: |
| 141 | + print('mode not found') |
| 142 | + |
| 143 | + |
| 144 | +def main(): |
| 145 | + # by marvin zhong at github |
| 146 | + print('Welcome to the Mouse Replayer by Marvin Zhong at github') |
| 147 | + while True: |
| 148 | + print( |
| 149 | + 'mode to select\t: [1] to record mouse event, [2] to replay your recorded mouse event') |
| 150 | + mode = input('select mode\t: ') |
| 151 | + if mode == '1': |
| 152 | + Rec() |
| 153 | + elif mode == '2': |
| 154 | + timeSecs = int(input('program will run after (s) : ')) |
| 155 | + startTime = time.time() |
| 156 | + # for timeSecs make countdown |
| 157 | + while time.time() - startTime < timeSecs: |
| 158 | + print(timeSecs - int(time.time() - startTime)) |
| 159 | + time.sleep(1) |
| 160 | + Play() |
| 161 | + endTime = int(time.time() - startTime) |
| 162 | + print('program runtime : ', endTime, 's') |
| 163 | + else: |
| 164 | + print('error') |
| 165 | + exit() |
| 166 | + print('do you want to close the program? [y/n]') |
| 167 | + close = input('close? : ') |
| 168 | + if close == 'y': |
| 169 | + break |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == '__main__': |
| 173 | + cordFile = 'mousePosition' |
| 174 | + main() |
0 commit comments