Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinZhong committed May 29, 2022
1 parent 948f0f1 commit 6f894a0
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt
68 changes: 68 additions & 0 deletions mousePlay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import time
import pyautogui
import random


def rightCord(xCord=None, yCord=None, pressTimes=1):
pyautogui.rightClick(xCord, yCord, pressTimes)


def clickCord(xCord=None, yCord=None, pressTimes=1):
pyautogui.click(xCord, yCord, pressTimes)


def moveCord(xCord=None, yCord=None, delay=0.1):
pyautogui.moveTo(xCord, yCord, delay)


# read cordFile and loop through each line
def main():
with open(cordFile+'.txt') as f:
mode, sRange, eRange = map(int, f.readline().split(' '))
if mode == 1:
for line in f.readlines()[1:]:
# print('hi')
which, xCord, yCord, pressTimes = map(int, line.split())
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
elif mode == 2:
print(sRange, eRange)
# interval random float from sRange to eRange
for line in f.readlines()[1:]:
interval = random.uniform(sRange, eRange)
print(interval)
which, xCord, yCord, pressTimes = map(int, line.split())
moveCord(xCord, yCord, interval)
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
elif mode == 3:
reminder = 0
for line in f.readlines()[1:]:
which, xCord, yCord, interval, pressTimes = map(
int, line.split())
# 0.6 seconds is the default prefix time
moveCord(xCord, yCord, interval - reminder - 0.6)
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
reminder = interval
else:
print('mode not found')


if __name__ == '__main__':
cordFile = 'mousePosition'
timeSecs = int(input('program will run after (s) : '))
startTime = time.time()
# for timeSecs make countdown
while time.time() - startTime < timeSecs:
print(timeSecs - int(time.time() - startTime))
time.sleep(1)
main()
endTime = int(time.time() - startTime)
print('program runtime : ', endTime, 's')
94 changes: 94 additions & 0 deletions mouseRec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import win32api
import win32con
import time
import pyautogui


def writer(click):
x, y = pyautogui.position()
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile+'.txt', 'a') as f:
f.write(str(click) + ' ' + str(x) + ' ' + str(y) + ' 1\n')
time.sleep(0.1)


def recorder(action):
print('program ready to record | press Shift to STOP')
# action = act
# get state from virtual-key-codes
while action:
if (win32api.GetKeyState(0x01)) < 0:
writer(1)
elif (win32api.GetKeyState(0x02)) < 0:
writer(2)
elif (win32api.GetKeyState(0x10)) < 0:
print('program stopped')
action = False


def realTimer(action):
print('program ready to record | press Shift to STOP')
# get state from virtual-key-codes
rTime = time.time()
print(rTime)
while action:
if (win32api.GetKeyState(0x01)) < 0:
x, y = pyautogui.position()
pTime = int(time.time() - rTime)
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile+'.txt', 'a') as f:
f.write('1 ' + str(x) + ' ' + str(y) +
' ' + str(pTime) + ' 1\n')
time.sleep(0.1)
elif (win32api.GetKeyState(0x02)) < 0:
x, y = pyautogui.position()
pTime = int(time.time() - rTime)
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile+'.txt', 'a') as f:
f.write('2 ' + str(x) + ' ' + str(y) +
' ' + str(pTime) + ' 1\n')
time.sleep(0.1)
elif (win32api.GetKeyState(0x10)) < 0:
print('program stopped')
break


# delete the file if it exists
def main():
try:
open(cordFile+'.txt', 'w').close()
except:
pass
print(pyautogui.size()) # print screen size
print('mode : [1] speed, [2] time-range, [3] real-time')
mode = input('select mode : ')
if mode == '1':
print('speed mode selected')
with open(cordFile+'.txt', 'a') as f:
f.write('1 0 0\n')
recorder(True)
elif mode == '2':
print('time range mode selected')
sTime = input('start range (s) :')
eTime = input('end range (s) :')
timeRange = sTime + ' ' + eTime
print('time range : ' + timeRange)
with open(cordFile+'.txt', 'a') as f:
f.write('2 ' + timeRange + '\n')
recorder(True)
elif mode == '3':
print('real time mode selected')
with open(cordFile+'.txt', 'a') as f:
f.write('3 0 0\n')
realTimer(True)
else:
print('error')
exit()


if __name__ == '__main__':
cordFile = 'mousePosition'
main()
Binary file added mouseReplayer.ico
Binary file not shown.
Binary file added mouseReplayer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Mouse Replayer

---

a simple mouse application that replays mouse events

let's automate boring tasks

## Usage :

0. Make mousePosition.txt
1. Edit the mousePosition.txt file or record with in App > Record Mouse Function
2. run the application select [2] to replay the mouse events

## Detail of mousePosition.txt:

- First Row are:
- mode: [1] speed | [2] random time in range | [3] real time,
- start range (optional, set to 0 if not used))
- end range (optional, set to 0 if not used)
- eg : 1 0 0
- Second Row and so on:
- Left click: 1 | right click: 2
- x position
- y position
- time in seconds (optional for random and real time)
- how many times of clicking
- eg : 1 500 500 1

<!-- ## Installation : -->

<!-- 1. Windows
- Download [MouseReplayer.exe]()
2. Mac OS
- Download [MouseReplayer.app]()
3. Python Environment
- clone the [replayer]() repository -->

## Note :

THERES NO KEYLOGGER OR SUCH IN THIS APPLICATION
174 changes: 174 additions & 0 deletions recordAndPlay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import win32api
import win32con
import time
import pyautogui
import random


def writer(click):
x, y = pyautogui.position()
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile+'.txt', 'a') as f:
f.write(str(click) + ' ' + str(x) + ' ' + str(y) + ' 1\n')
time.sleep(0.1)


def recorder(action):
print('program ready to record | press Shift to STOP')
# get state from virtual-key-codes
while action:
if (win32api.GetKeyState(0x01)) < 0:
writer(1)
elif (win32api.GetKeyState(0x02)) < 0:
writer(2)
elif (win32api.GetKeyState(0x10)) < 0:
print('program stopped')
action = False


def realTimer(action):
print('program ready to record | press Shift to STOP')
# get state from virtual-key-codes
rTime = time.time()
print(rTime)
while action:
if (win32api.GetKeyState(0x01)) < 0:
x, y = pyautogui.position()
pTime = int(time.time() - rTime)
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile+'.txt', 'a') as f:
f.write('1 ' + str(x) + ' ' + str(y) +
' ' + str(pTime) + ' 1\n')
time.sleep(0.1)
elif (win32api.GetKeyState(0x02)) < 0:
x, y = pyautogui.position()
pTime = int(time.time() - rTime)
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile+'.txt', 'a') as f:
f.write('2 ' + str(x) + ' ' + str(y) +
' ' + str(pTime) + ' 1\n')
time.sleep(0.1)
elif (win32api.GetKeyState(0x10)) < 0:
print('program stopped')
break


def Rec():
# delete the file if it exists
try:
open(cordFile+'.txt', 'w').close()
except:
pass
print(pyautogui.size()) # print screen size
print('mode : [1] speed, [2] time-range, [3] real-time')
mode = input('select mode : ')
if mode == '1':
print('speed mode selected')
with open(cordFile+'.txt', 'a') as f:
f.write('1 0 0\n')
recorder(True)
elif mode == '2':
print('time range mode selected')
sTime = input('start range (s) :')
eTime = input('end range (s) :')
timeRange = sTime + ' ' + eTime
print('time range : ' + timeRange)
with open(cordFile+'.txt', 'a') as f:
f.write('2 ' + timeRange + '\n')
recorder(True)
elif mode == '3':
print('real time mode selected')
with open(cordFile+'.txt', 'a') as f:
f.write('3 0 0\n')
realTimer(True)
else:
print('error')
exit()


def rightCord(xCord=None, yCord=None, pressTimes=1):
pyautogui.rightClick(xCord, yCord, pressTimes)


def clickCord(xCord=None, yCord=None, pressTimes=1):
pyautogui.click(xCord, yCord, pressTimes)


def moveCord(xCord=None, yCord=None, delay=0.1):
pyautogui.moveTo(xCord, yCord, delay)


# read cordFile and loop through each line
def Play():
with open(cordFile+'.txt') as f:
mode, sRange, eRange = map(int, f.readline().split(' '))
if mode == 1:
for line in f.readlines()[1:]:
# print('hi')
which, xCord, yCord, pressTimes = map(int, line.split())
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
elif mode == 2:
print(sRange, eRange)
# interval random float from sRange to eRange
for line in f.readlines()[1:]:
interval = random.uniform(sRange, eRange)
print(interval)
which, xCord, yCord, pressTimes = map(int, line.split())
moveCord(xCord, yCord, interval)
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
elif mode == 3:
reminder = 0
for line in f.readlines()[1:]:
which, xCord, yCord, interval, pressTimes = map(
int, line.split())
# 0.6 seconds is the default prefix time
moveCord(xCord, yCord, interval - reminder - 0.6)
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
reminder = interval
else:
print('mode not found')


def main():
# by marvin zhong at github
print('Welcome to the Mouse Replayer by Marvin Zhong at github')
while True:
print(
'mode to select\t: [1] to record mouse event, [2] to replay your recorded mouse event')
mode = input('select mode\t: ')
if mode == '1':
Rec()
elif mode == '2':
timeSecs = int(input('program will run after (s) : '))
startTime = time.time()
# for timeSecs make countdown
while time.time() - startTime < timeSecs:
print(timeSecs - int(time.time() - startTime))
time.sleep(1)
Play()
endTime = int(time.time() - startTime)
print('program runtime : ', endTime, 's')
else:
print('error')
exit()
print('do you want to close the program? [y/n]')
close = input('close? : ')
if close == 'y':
break


if __name__ == '__main__':
cordFile = 'mousePosition'
main()

0 comments on commit 6f894a0

Please sign in to comment.