def main(): # So we're able to quit if the game finished before the bot keyboard.add_hotkey("f8", exit_) mouse = Controller() pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract' # Crop the image for i in range(1, 9): squares[i] = 120 + (8 - i + 1) * SQUARE_SIZE - 50 # Create an array of letters and numbers representing the squares on the screen for i in range(0, 8): bruh = ord("a") + i squares[chr(bruh)] = 315 + (bruh - ord("a") + 1) * SQUARE_SIZE - 50 global CLICK_NUM clicks = 0 while run: # Parse the image and see where to click square = get_square_location( pytesseract.image_to_string( get_image(), config= '-l eng --oem 1 --psm 7 -c tessedit_char_whitelist=abcdefgh012345678' ))[:2] mouse.position = square for i in range(CLICK_NUM): mouse.click(Button.left) clicks += 1 # The game gets stuck after so many clicks (it clicks 500 times # at once on a single block) if clicks == 3: CLICK_NUM = 250 # Typical time it takes the website to respond time.sleep(4 + clicks / 2)
class MouseEventsCreator: def __init__(self): self.activeRectangleKeeper = ActiveRectangleKeeper() self.mouseHandler = Controller() self.activeCursorPositionKeeper = ActiveCursorPositionKeeper() def moveMouseToGridActiveRectangleCenter(self): activeRectangleCoords: List[Tuple[int, int]] = self.activeRectangleKeeper.getActiveRectangleCoords() rectanglePoints: List[Tuple[int, int]] = self.activeRectangleKeeper.getActiveRectangleCoords() rectangleCornerPoint_topLeft: Tuple[int, int] = rectanglePoints[0] rectangleCornerPont_topRight: Tuple[int, int] = rectanglePoints[1] rectangleCornerPoint_bottomRight: Tuple[int, int] = rectanglePoints[2] rectangleCornerPoint_bottomLeft: Tuple[int, int] = rectanglePoints[3] rectangleWidth: int = rectangleCornerPont_topRight[0] - rectangleCornerPoint_topLeft[0] rectangleHeight: int = rectangleCornerPoint_bottomLeft[1] - rectangleCornerPoint_topLeft[1] centerPoint_x: int = rectangleCornerPoint_topLeft[0] + (rectangleWidth // 2) centerPoint_y: int = rectangleCornerPoint_topLeft[1] + (rectangleHeight // 2) self.activeCursorPositionKeeper.currentPos_x = centerPoint_x self.activeCursorPositionKeeper.currentPos_y = centerPoint_y centerPoint: Tuple[int, int] = (centerPoint_x, centerPoint_y) print("moving to: " + str(centerPoint)) # self.mouseHandler.position = (centerPoint_x, centerPoint_y) self.mouseHandler.position = (centerPoint) def sendClick(self): self.mouseHandler.click(Button.left) def moveMouse(self, x: int, y: int): self.mouseHandler.position = (x, y)
def click(x_axis, y_axis, times_to_click=1, seconds_after_moving_mouse=.01): mouse = Mouse_Controller() mouse.position = ( x_axis, y_axis ) time.sleep( seconds_after_moving_mouse ) if times_to_click != 0: mouse.click(Button.left, times_to_click) time.sleep( seconds_after_moving_mouse )
def click_screen_position(xy): x = xy[0] y = xy[1] time.sleep(2) mouse = Controller() mouse.position = (x, y) mouse.click(Button.left, 1)
def open_invoice_server(): # 先关闭其他tab mouse.position = (50, 10) time.sleep(1) mouse.click(Button.right, 1) time.sleep(1) mouse.position = (100, 100) time.sleep(1) mouse.click(Button.left, 1) time.sleep(1) exit(-1) print('打开发票服务所有机器,并进入日志目录') ip_list = ['88.88.9.150', '88.88.9.151'] for ip in ip_list: _open_logined_tab() # 打开新tab keyboard.type(ip) keyboard.press(Key.enter) keyboard.release(Key.enter) keyboard.type('cd /opt/dsf/log/invoice_server/') keyboard.press(Key.enter) keyboard.release(Key.enter) keyboard.type('ls') keyboard.press(Key.enter) keyboard.release(Key.enter) time.sleep(1)
class mouseTurboClick(threading.Thread): def __init__(self, mouseHook: globalMouseHook, interval=0.1, isLeftOn=False, isRightOn=False): threading.Thread.__init__(self) self.mouseHook = mouseHook self.mouse = Controller() self.set_attributes(interval, isLeftOn, isRightOn) self.isRun = True # 連點是否執行中 def set_attributes(self, interval, isLeftOn, isRightOn): self.interval = interval # 設定連點間距 if interval < 0.01: # 避免間距為0 self.interval = 0.01 self.isLeftOn = isLeftOn # 左鍵連點是否開啟 self.isRightOn = isRightOn # 右鍵連點是否開啟 def run(self): while self.isRun: if (self.isLeftOn and self.mouseHook.is_leftDown()): self.mouse.click(Button.left) if (self.isRightOn and self.mouseHook.is_rightDown()): self.mouse.click(Button.right) sleep(self.interval) def stop(self): self.isRun = False
def mouse_click_on_point(x_position, y_position): """moves the mouse corsair to the wanted position and clicking on it :param x_position: x position :param y_position: y position :return: """ mouse = Controller() # Read pointer position print('The current pointer position is {0}'.format(mouse.position)) # Set pointer position mouse.position = (x_position, y_position) print('Now we have moved it to {0}'.format(mouse.position)) # Move pointer relative to current position mouse.move(5, -5) # Press and release mouse.press(Button.left) mouse.release(Button.left) # Double click; this is different from pressing and releasing # twice on Mac OSX mouse.click(Button.left, 2) # Scroll two steps down mouse.scroll(0, 2)
def _click_at(location=None, clicks=None, duration=None, button=None): """Click on Location coordinates. :param location: Location , image name or Pattern. :param clicks: Number of mouse clicks. :param duration: Speed of hovering from current location to target. :param button: Mouse button clicked (can be left, right, middle, 1, 2, 3). :return: None. """ if duration is None: duration = Settings.move_mouse_delay if location is None: location = Location(0, 0) pyautogui.moveTo(location.x, location.y, duration) if parse_args().highlight: hl = ScreenHighlight() hl.draw_circle(HighlightCircle(location.x, location.y, 15)) hl.render() if clicks > 1: mouse = Controller() mouse.position = (location.x, location.y) mouse.click(Button.left, 2) else: pyautogui.click(clicks=clicks, interval=Settings.click_delay, button=button) if Settings.click_delay != DEFAULT_CLICK_DELAY: Settings.click_delay = DEFAULT_CLICK_DELAY
def main(): global gTimeLeftClick, gTimeRightClick global gPressedLeft, gPressedRight print("Main Started") mouseCont = Controller() print("Mouse Controller Started") listener = Listener(on_click=on_click) listener.start() while True: if gPressedLeft: print("Left Click duration =" + str(time.time() * 1000 - gTimeLeftClick)) if ((time.time() * 1000 - gTimeLeftClick) > HOLD_DURATION * 1000): mouseCont.click(Button.left, 2) if gPressedRight: print("Right Click duration =" + str(time.time() * 1000 - gTimeRightClick)) if ((time.time() * 1000 - gTimeRightClick) > HOLD_DURATION * 1000): mouseCont.click(Button.right, 2) time.sleep(RAPIDFIRE_PERIOD)
class Kontroll: def __init__(self): self.mouse = MouseController() def flyttaMus(self, x, y): def bestämMusPosition(x, y): self.mouse.position = (int(x), int(y)) def mjukMusFörflyttning(frånX, frånY, tillX, tillY, hastighet=0.2): steg = 40 # testa justera vila = hastighet // steg deltaX = (tillX - frånX) / steg deltaY = (tillY - frånY) / steg for varjeSteg in range(steg): nyttX = deltaX * (steg + 1) + frånX nyttY = deltaY * (steg + 1) + frånY nyMusPosition(nyttX, nyttY) time.sleep(vila) return mjukMusFörflyttning(self.mouse.position[0], self.mouse.position[1], x, y) def vänsterMusKlick(self): self.mouse.click(Button.left) def vänsterMusDrag(self, start, end): self.flyttaMus(*start) time.sleep(0.2) self.mouse.press(Button.left) time.sleep(0.2) self.flyttaMus(*end) time.sleep(0.2) self.mouse.release(Button.left) time.sleep(0.2)
class ClickAction(Action): def __init__(self, x: int, y: int): self.__x = x self.__y = y self.__mouse_controller = Controller() def execute(self) -> None: logger.info( f'Going to click at ({self.__x}, {self.__y}). Current position is ' f'{self.__mouse_controller.position}') # first save off the current mouse position to put it back current_position = self.__mouse_controller.position # move the mouse and click self.__mouse_controller.position = (self.__x, self.__y) logger.info('Mouse moved') time.sleep(.100) self.__mouse_controller.click(Button.left) # move the mouse back self.__mouse_controller.position = current_position logger.info(f'Done clicking at ({self.__x}, {self.__y})') def __str__(self): return f'Click at ({self.__x}, {self.__y})' def get_action_type(self) -> ActionType: return ActionType.CLICK_ACTION
class Controller: """ Allows to control the mouse and the keyboard """ def __init__(self): self.keyboard = Key_controller() self.mouse = Mouse_controller() def press_key(self, key, time_kept=0): touche = key try: touche = Key[key] except: raise (Exception("Wrong key given : {}".format(key))) self.keyboard.press(touche) sleep(time_kept) self.keyboard.release(touche) def set_mouse(self, x, y): self.mouse.position = (x, y) def press_mouse(self, x=None, y=None, clic_right=False, double_clic=False): if x and y: self.set_mouse(x, y) if clic_right: self.mouse.click(Button.right, 1 + int(double_clic)) else: self.mouse.click(Button.left, 1 + int(double_clic))
def prayClicker(): time.sleep(12) button = Button.left mouse = Controller() while (True): # Laptop Coords xcoord = random.randint(1705, 1720) ycoord = random.randint(110, 126) mouse.position = (xcoord, ycoord) mouse.click(button) time.sleep(0.4) mouse.click(button) sleeptime = random.randint(31, 54) # 110 - 250 # Alert for upcoming click print("P: Going to sleep for: " + str(sleeptime) + " seconds.") for k in range(sleeptime, 0, -1): if (k == 30): print("P: 30s left.") elif (k == 20): print("P: 20s left.") elif (k == 10): print("P: 10s left.") time.sleep(1) # Random wait that isnt a whole integer time.sleep(random.random())
class Controller: def __init__(self): self.mouse = MouseController() def move_mouse(self, x, y): def set_mouse_position(x, y): self.mouse.position = (int(x), int(y)) def smooth_move_mouse(from_x, from_y, to_x, to_y, speed=0.2): steps = 40 sleep_per_step = speed // steps x_delta = (to_x - from_x) / steps y_delta = (to_y - from_y) / steps for step in range(steps): new_x = x_delta * (step + 1) + from_x new_y = y_delta * (step + 1) + from_y set_mouse_position(new_x, new_y) time.sleep(sleep_per_step) return smooth_move_mouse(self.mouse.position[0], self.mouse.position[1], x, y) def left_mouse_click(self): self.mouse.click(Button.left) def left_mouse_drag(self, start, end): self.move_mouse(*start) time.sleep(0.2) self.mouse.press(Button.left) time.sleep(0.2) self.move_mouse(*end) time.sleep(0.2) self.mouse.release(Button.left) time.sleep(0.2)
def main(): mouse = Controller() # Read pointer position print("The current pointer position is {0}".format(mouse.position)) print("Now please stare at your mouse, not the terminal :)") print("The mouse will move to the center of the screen\n") time.sleep(MOUSE_SLEEP_INTERVAL) # Set pointer position mouse.position = (1400, 900) print("Now we have moved it to {0}".format(mouse.position)) print("A move based on absolute position") print("Then the mouse will move to the right\n") time.sleep(MOUSE_SLEEP_INTERVAL) # Move pointer relative to current position mouse.move(50, 0) print("Now we have moved it to {0}".format(mouse.position)) print("A move based on relative position") print("Then we will click the right mouse button\n") time.sleep(MOUSE_SLEEP_INTERVAL) # Press and release mouse.press(Button.right) mouse.release(Button.right) print( "Then we will move to the left and double click the left mouse button\n" ) time.sleep(MOUSE_SLEEP_INTERVAL) mouse.move(-50, 0) # Double click; this is different from pressing and releasing mouse.click(Button.left, 2)
def connect_to_zoom(): """connect_to_zoom() function checks for the active online class button on online class webpage. if it finds it, it simulates a click on it and redirects to zoom page which will open the zoom app.""" try: for i in range(1, 20): try: xpath = "/html/body/div[1]/div[3]/div[2]/div[1]/div/div/div[2]/div/div/div/div/div/div/div[2]/div[{}]/div/div/div[2]/a".format( i) active_class = driver.find_element_by_xpath(xpath) active_class.click() if active_class: driver.close() driver.switch_to.window(driver.window_handles[0]) driver.maximize_window() break except: continue except: pass finally: time.sleep(3) launch_meeting_button = driver.find_element_by_xpath( '/html/body/div[3]/div[2]/div/div[1]/div') launch_meeting_button.click() time.sleep(2) mouse = Controller() mouse.position = ( 830, 349 ) #this position needs to be updated as per the resolution of the system, and where the OS Alert box appears. mouse.click(Button.left, 1) time.sleep(5) driver.close()
def open_terminal(): # 移动到 iterm的位置,点击左键 print('当前鼠标位置 {0}'.format(mouse.position)) mouse.position = (1180, 1000) print('现在我们移动到了 {0}'.format(mouse.position)) mouse.click(Button.left, 1) time.sleep(1)
def run(self): mouse = Controller() while self.program_running: while self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1)
def assign(frame, count_defects,fingertips,com): mouse = Controller() if count_defects == 0: distance = module3.dist(fingertips[0], com) if distance < 150: cv2.putText(frame, '0', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) elif distance < 200: cv2.putText(frame, 'Double Click', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) mouse.click(Button.left, 2) time.sleep(0.5) else: cv2.putText(frame, "Click", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) mouse.click(Button.left, 1) time.sleep(0.5) elif count_defects == 1: cv2.putText(frame, "Right", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) pyautogui.moveRel(20, 0) elif count_defects == 2: cv2.putText(frame, "Down", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) pyautogui.moveRel(0, 20) elif count_defects == 3: cv2.putText(frame, "Left", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) pyautogui.moveRel(-20, 0) elif count_defects == 4: cv2.putText(frame, "Up", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2) pyautogui.moveRel(0, -20) else: pass return frame
def perform_action(message, value): global configuration message = int(message) value = float(value) if do_simulate: keyboard = KController() mouse = MController() h = root.winfo_screenheight() if 0 <= message < 40: key = configuration.buttons[int(message)].value if not str(key).__contains__("Button"): print(value) keyboard.press(key) keyboard.release(key) if value == 1 and key == 'd': print('Calibrate') Controller.calibrate() elif value == 1 and key != 'd': #Controller.move_by_X_deg(-10) Controller.move_by_one_deg_X(-10) elif str(key) == "Button.left": mouse.click(Button.left) elif str(key) == "Button.right": mouse.click(Button.right) elif message == 40: global start global calk if (start): calk = value start = False #print(value,end=' ') #print(calk,end=' ') #print(round(value - calk)) #Controller.move_by_X_deg(round(value - calk)) if abs(round(value - calk)) > 1: Controller.move_by_one_deg_X(value - calk) calk = calk + value - calk #mouse.position = mouse.position[0] , round(value / 180 * h) """ global last_value global calk global pos h = root.winfo_screenheight() if calk > h/2: calk-=1 elif calk<h/2: calk+=1 offset = ((value) / 180) * h - last_value print(offset) print(mouse.position) last_value = ((value) / 180) * h calk+=offset if value/180*h == h/2: calk = h/2 mouse.position = (mouse.position[0],round(calk)) """ """
def aggroClicker(): time.sleep(6) button = Button.left mouse = Controller() roald = [[155, 132, 25], [156, 124, 39], [25, 7, 3], [44, 12, 7], [155, 155, 141], [61, 17, 9]] kendal = [[34, 30, 20], [47, 42, 27], [39, 39, 19], [37, 33, 22], [33, 27, 14]] snake = [[180, 94, 46], [237, 125, 60], [149, 79, 38], [128, 67, 33], [239, 111, 54]] corsair = [[47, 5, 3], [101, 85, 5], [39, 7, 5], [160, 160, 147], [146, 146, 132]] vampire = [[9, 7, 7], [182, 158, 146], [89, 10, 5], [159, 132, 117], [25, 23, 23]] #colours = roald + kendal + snake + corsair + vampire colours = vampire while (True): print("starting") image = ImageGrab.grab().convert('RGB') width, height = image.size #get middle 20% start_width = int((width / 10) * 4) end_width = int((width / 10) * 6) start_height = int((height / 10) * 4) end_height = int((height / 10) * 6) validPositions = [] foundone = False for y in range(start_height, end_height): for x in range(start_width, end_width): r, g, b = image.getpixel((x, y)) for i in range(len(colours)): if ((colours[i][0] == r) and (colours[i][1] == g) and (colours[i][2] == b)): validPositions.append([x, y]) foundone = True break if (foundone == True): break if (foundone == True): break print(validPositions) monsterToClick = random.randrange(0, len(validPositions)) print(validPositions[monsterToClick]) mouse.position = (validPositions[monsterToClick][0], validPositions[monsterToClick][1]) mouse.click(button) overallSleeptime = random.randint(682, 794) #overallSleeptime = random.randint(5, 15) print("Aggro: Going to sleep for: " + str(overallSleeptime) + " seconds.") time.sleep(overallSleeptime) time.sleep(random.random())
def main(times): # input click times listen() time.sleep(2) mouse_c = Controller() for i in range(times): time.sleep(0.1) # 一定要加,不然点太快直接没了 mouse_c.click(Button.left) print('clicked {} times'.format(times))
def destroy_walls(self): mouse = Controller() for pos in self.destroyWalls: X = self.xs[pos] + self.screenXStart + 7 Y = self.ys[pos] + self.screenYStart + 7 mouse.position = (X, Y) mouse.click(Button.left) self.destroyWalls.remove(pos)
def mouseCtr(): ''' 控制鼠标 ''' # 读鼠标坐标 mouse = Controller() # 点击鼠标 mouse.click(Button.left)
def flag_it(self): mouse = Controller() for pos in self.putFlagsOn: X = self.xs[pos] + self.screenXStart + 7 Y = self.ys[pos] + self.screenYStart + 7 mouse.position = (X, Y) mouse.click(Button.right) self.putFlagsOn.remove(pos)
def click(x, y, delay, num=2): mouse = Controller() mouse.position = (x, y) for i in range(1, num): i = i time.sleep(delay) mouse.position = (x, y) mouse.click(Button.left, 1)
class Finger(): def __init__(self, speed=10): self._mse = MouseControl() self._kbd = KbdControl() self._speed = speed def type(self, keys): self._kbd.type(keys) def move(self, dx, dy, type_='abs'): if type_ == 'abs': self._mse.position = (dx, dy) else: self._mse.move(dx, dy) def click(self, button='left', count=1): button = getattr(Button, button, 'left') self._mse.click(button, count) def scroll(self, vscr=2, hscr=0): self._mse.scroll(hscr, vscr) def drag(self, to=(), frm=(), button='left'): # simulate a drag. TODO: make in increments instead of instant if not frm: frm = self._mse.position if not to: to = self._mse.position button = getattr(Button, button, 'left') self._mse.press(button) self._mse.position = to self._mse.release(button) #while not self._mse.position == to: # simulate movement one step at a time #x_pos = self._mse.position[0] #y_pos = self._mse.position[1] #if not x_pos == to[0]: x_pos def press(self, device='mouse', button='left'): if device == 'mouse': self._mse.press(getattr(Button, button, 'left')) else: key = button if len(button) == 1 else getattr(Key, button, 'space') self._kbd.press(key) def release(self, device='mouse', button='left'): # specify mouse or keyboard if device == 'mouse': self._mse.release(getattr(Button, button, 'left')) else: key = button if len(button) == 1 else getattr(Key, button, 'space') self._kbd.release(key)
def click_at(x, y, cm=False): mouse = Mouse() button = Button.left if cm: x = int(x * 1366 / 34.3) y = int(y * 768 / 19.3) mouse.position = (x, y) mouse.position = (x, y) mouse.click(button)
class Flower: def __init__(self, colors, lvl): self.colors = colors self.lvl = lvl def cut_flower(self, position): self.mouse = Controller() self.mouse.position = position self.mouse.click(Button.left, 2)
def start_game(): button_location_btn = 756, 521 mouse = Controller() screenWidth, screenHeight = pyautogui.size() time.sleep(1) pyautogui.moveTo(button_location_btn) time.sleep(3) mouse.click(Button.left, 2) pyautogui.press("m")
#-*-coding:utf-8-*- from pynput.mouse import Button, Controller import time import win32clipboard as w import win32con from controlKeyboard import doKeyboard,altUp,down,enter,LaoGongTV,ctrlTab,ctrlW,space,ctrlV,keyFill,ctrlShiftI mouse = Controller() def setText(aString): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT, aString) w.CloseClipboard() for i in range(100): mouse.position = (1702,1006) mouse.click(Button.left,1) time.sleep(0.5) codes = ''' document.getElementsByClassName("p-txt")[%s].innerText; ''' % str(i) setText(codes) ctrlV() # time.sleep(2) enter()
class MouseKeyboard: def __init__(self): self.keyboard = KeyboardController() self.mouse = MouseController() self.controlKeys = {37:self.keyLeft, 38:self.keyUp, 39:self.keyRight, 40:self.keyDown, 60:self.leftClick, 62:self.rightClick, 8:self.keyBksp, 13:self.keyEnter} def handleButtonPress(self, key): key = key.strip("'\\") try: keyNum = int(key) except ValueError: keyNum = ord(key) try: print('Got {}.'.format(keyNum)) except OSError: pass if keyNum in self.controlKeys.keys(): self.controlKeys[keyNum]() else: try: self.keyboard.press(key) self.keyboard.release(key) except ValueError: print("Failed to press {}: ValueError.".format(key)) def handleMouseMove(self, key): key = key.strip("'\\") (xStr, yStr) = key.split() try: xVal = int(xStr) yVal = int(yStr) except ValueError: print("Got MouseMove but x, y string format unexpected: '{}', '{}'".format(xStr, yStr)) print("Mouse Moving by: {}, {}".format(xVal,yVal)) self.mouse.move(xVal*8,yVal*8) def keyUp(self): self.keyboard.press(Key.up) self.keyboard.release(Key.up) def keyLeft(self): self.keyboard.press(Key.left) self.keyboard.release(Key.left) def keyRight(self): self.keyboard.press(Key.right) self.keyboard.release(Key.right) def keyDown(self): self.keyboard.press(Key.down) self.keyboard.release(Key.down) def keyBksp(self): self.keyboard.press(Key.backspace) self.keyboard.release(Key.backspace) def keySpace(self): self.keyboard.press(Key.space) self.keyboard.release(Key.space) def keyEnter(self): self.keyboard.press(Key.enter) self.keyboard.release(Key.enter) def leftClick(self): self.mouse.click(Button.left) def rightClick(self): self.mouse.click(Button.right)