Last active
August 29, 2015 14:02
-
-
Save aznnico/0d92b553702f527c95b7 to your computer and use it in GitHub Desktop.
Mouse Clicker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
BlueStacks App Auto Clicker. | |
This program will find the BlueStacks App Player and make it the foreground | |
window. It will then send a mouse click to the top left corner of the application. | |
Finally, it will restore the foreground application and mouse position to its | |
original position. | |
""" | |
import win32api | |
import win32con | |
import win32gui | |
import re | |
import time | |
import threading | |
import pythoncom | |
import pyHook | |
SLEEP_TIMER = 60*2 | |
#SLEEP_TIMER = 5 | |
class WindowMgr: | |
"""Encapsulates some calls to the winapi for window management""" | |
def __init__ (self): | |
"""Constructor""" | |
self._handle = None | |
self.old_window_title = None | |
self.old_mouse_position = None | |
self.last_bluestacks_mouse_position = None | |
def find_window(self, class_name, window_name = None): | |
"""find a window by its class_name""" | |
self._handle = win32gui.FindWindow(class_name, window_name) | |
def _window_enum_callback(self, hwnd, wildcard): | |
"""Pass to win32gui.EnumWindows() to check all the opened windows""" | |
#if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None: | |
if wildcard == str(win32gui.GetWindowText(hwnd)) or wildcard in str(win32gui.GetWindowText(hwnd)): | |
self._handle = hwnd | |
def _window_enum_callback_bluestacks(self, hwnd, useless): | |
"""Pass to win32gui.EnumWindows() to check all the opened windows""" | |
if 'BlueStacks' in str(win32gui.GetWindowText(hwnd)): | |
print 'Found',str(win32gui.GetWindowText(hwnd)) | |
self.save_current_window() | |
self._handle = hwnd | |
self.set_foreground() | |
self.click(50,50) | |
self.restore_window() | |
def find_window_wildcard(self, wildcard): | |
print 'Finding window', wildcard | |
self._handle = None | |
win32gui.EnumWindows(self._window_enum_callback, wildcard) | |
def find_and_click_bluestacks(self): | |
print 'Find and click bluestacks' | |
self._handle = None | |
win32gui.EnumWindows(self._window_enum_callback_bluestacks,()) | |
def set_foreground(self): | |
"""put the window in the foreground""" | |
print 'Setting "%s" as the foreground' %win32gui.GetWindowText(self._handle) | |
if self._handle: | |
try: | |
win32gui.SetActiveWindow(self._handle) | |
#win32gui.SetForegroundWindow(self._handle) | |
except Exception as e: | |
print '[EXCEPTION] set foreground exception', str(e) | |
#win32gui.SetActiveWindow(self._handle) | |
else: | |
print 'No handle' | |
def save_current_window(self): | |
self.old_window_title = win32gui.GetWindowText(win32gui.GetForegroundWindow()) | |
#self.old_window_title = self.old_window_title.replace('.',' ').replace('*',' ').replace('\\',' ').strip() | |
#self.old_window_title = self.old_window_title.replace(' ','.*?') | |
self.old_hwnd = win32gui.GetForegroundWindow() | |
self.old_mouse_position = win32api.GetCursorPos() | |
print 'Saving current window and mouse:', self.old_window_title, self.old_mouse_position | |
def restore_window(self): | |
print 'Restoring old window' | |
#self.find_window_wildcard('%s' %self.old_window_title) | |
win32gui.SetForegroundWindow(self.old_hwnd) | |
win32api.SetCursorPos(self.old_mouse_position) | |
def do_proceed(self): | |
# Get current window and mouse position and check it against last run. | |
cur_window = win32gui.GetWindowText(win32gui.GetForegroundWindow()) | |
cur_mouse = win32api.GetCursorPos() | |
#self.status() | |
#print 'cur window', cur_window | |
#print 'cur_mouse', cur_mouse | |
if 'BlueStacks' not in cur_window: | |
return True | |
else: | |
if cur_mouse == self.last_bluestacks_mouse_position: | |
return True | |
else: | |
self.last_bluestacks_mouse_position = cur_mouse | |
return False | |
def status(self): | |
print 'old window title', self.old_window_title | |
print 'old mouse', self.old_mouse_position | |
print 'last bluestacks mouse', self.last_bluestacks_mouse_position | |
def click(self,x,y): | |
print 'Sending mouse click' | |
rect = win32gui.GetWindowRect(self._handle) | |
a,b,c,d = rect | |
x = x+a | |
y = y+b | |
win32api.SetCursorPos((x,y)) | |
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) | |
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0) | |
class BlueStacks(threading.Thread): | |
power = True | |
def __init__(self): | |
threading.Thread.__init__(self) | |
@classmethod | |
def deactivate(cls): | |
cls.power = False | |
print 'Deactivated power', cls.power | |
@classmethod | |
def activate(cls): | |
cls.power = True | |
print 'Activated power', cls.power | |
@classmethod | |
def status(cls): | |
print 'status power', cls.power | |
def run(self): | |
w = WindowMgr() | |
while True: | |
print '.'*10 | |
self.status() | |
if self.power: | |
try: | |
if w.do_proceed(): | |
w.find_and_click_bluestacks() | |
except Exception as e: | |
print e | |
time.sleep(SLEEP_TIMER) | |
bs = BlueStacks() | |
bs.start() | |
def handle_event(event): | |
if event.Key == 'F10': | |
BlueStacks.deactivate() | |
if event.Key == 'F12': | |
BlueStacks.activate() | |
#print event.Key | |
return True | |
hm = pyHook.HookManager() | |
hm.KeyDown = handle_event | |
hm.HookKeyboard() | |
pythoncom.PumpMessages() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment