def draw_arc(self, window): #self.drawing_area.window.draw_arc(self.context, False, self._x, self._y, 70, 70, 0, 360*64) window.set_keep_below(True) window.hide() m = PyMouse() m.move(self._x, self._y) m.click(self._x, self._y, 1)
class Mouse: @property def vis(self): return self._vis def __init__(self,vis): self._vis = vis self.pm = PM() """ Takes a board coordinate (x,y) and uses self._vis to calculate the screen coordinates to click there. Then it waits 5.7 ms, and takes a screenshot""" def left_click(self,x,y): #print "clicking at",x,y x0, y0 = self.vis.edge_coords x1, y1 = (x0 + 8 + x * 16, y0 + 8 + y * 16) self.pm.move(x1,y1) self.pm.click(x1,y1,1) #time.sleep(0.25) def right_click(self,x,y): x0, y0 = self.vis.edge_coords x1, y1 = (x0 + 8 + x * 16, y0 + 8 + y * 16) self.pm.click(x1,y1,2) #time.sleep(0.05) """ Uses self._vis to determine the position of the smiley reset button""" def reset_board(self): (resx, resy) = self.vis.get_reset() self.pm.move(resx,resy) self.pm.press(resx,resy,1) time.sleep(0.5) self.pm.release(resx,resy,1)
def netflix_continue(): m = PyMouse() x, y = m.screen_size() m.click(x/2, y/2-60) time.sleep(0.33) m.move(x/2, 0) return ""
def netflix_play(): m = PyMouse() m.move(150, 640) time.sleep(0.25) m.click(150, 640) m.move(300, 0) return ""
class WinMouseControl(): mouseObject='' def __init__(self): self.mouseObject=PyMouse() print "mouse init success" pass def __del__(self): pass def mouseMove(self,x,y,button=None): self.mouseObject.move(x, y) return 0 def mouseClick(self,x,y,button): self.mouseObject.click(x, y, button) #button:1 left, button2: middle button3:right return 0 def mousePress(self,x,y,button): self.mouseObject.press(x, y, button) #button:1 left, button2: middle button3:right return 0 def mouseRelease(self,x,y,button): self.mouseObject.release(x, y, button) #button:1 left, button2: middle button3:right return 0
def click(pos): ''' pos: (x, y) # (0, 0) for top left piece ''' m = PyMouse() m.click(RECT[0] + PIECE_X / 2 + PIECE_X * pos[0], RECT[1] + PIECE_Y / 2 + PIECE_Y * pos[1]) time.sleep(0.5)
def main(): mouse = PyMouse() # wm = cwiid.Wiimote("00:25:A0:CE:3B:6D") wm = cwiid.Wiimote() wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_IR X, x = calibrate(wm) trans = getTransMatrix(x, X) screen = mouse.screen_size() lastTime = time.time() o = None state = NEUTRAL print("battery: %f%%" % (float(wm.state["battery"]) / float(cwiid.BATTERY_MAX) * 100.0)) window = pygame.display.set_mode((200, 150)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) pos = getWiiPointNonBlock(wm) if pos != None: ipt = numpy.matrix([[pos.x], [pos.y], [1]]) optMat = trans.I * ipt o = Point(optMat[0] / optMat[2], optMat[1] / optMat[2]) if o.x < 0: o.x = 0 elif o.x >= screen[0]: o.x = screen[0] - 1 if o.y < 0: o.y = 0 elif o.y >= screen[1]: o.y = screen[1] - 1 if state == NEUTRAL: state = FIRST_INPUT lastTime = time.time() elif state == FIRST_INPUT: if time.time() - FIRST_INPUT_TO_PRESSED > lastTime: mouse.press(o.x, o.y) state = PRESSED elif state == PRESSED: mouse.move(o.x, o.y) if state == FIRST_INPUT and pos == None and time.time() - FIRST_INPUT_TO_PRESSED < lastTime: mouse.click(o.x, o.y) time.sleep(0.2) state = NEUTRAL if state == PRESSED and pos == None and time.time() - 1 > lastTime: mouse.release(o.x, o.y) state = NEUTRAL
def mouse_simulate(): try: class event(PyMouseEvent): def move(self, x, y): pass def click(self, x, y, button, press): if press: print("Mouse pressed at", x, y, "with button", button) else: print("Mouse released at", x, y, "with button", button) e = event() e.start() except ImportError: print("Mouse events are not yet supported on your platform") m = PyMouse() t_corr = (1173,313) i_pos = t_corr m.move(i_pos[0] , i_pos[1]) while(True): if is_run is False: time.sleep(1) else: time.sleep(0.2) m.click(i_pos[0], i_pos[1]) try: e.stop() except: pass
class Robot(object): def __init__(self): self.mouse = PyMouse() self.keyboard = PyKeyboard() self.przyciskPSP2klawiatura = {'up': 'w', 'right': 'd', 'down': 's', 'left': 'a', 'triangle': self.keyboard.enter_key, 'circle': 'f', 'cross': 'g', 'square': 'h', 'l': self.keyboard.control_r_key, 'r': self.keyboard.shift_r_key, 'start': 'k', 'select': 'l'} def reaguj(self, x, y, przyciskPSP2Stan): self.reaguj_mysz(x, y) self.reaguj_klawiatura(przyciskPSP2Stan) def reaguj_mysz(self, x, y): max_predkosc_kursora = 0.00000000000000000000000000000000000000000000000000001 x += int((x / float(128)) * max_predkosc_kursora + self.mouse.position()[0]) y += int((y / float(128)) * max_predkosc_kursora + self.mouse.position()[1]) x, y = min(self.mouse.screen_size()[0], x), min( self.mouse.screen_size()[1], y) x, y = max(0, x), max(0, y) self.mouse.move(x, y) def reaguj_klawiatura(self, przyciskPSP2Stan): for przycisk_psp, czyWcisniety in przyciskPSP2Stan.iteritems(): przycisk_klawiaturowy = self.przyciskPSP2klawiatura[przycisk_psp] if czyWcisniety == '1': if przycisk_klawiaturowy == 'g': self.mouse.click(*self.mouse.position()) break self.keyboard.press_key(przycisk_klawiaturowy) else: self.keyboard.release_key(przycisk_klawiaturowy)
def on_message(self, message): m = PyMouse() data = json.loads(message) source = data['from'] destination = data['to'] offset = data['offset'] m.click(source['x'] + offset, source['y'] + offset) m.click(destination['x'] + offset, destination['y'] + offset)
def click(p, t = 0.6): x = p.x y = p.y m = PyMouse() m.move(x, y) m.click(x, y, 1) # m.press(x, y) # m.release(x, y) time.sleep(t)
def main(): mouse = PyMouse() # wm = cwiid.Wiimote("00:25:A0:CE:3B:6D") wm = cwiid.Wiimote() wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_IR X,x = calibrate(wm) trans = getTransMatrix(x, X) screen = mouse.screen_size() lastTime = time.time() o = None points = [] print('battery: %f%%' % (float(wm.state['battery']) / float(cwiid.BATTERY_MAX) * 100.0)) window = pygame.display.set_mode((200, 150)) while True: for event in pygame.event.get(): if (event.type == pygame.QUIT): sys.exit(0) while (time.time() < lastTime + 0.01): pos = getWiiPointNonBlock(wm) if (pos != None): points.append(pos) print(len(points)) if (len(points) > 0): pos = avPoints(points) ipt = numpy.matrix([[pos.x], [pos.y], [1]]) optMat = trans.I * ipt o = Point(optMat[0] / optMat[2], optMat[1] / optMat[2]) if (o.x < 0): o.x = 0 elif (o.x >= screen[0]): o.x = screen[0] - 1 if (o.y < 0): o.y = 0 elif (o.y >= screen[1]): o.y = screen[1] - 1 mouse.move(o.x, o.y) if (wm.state['buttons'] & cwiid.BTN_A): mouse.click(o.x, o.y) lastTime = time.time() points = []
def all_in(a=None): m = PyMouse() if a: time.sleep(a) k = (679,558) m.click(k[0],k[1], 1) for a in range(0,13): wsh.SendKeys("9") wsh.SendKeys("{ENTER} ") wsh.SendKeys("{F3}")
class MouseRemote(AweRemPlugin): """ Print "RedButton Triggered" """ def activate(self): self.realMouse = PyMouse() self.handler = MouseHandler(self) self.info = {"title": "Mouse", "category": "utils", "priority": 0} def getHandler(self): return self.handler def getInfo(self): return self.info def getIconPath(self, dpi): return "" def click(self, button, x=None, y=None): curx, cury = self.realMouse.position() if x is None: x = curx if y is None: y = cury self.realMouse.click(x, y, button) def press(self, button, x=None, y=None): curx, cury = self.realMouse.position() if x is None: x = curx if y is None: y = cury self.realMouse.press(x, y, button) def release(self, button, x=None, y=None): curx, cury = self.realMouse.position() if x is None: x = curx if y is None: y = cury self.realMouse.release(x, y, button) def move(self, deltaX, deltaY): curx, cury = self.realMouse.position() if deltaX is not None: curx += deltaX if deltaY is not None: cury += deltaY self.realMouse.move(curx, cury) def moveAbsolute(self, x, y): self.realMouse.move(x, y)
def run(self): mouse = PyMouse() old_x_dim = None old_y_dim = None while not self._stopevent.isSet(): time.sleep(0.5) x_dim, y_dim = mouse.position() if x_dim != old_x_dim or y_dim != old_y_dim: mouse.click(x_dim, y_dim, 1) self._stopevent.wait(1.0) old_x_dim = x_dim old_y_dim = y_dim threading.Thread.__init__(self) self._stopevent = threading.Event()
class Player: def __init__(self): self.mouse = PyMouse() def click(self, pos): self.mouse.click(*pos) def shoot(self, target, power, table_offset, cue_ball): adj_target = np.add(target, table_offset) self.mouse.press(*adj_target) adj_cue = np.add(cue_ball.get_pos(), table_offset) self.mouse.drag(*adj_cue) self.mouse.release(*adj_cue)
def set_mouse(x=0,y=0,click=0): if system() == "Darwin": sp.Popen(["./mac_os_helpers/mouse", "-x", "%s" %x, "-y", "%s" %y, "-click", "%s" %click]) else: try: from pymouse import PyMouse m = PyMouse() m.move(0,0) # hack to init PyMouse -- still needed except ImportError as e: print "Error: %s" %(e) if click: m.click(x,y) else: m.move(x,y)
def mousePressEvent (self, event): if event.button() == QtCore.Qt.LeftButton: self.parent.hide() pos=event.pos() mouse = PyMouse() time.sleep(0.2) mouse.click(pos.x(), pos.y()) # parent is PositionSelectionUI self.parent.callTimerAndRun() self.parent.timer_window.move(pos.x() + 320, pos.y() - 380) QtGui.QWidget.mousePressEvent(self, event)
class RepClick(PyMouseEvent): def __init__(self): PyMouseEvent.__init__(self) self.mouse = PyMouse() self.first = True def click(self, x, y, button, press): if self.first and button == 1 and press: self.first = False x,y = self.mouse.position() delay = 1.0/CLICKS_PER_SEC for i in xrange(REPEATS): self.mouse.click(x, y) sleep(delay) self.stop()
def show_mouse_position(): # import the module from pymouse import PyMouse m = PyMouse() m.move(200, 200) # click works about the same, except for int button possible values are 1: left, 2: right, 3: middle m.click(500, 300, 1) # get the screen size m.screen_size() # (1024, 768) # get the mouse position m.position()
def fill_board(start_board, solution_board, x,y,width,height): cell_width = width / 9 cell_height = height / 9 mouse = PyMouse() keyboard = PyKeyboard() for i in range(0,9): for j in range(0,9): # insert only empty cell if start_board.values[j][i] == 0: sleep(0.1) mouse.click(x + cell_width*(j+0.5), y+cell_height*(i+0.5)) sleep(0.01) keyboard.tap_key(str(solution_board.values[j][i]))
class MouseController: def __init__(self): self.mouse = PyMouse() def set_mouse_position(self,x,y): self.mouse.move(x,y) def get_mouse_position(self): return self.mouse.position() def get_screen_size(self): return self.mouse.screen_size() #x,y, button possible values are 1: left, 2: middle, 3: right def click_at_location(self,x,y,button_id): self.mouse.click(x,y,button_id)
class PointerNode(Node): nodeName = "Pointer" def __init__(self, name): terminals = { 'irXIn': dict(io='in'), 'irYIn': dict(io='in'), 'bPressed': dict(io='in'), 'aPressed': dict(io='in'), 'bRel': dict(io='in'), } self.mouse = PyMouse() self.screenSize = self.mouse.screen_size() self.dragging = False Node.__init__(self, name, terminals=terminals) def process(self, **kwds): self.irX = kwds['irXIn'] self.irY = kwds['irYIn'] self.isBPressed = kwds['bPressed'] self.isAPressed = kwds['aPressed'] self.isBReleased = kwds['bRel'] # check if B button is pressed and ir data is not zero if self.isBPressed and self.irX != 0 and self.irY != 0: # recalculate the x and y pos of the mouse depending on the ir data and screen size xScreen = self.screenSize[0] - int((self.screenSize[0] / 1024) * self.irX) yScreen = int((self.screenSize[1] / 760) * self.irY) # check if x and y data is valid and move mouse to pos if xScreen <= self.screenSize[0] and xScreen >= 0 and yScreen <= self.screenSize[1] and yScreen >= 0: self.mouse.move(xScreen, yScreen) # when b is released and object is dragged, object is released if self.isBReleased and self.dragging: self.mouse.click(self.mouse.position()[0], self.mouse.position()[1]) self.dragging = False # when b is not pressed and a is pressed, do a single click if self.isAPressed and not self.isBPressed: self.mouse.click(self.mouse.position()[0], self.mouse.position()[1]) self.dragging = False # when b and a are pressed, do drag elif self.isAPressed and self.isBPressed: self.mouse.press(self.mouse.position()[0], self.mouse.position()[1]) self.dragging = True
class ScreenShot(Thread): # CONFIGURATION SCALE_RATIO_X = 1.4 SCALE_RATIO_Y = 2 MAX_SCREENSHOT_COUNT = 5 # DELAY IN SECONDS DELAY_BETWEEN_SCREENSHOTS = 2.0 slide_counter = 0 def __init__(self, event): Thread.__init__(self) # set wx for screenshot self.screen = wx.ScreenDC() self.size = self.screen.GetSize() self.bmp = wx.EmptyBitmap(self.size[0], self.size[1]) # set mouse click settings self.stopped = event self.mouse = PyMouse() self.width, self.height = self.mouse.screen_size() self.set_coordinates() def set_coordinates(self): self.x = self.width / self.SCALE_RATIO_X self.y = self.height / self.SCALE_RATIO_Y def perform_click(self): self.mouse.click(self.x, self.y) def take_screenshot(self): mem = wx.MemoryDC(self.bmp) mem.Blit(0, 0, self.size[0], self.size[1], self.screen, 0, 0) del mem self.bmp.SaveFile('screenshot{0}.png'.format(self.slide_counter), wx.BITMAP_TYPE_PNG) def run(self): while not self.stopped.wait(self.DELAY_BETWEEN_SCREENSHOTS): self.slide_counter += 1 self.perform_click() self.take_screenshot() if self.slide_counter > self.MAX_SCREENSHOT_COUNT: self.stopped.set()
class Mouse: def __init__(self): self.m = PyMouse() self.suppress_xlib_output(2) def goto_init(self): self.m.click(65, 148) def get_mouse_position(self): return self.m.position() @staticmethod def suppress_xlib_output(num): for i in range(num): print '\r\033[1A' + 46 * ' ', print
class DotaCamera: """ API for controlling a Dota 2 replay camera. Since Dota 2 has no way to control a replay camera directly, we simulate this by using Python mouse events to click on the minimap. For now, this supports only 2560x1440 resolution. """ MAP_SIZE = (17408, 15872) #MINIMAP = (14, 1080, 361, 343) MINIMAP = (1000, 500, 100, 100) def __init__(self): """ Initialize with the specified screen resolution width and height. """ self.mouse = PyMouse() def set(self, x, y): """ Sets the camera to the specified location. Args: x the x coordinate in game units y the y coordinate in game units """ MINIMAP = DotaCamera.MINIMAP MAP_SIZE = DotaCamera.MAP_SIZE # y actually starts at bottom, not top y = -y # Translate coordinates x += MAP_SIZE[0] / 2 y += MAP_SIZE[1] / 2 # Scale to minimap coordinates new_x = (float(x) / MAP_SIZE[0]) * MINIMAP[2] + MINIMAP[0] new_y = (float(y) / MAP_SIZE[1]) * MINIMAP[3] + MINIMAP[1] self.mouse.click(new_x, new_y)
class Mouse(object): """ Wrapper to handle the computer interface. """ def __init__(self): super(Mouse, self).__init__() self.m = PyMouse() self.x, self.y = self.m.position() def move(self, end_x, end_y): ''' Takes in values that you want to offset ''' end_x += self.x end_y += self.y x_diff = (end_x - self.x) y_diff = (end_y - self.y) steps = max(x_diff,y_diff) if steps != 0: x_step = float(x_diff) / steps y_step = float(y_diff) / steps step = 0 while step < steps: self.x += x_step self.y += y_step self.m.move(self.x,self.y) time.sleep(self.time_delay) step += 1 def click(self): self.m.click(self.x,self.y,1) def right_click(self): self.m.click(self.x,self.y,2) x = 0 y = 0 time_delay = .0005
def send_cast(self, query): w = gtk.gdk.get_default_root_window() sz = w.get_size() if query is not '': x,y,btn = query.split("&") m = PyMouse() x = int(sz[0] * float(x)) y = int(sz[1] * float(y)) m.click(x, y, int(btn)) print "The size of the window is %d x %d" % sz pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1]) pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1]) ab = pb.scale_simple(sz[0]/2, sz[1]/2, gtk.gdk.INTERP_NEAREST) fH = cStringIO.StringIO() ab.save_to_callback(fH.write, "jpeg", {"quality": '100'}) if (ab != None): # ab.save("screenshot.jpeg","jpeg", {"quality": '60'}) return base64.b64encode(fH.getvalue()) else: return ""
def mouseControl(): """ Control the mouse using glove accelerometer """ m = PyMouse() x_dim,y_dim = m.screen_size() m.move(x_dim/2,y_dim/2) while True: x_pos = (x_dim/2) + int(-share_var.yaw*x_dim/90.0) y_pos = (y_dim/2) + int(-share_var.pitch*y_dim/90.0) # if (x_pos < 0): x_pos = 0 # if (x_pos > x_dim): x_pos = x_dim # if (y_pos < 0): y_pos = 0 # if (y_pos > y_dim): y_pos = y_dim m.move(x_pos, y_pos) if share_var.flexIndexFinger > 45: m.click(x_pos,y_pos)
w = Gdk.get_default_root_window() pb = Gdk.pixbuf_get_from_window(w, x, y, 1, 1) x = int.from_bytes(pb.get_pixels(), "big") return x m = PyMouse() ft = 0 while True: k = screenshotocr(filename, 593, 60, 486, 60) print(k) if (k == "Duo video call" or k == "Duo voice call"): print("Ahoy") if (ft == 0): ft = 1 b = screenshotocr(filename1, 593, 110, 486, 150) print(b) #check if caller is valid contact if b == "AAAA" or b == "BBBB" or b == "ILT": print("Valid call") #answer call m.click(894, 1102) else: ft = 0 sleep(2.5)
class Bot(object): """Mouse and Keyboard robot class. Abbreviation table: - m: stands for mouse - k: stands for keyboard - dl: stands for delay - n: how many times you want to tap the key - i: usually for the ith function key, F1 ~ F12 Almost every method have an option keyword ``dl`` (dl stands for delay), there is ``dl`` seconds delay applied at begin. By default it is ``None``, means no delay applied. Keyboard Key Name Table (Case Insensitive):: # Main Keyboard Keys "ctrl": self.k.control_key, "l_ctrl": self.k.control_l_key, "r_ctrl": self.k.control_r_key, "alt": self.k.alt_key, "l_alt": self.k.alt_l_key, "r_alt": self.k.alt_r_key, "shift": self.k.shift_key, "l_shift": self.k.shift_l_key, "r_shift": self.k.shift_r_key, "tab": self.k.tab_key, "space": self.k.space_key, "enter": self.k.enter_key, "back": self.k.backspace_key, "backspace": self.k.backspace_key, # Side Keyboard Keys "home": self.k.home_key, "end": self.k.end_key, "page_up": self.k.page_up_key, "pageup": self.k.page_up_key, "page_down": self.k.page_down_key, "page_down": self.k.page_down_key, "insert": self.k.insert_key, "ins": self.k.insert_key, "delete": self.k.delete_key, "del": self.k.delete_key, "up": self.k.up_key, "down": self.k.down_key, "left": self.k.left_key, "right": self.k.right_key, # Function Keys "f1": F1 """ def __init__(self): self.m = PyMouse() self.k = PyKeyboard() self.dl = 0 self._key_mapper = { # Main Keyboard Keys "ctrl": self.k.control_key, "l_ctrl": self.k.control_l_key, "r_ctrl": self.k.control_r_key, "alt": self.k.alt_key, "l_alt": self.k.alt_l_key, "r_alt": self.k.alt_r_key, "shift": self.k.shift_key, "l_shift": self.k.shift_l_key, "r_shift": self.k.shift_r_key, "tab": self.k.tab_key, "space": self.k.space_key, "enter": self.k.enter_key, "back": self.k.backspace_key, "backspace": self.k.backspace_key, # Side Keyboard Keys "home": self.k.home_key, "end": self.k.end_key, "page_up": self.k.page_up_key, "pageup": self.k.page_up_key, "page_down": self.k.page_down_key, "page_down": self.k.page_down_key, "insert": self.k.insert_key, "ins": self.k.insert_key, "delete": self.k.delete_key, "del": self.k.delete_key, "up": self.k.up_key, "down": self.k.down_key, "left": self.k.left_key, "right": self.k.right_key, # f1 - f12 is the function key } for i in range(1, 1 + 12): self._key_mapper["f%s" % i] = self.k.function_keys[i] def _parse_key(self, name): name = str(name) if name in string.printable: return name elif name.lower() in self._key_mapper: return self._key_mapper[name.lower()] else: raise ValueError("%r is not a valid key name, use one of %s." % (name, list(self._key_mapper))) def delay(self, dl=0): """Delay for ``dl`` seconds. """ if dl is None: time.sleep(self.dl) elif dl < 0: sys.stderr.write( "delay cannot less than zero, this takes no effects.\n") else: time.sleep(dl) #--- Meta --- def get_screen_size(self): """Return screen's width and height in pixel. **中文文档** 返回屏幕的像素尺寸。 """ width, height = self.m.screen_size() return width, height def get_position(self): """Return the current coordinate of mouse. **中文文档** 返回鼠标所处的位置坐标。 """ x_axis, y_axis = self.m.position() return x_axis, y_axis #--- Mouse Macro --- def left_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Left click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处左键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 1, n) self.delay(post_dl) def right_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Right click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处右键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 2, n) self.delay(post_dl) def middle_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Middle click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处中键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 3, n) self.delay(post_dl) def scroll_up(self, n, pre_dl=None, post_dl=None): """Scroll up ``n`` times. **中文文档** 鼠标滚轮向上滚动n次。 """ self.delay(pre_dl) self.m.scroll(vertical=n) self.delay(post_dl) def scroll_down(self, n, pre_dl=None, post_dl=None): """Scroll down ``n`` times. **中文文档** 鼠标滚轮向下滚动n次。 """ self.delay(pre_dl) self.m.scroll(vertical=-n) self.delay(post_dl) def scroll_right(self, n, pre_dl=None, post_dl=None): """Scroll right ``n`` times. **中文文档** 鼠标滚轮向右滚动n次(如果可能的话)。 """ self.delay(pre_dl) self.m.scroll(horizontal=n) self.delay(post_dl) def scroll_left(self, n, pre_dl=None, post_dl=None): """Scroll left ``n`` times. **中文文档** 鼠标滚轮向左滚动n次(如果可能的话)。 """ self.delay(pre_dl) self.m.scroll(horizontal=-n) self.delay(post_dl) def move_to(self, x, y, pre_dl=None, post_dl=None): """Move mouse to (x, y) **中文文档** 移动鼠标到 (x, y) 的坐标处。 """ self.delay(pre_dl) self.m.move(x, y) self.delay(post_dl) def drag_and_release(self, start_x, start_y, end_x, end_y, pre_dl=None, post_dl=None): """Drag something from (start_x, start_y) to (end_x, endy) **中文文档** 从start的坐标处鼠标左键单击拖曳到end的坐标处 start, end是tuple. 格式是(x, y) """ self.delay(pre_dl) self.m.press(start_x, start_y, 1) self.m.drag(end_x, end_y) self.m.release(end_x, end_y, 1) self.delay(post_dl) #--- Keyboard Single Key --- def tap_key(self, key_name, n=1, interval=0, pre_dl=None, post_dl=None): """Tap a key on keyboard for ``n`` times, with ``interval`` seconds of interval. Key is declared by it's name Example:: bot.tap_key("a") bot.tap_key(1) bot.tap_key("up") bot.tap_key("space") bot.tap_key("enter") bot.tap_key("tab") **中文文档** 以 ``interval`` 中定义的频率按下某个按键 ``n`` 次。接受按键名作为输入。 """ key = self._parse_key(key_name) self.delay(pre_dl) self.k.tap_key(key, n, interval) self.delay(post_dl) def enter(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press enter key n times. **中文文档** 按回车键/换行键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.enter_key, n, interval) self.delay(post_dl) def backspace(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press backspace key n times. **中文文档** 按退格键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.backspace_key, n, interval) self.delay(post_dl) def space(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press white space key n times. **中文文档** 按空格键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.space_key, n) self.delay(post_dl) def fn(self, i, n=1, interval=0, pre_dl=None, post_dl=None): """Press Fn key n times. **中文文档** 按 Fn 功能键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.function_keys[i], n, interval) self.delay(post_dl) def tab(self, n=1, interval=0, pre_dl=None, post_dl=None): """Tap ``tab`` key for ``n`` times, with ``interval`` seconds of interval. **中文文档** 以 ``interval`` 中定义的频率按下某个tab键 ``n`` 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.tab_key, n, interval) self.delay(post_dl) def up(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press up key n times. **中文文档** 按上方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.up_key, n, interval) self.delay(post_dl) def down(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press down key n times. **中文文档** 按下方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.down_key, n, interval) self.delay(post_dl) def left(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press left key n times **中文文档** 按左方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.left_key, n, interval) self.delay(post_dl) def right(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press right key n times. **中文文档** 按右方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.right_key, n, interval) self.delay(post_dl) def delete(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres delete key n times. **中文文档** 按 delete 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.delete_key, n, interval) self.delay(post_dl) def insert(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres insert key n times. **中文文档** 按 insert 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.insert_key, n, interval) self.delay(post_dl) def home(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres home key n times. **中文文档** 按 home 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.home_key, n, interval) self.delay(post_dl) def end(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press end key n times. **中文文档** 按 end 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.end_key, n, interval) self.delay(post_dl) def page_up(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres page_up key n times. **中文文档** 按 page_up 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.page_up_key, n, interval) self.delay(post_dl) def page_down(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres page_down key n times. **中文文档** 按 page_down 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.page_down, n, interval) self.delay(post_dl) #--- Keyboard Combination --- def press_and_tap(self, press_key, tap_key, n=1, interval=0, pre_dl=None, post_dl=None): """Press combination of two keys, like Ctrl + C, Alt + F4. The second key could be tapped for multiple time. Examples:: bot.press_and_tap("ctrl", "c") bot.press_and_tap("shift", "1") **中文文档** 按下两个键的组合键。 """ press_key = self._parse_key(press_key) tap_key = self._parse_key(tap_key) self.delay(pre_dl) self.k.press_key(press_key) self.k.tap_key(tap_key, n, interval) self.k.release_key(press_key) self.delay(post_dl) def press_two_and_tap(self, press_key1, press_key2, tap_key, n=1, interval=0, pre_dl=None, post_dl=None): """Press combination of three keys, like Ctrl + Shift + C, The tap key could be tapped for multiple time. Examples:: bot.press_and_tap("ctrl", "shift", "c") **中文文档** 按下三个键的组合键。 """ press_key1 = self._parse_key(press_key1) press_key2 = self._parse_key(press_key2) tap_key = self._parse_key(tap_key) self.delay(pre_dl) self.k.press_key(press_key1) self.k.press_key(press_key2) self.k.tap_key(tap_key, n, interval) self.k.release_key(press_key1) self.k.release_key(press_key2) self.delay(post_dl) def ctrl_c(self, pre_dl=None, post_dl=None): """Press Ctrl + C, usually for copy. **中文文档** 按下 Ctrl + C 组合键, 通常用于复制。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("c") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_v(self, pre_dl=None, post_dl=None): """Press Ctrl + V, usually for paste. **中文文档** 按下 Ctrl + V 组合键, 通常用于粘贴。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("v") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_x(self, pre_dl=None, post_dl=None): """Press Ctrl + X, usually for cut. **中文文档** 按下 Ctrl + X 组合键, 通常用于剪切。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("x") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_z(self, pre_dl=None, post_dl=None): """Press Ctrl + Z, usually for undo. **中文文档** 按下 Ctrl + Z 组合键, 通常用于撤销上一次动作。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("z") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_y(self, pre_dl=None, post_dl=None): """Press Ctrl + Y, usually for redo. **中文文档** 按下 Ctrl + Y 组合键, 通常用于重复上一次动作。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("y") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_a(self, pre_dl=None, post_dl=None): """Press Ctrl + A, usually for select all. **中文文档** 按下 Ctrl + A 组合键, 通常用于选择全部。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("a") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_f(self, pre_dl=None, post_dl=None): """Press Ctrl + F, usually for search. **中文文档** 按下 Ctrl + F 组合键, 通常用于搜索。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("f") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_fn(self, i, pre_dl=None, post_dl=None): """Press Ctrl + Fn1 ~ 12 once. **中文文档** 按下 Ctrl + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.control_key) self.delay(post_dl) def alt_fn(self, i, pre_dl=None, post_dl=None): """Press Alt + Fn1 ~ 12 once. **中文文档** 按下 Alt + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.alt_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.alt_key) self.delay(post_dl) def shift_fn(self, i, pre_dl=None, post_dl=None): """Press Shift + Fn1 ~ 12 once. **中文文档** 按下 Shift + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.shift_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.shift_key) self.delay(post_dl) def alt_tab(self, n=1, pre_dl=None, post_dl=None): """Press Alt + Tab once, usually for switching between windows. Tab can be tapped for n times, default once. **中文文档** 按下 Alt + Tab 组合键, 其中Tab键按 n 次, 通常用于切换窗口。 """ self.delay(pre_dl) self.k.press_key(self.k.alt_key) self.k.tap_key(self.k.tab_key, n=n, interval=0.1) self.k.release_key(self.k.alt_key) self.delay(post_dl) #--- Other --- def type_string(self, text, interval=0, pre_dl=None, post_dl=None): """Enter strings. **中文文档** 从键盘输入字符串, interval是字符间输入时间间隔, 单位是秒。 """ self.delay(pre_dl) self.k.type_string(text, interval) self.delay(post_dl) def copy_text_to_clipboard(self, text): """Copy text to clipboard. **中文文档** 拷贝字符串到剪贴板。 """ pyperclip.copy(text)
def simulate_click_pc(): m = PyMouse() m.click(150, 650, 1)
# -*- coding: utf-8 -*- from pymouse import PyMouse from pykeyboard import PyKeyboard import time import datetime m = PyMouse() k = PyKeyboard() # - - - Global Variables - - - # 每個步驟與伺服器之間,等待的秒數(也就是預期的網路延遲時間) gl_lag = 0.1 # Get focus off terminal m.click(250, 350, 1) # 將伺服器延遲時間算入的最普通的正常點擊方式 def click_then_wait(position, sleep_sec): """ 點擊 position 之後睡眠 sleep_sec 秒,給予伺服器緩衝時間 """ click_no_wait(position) time.sleep(sleep_sec) def click_no_wait(position): print get_time_stamp() + "click_no_wait(): [" + str(position[0]) + "," + str(position[1]) + "]" m.click(position[0], position[1]) def get_time_stamp(): current_time = str(datetime.datetime.now()).split('.')[0] # 2015-03-29 20:18:08
import random, time m = PyMouse() try: size = m.screen_size() print "size: %s" % (str(size)) pos = (random.randint(0, size[0]), random.randint(0, size[1])) except: pos = (random.randint(0, 250), random.randint(0, 250)) print "Position: %s" % (str(pos)) print 'move' m.move(pos[0], pos[1]) time.sleep(2) print 'click left' m.click(pos[0], pos[1], 1) time.sleep(2) print 'click right' m.click(pos[0], pos[1], 2) time.sleep(2) print 'click middle' m.click(pos[0], pos[1], 3)
def handle_home(self, message): keyboard = Controller() m = PyMouse() right = Key.right left = Key.left reminder = message.data['reminder'] reminder = (' ' + reminder).replace(' my ', ' your ').strip() reminder = (' ' + reminder).replace(' our ', ' your ').strip() # self.settings['wigh_reminders'] = [] # return if 'wigh_reminders' in self.settings: self.settings['wigh_reminders'].append(reminder) if len(self.settings['wigh_reminders']) > 1: self.speak_dialog('home') return else: self.settings['wigh_reminders'] = [reminder] # reminders = [] # reminders.append(message.data['reminder']) # self.speak_dialog('home') try: recognizer = cv2.face.LBPHFaceRecognizer_create() recognizer.read('/home/craghack/Documents/camera/trainer.yml') faceCascade = cv2.CascadeClassifier( "/home/craghack/Documents/camera/haarcascade_frontalface_default.xml" ) #font = cv2.FONT_HERSHEY_SIMPLEX names = ['unknown'] ids = open("/home/craghack/Documents/camera/ids.txt", "r") for line in ids: names.append(line.split(' ')[2].rstrip()) ids.close() # Initialize and start realtime video capture cam = cv2.VideoCapture(0) #cam.set(3, 640) # set video widht #cam.set(4, 480) # set video height minW = 0.1 * cam.get(3) minH = 0.1 * cam.get(4) emoji = None cmd = subprocess.check_output( "wmctrl -l | grep \".png\" | cut -b 23-", shell=True).decode("utf-8").rstrip() if not cmd == "": emoji = cmd.split('.')[0] os.system("wmctrl -a \"" + cmd + "\"") else: emoji = "suspicious" os.system( "xdg-open /home/craghack/Downloads/emojis/suspicious.png && sleep 1" ) sleep(.25) m.click(350, 460) sleep(.25) m.move(799, 479) if emoji == "happy": keyboard.press(left) keyboard.release(left) emoji = "suspicious" elif emoji == "suprised": keyboard.press(right) keyboard.release(right) emoji = "suspicious" # self.speak_dialog('detection.face') self.speak_dialog('home') confident = False count = 0 recognized = [] while not confident: rval, frame = cam.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(int(minW), int(minH))) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) id, confidence = recognizer.predict(gray[y:y + h, x:x + w]) confidence = 100 - confidence # Confidence starts backwards for some reason id = names[id - 1] if (id == "unknown") and (confidence > 50): # confident = True if emoji == "suspicious": keyboard.press(left) keyboard.release(left) sleep(.75) keyboard.press(right) keyboard.release(right) elif emoji == "happy": keyboard.press(right) keyboard.release(right) sleep(.75) keyboard.press(right) keyboard.release(right) recognized.append("unknown") self.speak_dialog('seen.face') elif (confidence > 35): # confident = True if id not in recognized: if emoji == "suspicious": keyboard.press(right) keyboard.release(right) emoji = "happy" elif emoji == "happy": keyboard.press(right) keyboard.release(right) sleep(.75) keyboard.press(left) keyboard.release(left) recognized.append(id) dialog = id + '.face' self.speak_dialog(dialog) text = "" for reminder in self.settings['wigh_reminders']: text += reminder + ", and " # self.settings['wigh_reminders'].remove(reminder) self.settings['wigh_reminders'] = [] response = {'reminder': text[:-6]} self.speak_dialog('remind', data=response) if len(recognized) != 0 and len(recognized) >= len(faces): count += 1 if (count >= 50): confident = True else: count = 0 cam.release() sleep(1) keyboard.press(Key.esc) keyboard.release(Key.esc) os.system("wmctrl -a start-mycroft.sh debug") except: self.settings['wigh_reminders'] = []
PARAM2 = 1.5 if threshold < val < 2 * threshold: return val - threshold return val while not isfile('/tmp/stop'): sample = s.get_single_sample()[1] #y = y * 0.96 + 0.04 * (sample[0] - 1.0) * (1079 / 4.0) x = m.position()[0] val = sample[8] if abs(val) > X_THRESHOLD: x -= convert(val, X_THRESHOLD) if str(x) == 'nan': continue y = m.position()[1] val = sample[7] if abs(val) > Y_THRESHOLD: y -= convert(val, Y_THRESHOLD) if abs(val) > 150: x, y = m.position() m.click(x, y) if str(y) == 'nan': continue m.move(x, y)
from pymouse import PyMouse m = PyMouse() # 获取当前位置 print(m.position()) # 移动位置 m.move(1200, 900) # 移动并且在xy位置点击,1位左键点击,2为右键点击 m.click(177, 170, 2) # 获取当前屏幕的尺寸 x, y = m.screen_size() print(x) print(y)
for x in range(15): reference_y += 1 reference_x += 1 m.move(reference_x,reference_y) time.sleep(0.005) elif (input_hex == moveDownLeft): print 'down-left' if(reference_y<yUpper and reference_x>xLower): for x in range(15): reference_y += 1 reference_x -= 1 m.move(reference_x,reference_y) time.sleep(0.005) # mouse click events elif ( input_hex == leftClick): m.press(reference_x,reference_y,1) elif ( input_hex == leftRelease): m.release(reference_x,reference_y,1) elif ( input_hex == select2): m.click(reference_x,reference_y,1) m.click(reference_x,reference_y,1)
from pymouse import PyMouse import random import time m = PyMouse() a = m.position() time.sleep(random.randint(5, 10)) m.click(8, 5) print(a)
from pymouse import PyMouse import time import random # t = random.randint(70,130) t=300 times_to_repeat = t m = PyMouse() while times_to_repeat >= 0: x = random.uniform(770,790) y = random.uniform(740,780) z1 = random.uniform(0,1) z2 = random.uniform(0,3) z = (z1+z2)/2 x_dim, y_dim = m.screen_size() m.click(x, y, 1) times_to_repeat -= 1 print('[clickin'...]') time.sleep(z) if times_to_repeat == 0: break
class AutoClicker(PyMouseEvent): def __init__(self, clicksPerSecond=5, randomizeRange=0, toggleClick=False, holdClick=True, keyToUse="Shift_L"): PyMouseEvent.__init__(self) self.isClicking = False if clicksPerSecond > 0: self.clicksPerSecond = clicksPerSecond else: raise ValueError("Clicks per second must be greater than 0.") if randomizeRange >= 0: self.randomizeRange = randomizeRange else: raise ValueError( "Random range must be greater than or equal to 0.") self.mouse = PyMouse() self.toggleClick = toggleClick self.holdClick = holdClick self.keyToUse = keyToUse #"Caps_Lock" self.initHookManager() def initHookManager(self): """ Initializes the HookManager, which the autoclicker uses to get key information globally. After this is called, self.hm refers to the HookManaer that was initialized. """ self.hm = pyxhook.HookManager() self.hm.HookKeyboard() self.hm.KeyDown = self.keyDownHandler self.hm.KeyUp = self.keyUpHandler self.hm.start() def keyDownHandler(self, hookEvent=None): """ Begins the clicking inside of a thread. """ if hookEvent.Key == self.keyToUse: if not self.isClicking: Thread(target=self.threadedAutoClick, args=(hookEvent, )).start() elif self.toggleClick: self.stopClicking() def threadedAutoClick(self, hookEvent=None): """ Auto clicks inside a thread so that it can be exited by other functions. """ self.isClicking = True while (self.isClicking): x, y = self.mouse.position() self.mouse.click(x, y, 1) sleepTime = (1.0 / self.clicksPerSecond) #For clicks per second if self.randomizeRange != 0.0: #Slight efficiency boost is useful sleepTime += random.uniform( 0.0, (self.randomizeRange / 1000.0)) #For the random delay time.sleep(sleepTime) def keyUpHandler(self, hookEvent=None): """ Handles any key being released. """ if hookEvent.Key == self.keyToUse and self.holdClick: self.stopClicking() def stopClicking(self): """ Stop the clicking """ self.isClicking = False def cleanUp(self): """ Cleans up the AutoClicker. You *must* call this when you want to any program which uses this class to end! """ self.hm.cancel() self.stopClicking()
if __name__ == "__main__": player_name = 'Bakambu' BIN = 1400 SellPrice = 1700 time.sleep(10) contToReList = 0 marketplace() typ.typing(player_name, BIN) while True: time.sleep(10) search_player() if is_pixel_white(690, 565): buy() sell(SellPrice) marketplace() else: m.click(714, 568, 1) ## player not found (click OK) if (contToReList >= 20): relist() contToReList = 0 marketplace() typ.typing(player_name, BIN) else: contToReList = contToReList + 1
# 南 sleep(1) m.press(850, 100) sleep(1) m.move(410, 470) sleep(1) m.release(410, 470) # 确定布局 sleep(1) m.click(680, 170) def comform_prize(): sleep(1) m.click(680, 450) sleep(1) if __name__ == '__main__': # 焦点回到游戏 count = 0 m = PyMouse() sleep(1) m.click(50, 110) # 自动刷 FB auto_fb() # 生存试炼 # live_training()
import cv2 from pymouse import PyMouse def process_image(image): original_image = image # Grayscale processed_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Edge Detection processed_image = cv2.Canny(image, threshold1=200, threshold2=300) return processed_image def main(): while (True): # This grabs the window window = np.array(ImageGrab.grab(bbox=(0, 40, 800, 640))) new_screen = process_image(window) cv2.imshow('window', new_screen) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break # main() # PyMouse is to programmatically click things mouse = PyMouse() x_dim, y_dim = mouse.screen_size() mouse.click(x_dim / 2, y_dim / 2, 1) mouse.click(x_dim / 2, y_dim / 2, 1)
from pymouse import PyMouse from time import sleep if __name__ == '__main__': m = PyMouse() pos = m.position() xPos = pos[0] yPos = pos[1] while True: m.click(xPos, yPos, 3) sleep(0.02)
# time.sleep( 3 ) while True: current_time = time.localtime() stamp = time.strftime('%Y-%m-%d-%H:%M:%S', current_time) print stamp start_time = "2018-10-12-23:12:00" # start_time = "2018-10-13-00:01:00" if stamp > start_time : print 'start battle ' m.click( 60, y_dim - 20, 1) time.sleep( 0.1 ) # m.click( 290, y_dim/2+50, 1) # battle wild BOSS # m.click( 290+50, y_dim/2+50, 1) # battle guild # m.click( 290, y_dim/2+250, 1) # nation battle # m.click( 290, y_dim/2+125, 1); time.sleep(25) # lv bu # m.click( 290, y_dim/2+50, 1); time.sleep(25) # huang cheng # tower() country() # legendInstance() time.sleep( 3 ) m.click( 430, y_dim*0.67, 1) # skip time.sleep( 2 ) m.click( 430, y_dim*0.8, 1) # skip time.sleep( 2 ) else:
class KM: """ 这里存放所有模拟鼠标、键盘事件 """ def __init__(self): self.m = PyMouse() self.k = PyKeyboard() def open_wifi(self): # 打开wifi,暂时在非锁屏转态下才可用 self.m.move(1600, 870) time.sleep(1) self.m.click(1500, 860) time.sleep(1) self.m.click(1500, 850) time.sleep(1) self.k.tap_key(self.k.escape_key) return 'wifi已打开/关闭' def thunder_res(self): # 迅雷重启线程,注意迅雷X需要最大化,图标放最下角,第7个 self.m.move(1314, 863) time.sleep(1.6) self.m.click(1312, 850) time.sleep(1) self.m.click(1274, 817) time.sleep(3) self.m.click(336, 160) time.sleep(10) self.m.click(336, 160) time.sleep(2) self.m.click(1511, 25) time.sleep(0.5) screen_size = self.m.screen_size() self.m.move(int(screen_size[0] / 2), int(screen_size[1] / 2)) self.m.click(int(screen_size[0] / 2), int(screen_size[1] / 2)) return '迅雷已重启任务'
class BookingBot(object): def __init__(self, user_name, user_pwd): self._user_name = user_name self._user_pwd = user_pwd self.keyboard = PyKeyboard() self.mouse = PyMouse() def start_wechat(self): # itchat.send('start booking bot, input:help, get info', 'filehelper') self._book_day = time.localtime(time.time()).tm_mday self._book_hour = time.localtime(time.time()).tm_hour self._sport_name = 'basketball' self._sport_day = 7 self._sport_hour = [21] @itchat.msg_register([itchat.content.TEXT]) def chat_trigger(msg): if msg['Text'] == 'help': itchat.send(u'input your user_name', 'filehelper') itchat.send(u'example:u=txf', 'filehelper') itchat.send(u'input your user_pwd', 'filehelper') itchat.send(u'example:p=123', 'filehelper') itchat.send(u'input booking day', 'filehelper') itchat.send(u'example:bd=15', 'filehelper') itchat.send(u'input booking hour', 'filehelper') itchat.send(u'example:bh=8', 'filehelper') itchat.send(u'input sport name', 'filehelper') itchat.send(u'example:sn=basketball', 'filehelper') itchat.send(u'input sport day', 'filehelper') itchat.send(u'example:sd=7', 'filehelper') itchat.send(u'input sport hour', 'filehelper') itchat.send(u'example:sh=21 19 15', 'filehelper') itchat.send(u'start booking, input:start', 'filehelper') elif 'u=' in msg['Text']: self._user_name = msg['Text'][2:] itchat.send(u'record your name', 'filehelper') elif 'p=' in msg['Text']: self._user_pwd = msg['Text'][2:] itchat.send(u'record your password', 'filehelper') elif 'bd=' in msg['Text']: self._book_day = int(msg['Text'][3:]) itchat.send(u'record booking day: {}'.format(self._book_day), 'filehelper') elif 'bh=' in msg['Text']: self._book_hour = int(msg['Text'][3:]) itchat.send(u'record booking hour: {}'.format(self._book_hour), 'filehelper') elif 'sn=' in msg['Text']: self._sport_name = msg['Text'][3:] itchat.send(u'record sport name: {}'.format(self._sport_name), 'filehelper') elif 'sd=' in msg['Text']: self._sport_day = int(msg['Text'][3:]) itchat.send(u'record sport day: {}'.format(self._sport_day), 'filehelper') elif 'sh=' in msg['Text']: in_msg = msg['Text'][3:] itchat.send(u'record sport hours: {}'.format(in_msg), 'filehelper') in_msg = in_msg.split(' ') hour_list = [int(hour) for hour in in_msg] self._sport_hour = hour_list elif msg['Text'] == u'start': assert self._user_name is not None assert self._user_pwd is not None assert self._book_day is not None assert self._book_hour is not None assert self._sport_name is not None assert self._sport_day is not None assert self._sport_hour is not None while True: daytime = time.localtime(time.time()).tm_mday hourtime = time.localtime(time.time()).tm_hour mintime = time.localtime(time.time()).tm_min print('Now time: {} day {} hour {} min'.format( daytime, hourtime, mintime)) if daytime == self._book_day and hourtime == self._book_hour: if self.book(self._sport_hour, self._sport_name, self._sport_day) == 1: itchat.send(u'booking successful', 'filehelper') break else: itchat.send(u'no available time', 'filehelper') self._log_failed() itchat.send_image('failed.jpg', 'filehelper') break else: time.sleep(60) itchat.auto_login(hotReload=True) # itchat.auto_login() itchat.run() def _login(self): self.browser = webdriver.Firefox() self.browser.set_window_size(1920, 1080) self.browser.set_window_position(0, 0) self.browser.get('http://www.cityu.edu.hk/sfbi-std/fbi_fp.htm') time.sleep(2) self.keyboard.type_string(self._user_name) self.keyboard.tap_key(self.keyboard.tab_key) self.keyboard.type_string(self._user_pwd) self.keyboard.tap_key(self.keyboard.enter_key) time.sleep(2) def book(self, sport_hours, sport_name='basketball', sport_day=7): self._login() self._choose_day(sport_day) self._choose_sport(sport_name) time.sleep(3) labels = self._get_access_lable() for hour in sport_hours: if labels[hour - 8]: self._choose_hour(hour) break else: print('Sorry, no available time') self.browser.close() time.sleep(30) return 0 self._confirm_booking() self.browser.close() return 1 def _choose_day(self, day): assert day < 8 assert day >= 0 curr_wday = time.localtime(time.time()).tm_wday booking_day = curr_wday + day + 1 if booking_day > 13: booking_day -= 13 if booking_day > 6: pos = self._get_day_pos(1, (booking_day - 7)) else: pos = self._get_day_pos(0, booking_day) self._click(pos[0], pos[1]) time.sleep(1) def _choose_sport(self, sport): # you need to add the sport you want if sport == 'basketball': self._click(1075, 460) time.sleep(1) def _choose_hour(self, hour): hour = hour - 8 pos = self._get_hour_pos(hour) self._click(pos[0], pos[1]) time.sleep(1) def _confirm_booking(self): self._click(377, 600) self.keyboard.type_string(self._user_pwd) self.keyboard.tap_key(self.keyboard.enter_key) def _click(self, x, y): self.mouse.move(x, y) time.sleep(1) self.mouse.click(x, y, 1, 3) def _get_day_pos(self, row, column): return [300 + column * 27, 470 + row * 30] def _get_hour_pos(self, row): return [368, 316 + row * 23] def _get_access_lable(self): img = ImageGrab.grab() b, g, r = img.split() img = Image.merge("RGB", (r, g, b)) img = np.array(img, dtype=np.uint8) roi = img[315:624, 350:379] flags = [] for i in range(14): sub_roi = roi[i * 22:(i + 1) * 22, :] blue = sub_roi[sub_roi.shape[0] // 2, sub_roi.shape[1] // 2, 0] if blue > 120: flag = True else: flag = False flags.append(flag) return flags def _log_failed(self): img = ImageGrab.grab() img = np.array(img, dtype=np.uint8) screen_roi = img[:1080, :1920] img = Image.fromarray(screen_roi) img.save('failed.jpg')
#!/bin/python from pymouse import PyMouse import time k = ('左', '右', '中') m = PyMouse() t = int(input('输入点击总次数(0=不限制,慎用!):')) if (t == 0): print('[W]您设定了不限制,您需要考虑您是否有办法关闭它!') d = float(input('输入两次点击之间的间隔(单位为秒,可以为0):')) if (d < 0.005): print('[W]您设定的间隔很小,您需要考虑您的计算机或要点击的东西是否可以承受它。') p = int(input('点击的键(1=左键,2=右键,3=中键):')) print('[I]全部设置完毕!') print('[I]你有5秒的时间移动鼠标到想要连点的地方或关闭点击器。') for i in range(5): print('[I]剩余', 5 - i, 'S') time.sleep(1) print('[I]Start to click.') s = time.time() i = 0 while (i < t or t == 0): m.click(m.position()[0], m.position()[1], p) i = i + 1 # print('[D]TIME =',time.asctime(time.localtime(time.time())),' ','POSITION =',m.position(),' ','TOTAL =',i) #中二装B疯狂输出模式。 time.sleep(d) e = time.time() print('[S]点击', k[p - 1], '键', t, '次,点击间隔为', d, '的任务成功结束,共用时约', round(e - s, 2), '秒,平均每秒约点击', round(t / (e - s), 2), '次。')
# @Time : 2019/9/4 9:01 # @Author : WZG # --coding:utf-8-- import time from pymouse import PyMouse #from pykeyboard import PyKeyboard #k = PyKeyboard() m = PyMouse() a = m.position() print(a) time.sleep(74000) m.click(1052, 329) m.click(1052, 329) while True: time.sleep(86401) m.click(1052, 329) m.click(1052, 329) # k.type_string('hello')
class Bot: MOVE_OKAY = 0 #Enumerated values for movement results MOVE_FAIL = 1 MOVE_INPR = 2 OBT_WT = 0.01 #Wait times for detection loops ENT_WT = 0.01 LWT_WT = 0.01 def AttackPos(self, pc): ''' Perform an attack on a given position ''' l, t, _, _ = self.sv.GetWindowPos() pc = (pc[0] + l, pc[1] + t) self.MouseMove(self.m.position(), pc) self.k.press_key('w') time.sleep(np.random.rand() / 2) self.k.release_key('w') print('Attack: ' + str(pc)) def __init__(self, name): self.wname = name #Name of the game window self.sv = ScreenViewer() #For getting screens of the game self.mm = MovementMap() #Bot's internal map of the world self.ts = TargetingSystem(m=7, n=9, ss=(800, 600), sb=(4, 0, 4, 12), cp=(400, 274), forceTrain=False) #self.ts = TargetingSystem(m = 6, n = 9, ss = (1278, 786), cp = (642, 342)) self.m = PyMouse() #PyMouse object for triggering mouse input self.k = PyKeyboard() #For triggering keyboard self.pm = ProjMap(800, 600) #Convert between 3d and 2d coords self.p = None #The current path of the bot self.pt = None #The current type of the path (to frontier, item, or home) self.pind = None #The next element to visit in the path self.p self.ghf = False #Go home flag self.cpi = 0 #Current potion index self.ssn = 0 #Debug: screenshot number self.mfc = 0 #Debug: movement flag count self.lwc = 0 #LW detection count self.hp = None #Home position self.pHealth, self.pMana = 0, 0 #Player health and mana self.nmmpm = 1 #Number of mouse move points multiplier self.ctl = False #Continue target loop flag self.ecp = [] #Enemy cell positions self.tlMut = Lock() self.tlut = None #Targeting loop update time self.tllct = None #Targeting loop last check time self.lwMut = Lock() #Mutex lock for checking if LW is happening self.clwl = False #Continue LW detection loop self.lwof = False #Lightning-warp occuring flag self.lwuo = False #Lightning-warp update occured self.obMut = Lock() self.cobl = False self.pct = [] #Shared grid position list self.pctlu = None #PCT last update time self.pctlg = None #PCT last get time def __del__(self): pass def ClickOnDoor(self): print('At door; reset instance!') raise NotImplementedError('ClickOnDoor not implemented.') def CPE(self): if self.p is None: return None return self.p[self.pind] def GetEnemyPositions(self): ''' Gets a current list of enemy positions in a thread safe manner ''' self.tlMut.acquire() lut = self.tlut #Last update time for enemy cell positions rv = self.ecp[:] #Make copy of current list to prevent race conditions self.tlMut.release() if self.tllct == lut: return [] #No update since last time self.tllct = lut #self.ecp has been updated; update time of last check return rv #Return copy def GetPath(self): ''' Makes a path for character ''' if self.mfc >= 4: #Moved failed 4 times; move anywhere else #print('Neighbor') self.p = self.mm.GetPathToNeighbor() self.pt = 0 elif len(self.mm.ItemPos()) > 0: #print('Item') self.p = self.mm.GetPathToItem() self.pt = 1 elif self.ghf: #Go home flag set; return to start #print('GO HOME!') self.p = self.mm.GetPathToHome() self.pt = 2 else: #Find path to frontier #print('Frontier') self.p = self.mm.GetPathToFrontier() self.pt = 0 if self.p is None: #Character doesn't know where to go #print('Path is still none.') self.p = self.mm.GetPathToNeighbor() self.pt = 0 if self.p is None: return False self.pind = max(len(self.p) - 2, 0) return True def GetPlayerPos(self): ''' Returns the current position of the player in 3d coords ''' cppl = self.ts.CharPos() #Get pixel location of character on screen return self.pm.Solve3DT( cppl[0], cppl[1]) #Get 3d location corresponding to pixel location def GetScreenDifference(self): I1, its = self.sv.GetScreenWithTime() #self.k.type_string('z') #Toggle item highlight while True: #Loop until new screen is available time.sleep(0.05) I2, its2 = self.sv.GetScreenWithTime() if its != its2: break #self.k.type_string('z') #Toggle item highlight #imsave('DI/' + str(self.ssn) + '.jpg', I1.astype(np.uint8)) #self.ssn += 1 #imsave('DI/' + str(self.ssn) + '.jpg', I2.astype(np.uint8)) #self.ssn += 1 R = np.where(np.abs(I1 - I2) >= 16, I2, 0) return R def GetScreenPredictions(self): self.lwMut.acquire() rv = self.pct[:] lut = self.pctlu self.lwMut.release() if self.pctlg == lut: return [] #No update since last time return rv def GoToPosition(self, pc): ''' #Click on a given pixel coordinate ''' l, t, _, _ = self.sv.GetWindowPos() pc = (pc[0] + l, pc[1] + t) self.MouseMove(self.m.position(), pc) self.k.type_string('r') #Use lightning warp to move to cell time.sleep(0.1) def GoToPosition2(self, pc): ''' Click on a given pixel coordinate ''' l, t, _, _ = self.sv.GetWindowPos() pc = (pc[0] + l, pc[1] + t) self.MouseMove(self.m.position(), pc) self.k.type_string('r') #Use lightning warp to move to cell mf = False r = 0 for j in range(64): #Detect lightning-warp animation r += self.ts.DetectLW(self.sv.GetScreen(), self.ts.CharPos()) if r > 2: print('LW1 Break!! ' + str(j)) mf = True break time.sleep(0.005) if not mf: return False nllw = 20 #Number of past DetectLW calls to remember lflw = np.ones((nllw)) #Result of last nllw DetectLW calls for j in range(256): lflw[j % nllw] = self.ts.DetectLW(self.sv.GetScreen(), self.ts.CharPos()) if lflw.sum() < 1: print('LW2 Break!! ' + str(j)) break time.sleep(0.005) return True def IsMoving(self): self.lwMut.acquire() rv = (self.lwof, self.lwuo) self.lwMut.release() return rv def LWDetectLoop(self): nllw = 20 #Number of past DetectLW calls to remember lwdt = 10 #Lightning-warp detection threshold lflw = np.zeros((nllw)) #Result of last nllw DetectLW calls i = 0 #Index into lflw tlwof = False while self.clwl: #LWPM scales prediction result based on time since LW button was pressed lflw[i] = self.ts.DetectLW(self.sv.GetScreen()) print(str(lflw[i])) i = (i + 1) % nllw #Use lflw as a circular array tlwof = lflw.sum() > lwdt self.lwMut.acquire() if not tlwof and self.lwof: #Update occurs when bot stops moving self.lwuo = True #Lightning-warp update occured self.lwof = tlwof self.lwMut.release() time.sleep(Bot.LWT_WT) def LWTest(self): ''' Used to determine delay for lightning-warp ''' self.sv.GetHWND(self.wname) self.sv.Start() l, t, _, _ = self.sv.GetWindowPos() pc = (700 + l, 150 + t) self.MouseMove(self.m.position(), pc) st = time.time() self.k.type_string('r') ftt, ltt = -1, -1 for _ in range(10): res = self.ts.DetectLW(self.sv.GetScreen()) print(str(time.time()) + ': ' + str(res)) if res and ftt == -1: ftt = time.time() if res: ltt = time.time() else: ltf = time.time() time.sleep(0.1) self.sv.Stop() print('First True: ' + str(ftt - st)) print('Last True: ' + str(ltt - st)) print('Total: ' + str(max(ltt, ltf) - st)) def MouseMove(self, sp, ep): nmmp = int(16 * self.nmmpm) if self.nmmpm > 1: self.nmmpm = 1 x = np.linspace(sp[0], ep[0], num=nmmp) y = np.linspace(sp[1], ep[1], num=nmmp) for i in range(len(x)): self.m.move(int(x[i]), int(y[i])) time.sleep(0.00005) def NextPathEle(self): self.pind -= 1 def ObjDetectLoop(self): while self.ctl: #Loop while continue targeting loop flag is set self.ts.ClassifyInput(self.sv.GetScreen( )) #Predicted cell types for portions of the screen #self.ts.DisplayPred() WP, PP = self.pm.GridIter() n = WP.shape[0] PCT = [(WP[i][0:3], CIi, self.ts.CellLookup(CIi)) for i, CIi in enumerate( self.ts.PixelToCell(PPi) for PPi in PP)] PCT = [PCTi for PCTi in PCT if PCTi[2] is not None ] #Filter out any grid cells covered by HUD self.obMut.acquire() self.pct = PCT #Update shared grid position list self.pctlu = time.time() #Update time self.obMut.release() time.sleep(Bot.OBT_WT) def PathDone(self): return self.p is None or self.pind < 0 def PickupItem(self): if self.p is None: return pp = self.ts.CharPos() dip = [(abs(pp[0] - px) + abs(pp[1] - py), (px, py)) for px, py in self.ts.GetItemLocations()] nip = len(dip) if nip == 0: print('No items found.') return dip.sort() l, t, _, _ = self.sv.GetWindowPos() for id, pc in dip[0:1]: self.k.press_key('z') self.k.press_key('f') #Depress pick-up key time.sleep(0.4) pc = (pc[0] + l, pc[1] + t) self.MouseMove(self.m.position(), pc) print('Pickup item: ' + str(pc)) self.m.click(int(pc[0]), int(pc[1]), 1) #Try to pick-up the item self.k.release_key('f') #Release pick-up key self.k.release_key('z') time.sleep(id / 512) #Wait long enough to get item self.mm.ItemRemove(self.p[0]) def Run(self): if not self.sv.GetHWND( self.wname): #Get the window handle for the game print("Failed to find window.") self.Start() cv, M_ITER = 0, 64 #Counter value and max number of loops while True: if cv >= M_ITER and not self.ghf: self.ghf = True cv = 0 if self.ghf and cv >= M_ITER: break self.pHealth = self.ts.GetHealth(self.sv.GetScreen()) self.pMana = self.ts.GetMana(self.sv.GetScreen()) if self.pHealth < 0.75: #Life is lower than 75%; use potion self.UseHealthPotion() if self.pMana < 0.25: #Mana is lower than 25%; user potion self.UseManaPotion() MSF = self.UpdatePosition() if MSF == Bot.MOVE_INPR: #Bot is still moving; don't try to move now print('Moving.') time.sleep(0.05) continue if MSF == Bot.MOVE_OKAY: #Predict open/obstacle if character moved cv += 1 print("Moved to: " + str(self.CPE())) winsound.Beep(600, 250) for WP, CI, PCTi in self.GetScreenPredictions(): self.mm.InsertCellPrelim(WP, PCTi, (not self.ts.IsEdgeCell(*CI)) or (PCTi == 'C')) if self.mm.InsertFinished( ): #Insert cells into map; may shift player location self.ShiftMap( ) #Translate projection to match new location self.p = None #Need to recompute path from new location if MSF == Bot.MOVE_FAIL: print("Move Failed.") winsound.Beep(440, 250) ecp = self.GetEnemyPositions() for ecpi in ecp: #Attack enemies self.AttackPos(tuple(int(k) for k in ecpi.round())) #Check for items on the screen. Doesn't work well!! #self.PickupItem() if len(ecp) > 0: self.SlowMouseMove() if self.PathDone(): #If there is no route current; find a new path if self.ghf and self.mm.AtHome(): self.ClickOnDoor() #Back to start elif self.pt == 1: self.PickupItem() #Pickup item on screen if not self.GetPath(): print("Couldn't make path...") time.sleep(0.05) continue #Failed to find a path; try again #Get pixel location on screen of 3d cell to go to l2d = tuple( int(k) for k in self.pm.Get2DT(np.array([self.CPE() ])).ravel().round()) self.GoToPosition(l2d) #Perform lightning-warp def ShiftMap(self, st=None): if st is None: #Translate projection map based on new character position st = self.mm.GetPosition() cpx, cpy, cpz = st #X, Y, Z components of shift hpx, hpy, hpz = self.hp self.pm.TranslateM(hpx - cpx, hpy - cpy, hpz - cpz) def SlowMouseMove(self): self.nmmpm = 10 def Start(self): self.sv.Start() #Start capturing images of the window cp3d = self.GetPlayerPos() #Get initial player position self.hp = cp3d #Update data member self.mm.Start(cp3d) #Update internal map self.p = None #The current path (of Exile, HA!) self.StartTargetLoop() #Start obtaining targets concurrently self.StartObjLoop() #Start detecting obstacles concurrently self.StartLWLoop() #Start detecting lightning-warp def StartLWLoop(self): self.clwl = True thrd = Thread(target=self.LWDetectLoop) thrd.start() return True def StartObjLoop(self): self.cobl = True thrd = Thread(target=self.ObjDetectLoop) thrd.start() return True def StartTargetLoop(self): self.ctl = True thrd = Thread(target=self.TargetLoop) thrd.start() return True def Stop(self): #Notify all concurrent threads to stop self.StopObjDetectLoop() self.StopTargetLoop() self.StopLWLoop() self.sv.Stop() def StopLWLoop(self): self.clwl = False def StopObjDetectLoop(self): self.cobl = False def StopTargetLoop(self): self.ctl = False #Turn off continue targeting loop flag def TargetLoop(self): while self.ctl: #Loop while continue targeting loop flag is set self.ts.DetectMovement( self.GetScreenDifference()) #Find cells that have movement self.ts.ClassifyDInput(self.sv.GetScreen( )) #Determine which cells contain enemies or items tecp = self.ts.EnemyPositionsToTargets() #Obtain target locations self.tlMut.acquire() self.ecp = tecp #Update shared enemy position listed self.tlut = time.time() #Update time self.tlMut.release() time.sleep(Bot.ENT_WT) def SplitSave(self, p='TSD/Train/Images', wp='TSD/Train/Split'): ''' #p: #Dir contains images to split #wp: #Dir to write split images ''' c = 0 if not os.path.exists(wp): os.mkdir(wp) pdl = np.random.choice( [fni for fni in os.listdir(p) if fni.startswith('di')], 32, replace=False) for i, fn in enumerate(pdl): print('{:4d}/{:4d}:\t{:s}'.format(i + 1, len(pdl), fn)) #A = imread(os.path.join(p, fn))[0:-14, 1:-1] #A = self.GetScreen() #S = self.ts.DivideIntoSubimages(A).astype(np.uint8) A = imread(os.path.join(p, fn))[0:-12, 4:-4, :] S = self.ts.DivideIntoSubimages(A).astype(np.uint8) for i, Si in enumerate(S): imsave(os.path.join(wp, '{:03d}.png'.format(c)), Si) c += 1 def SplitDataset(self, p='TS/Train', wp='TS/Train'): if not os.path.exists(wp): os.mkdir(wp) if not os.path.exists(os.path.join(wp, 'Enemy')): os.mkdir(os.path.join(wp, 'Enemy')) if not os.path.exists(os.path.join(wp, 'Closed')): os.mkdir(os.path.join(wp, 'Closed')) if not os.path.exists(os.path.join(wp, 'Open')): os.mkdir(os.path.join(wp, 'Open')) if not os.path.exists(os.path.join(wp, 'Overlay')): os.mkdir(os.path.join(wp, 'Overlay')) #Use old targeting system to classify new input #ts2 = TargetingSystem(m = 6, n = 9, ss = (1278, 786), cp = (642, 342), forceTrain = True) self.ts.C.RestoreClasses(['C', 'O', 'E']) dl = os.listdir(os.path.join(p, 'Split')) S, F = [], [] c = [0, 0, 0, 0] for i, fni in enumerate(dl): fpn = os.path.join(p, 'Split', fni) fnInt = int(fni[0:-4]) if fnInt % 63 == 0 or fnInt % 63 == 54 or fnInt % 63 == 55 or fnInt % 63 == 56 or fnInt % 63 == 57 or fnInt % 63 == 59 or fnInt % 63 == 60 or fnInt % 63 == 61 or fnInt % 63 == 62: nfp = os.path.join(wp, 'Overlay', fni) os.rename(fpn, nfp) print('os.rename(' + str(fpn) + ', ' + str(nfp)) continue S.append(imread(fpn)) F.append(fni) if len(S) > 20 or (i + 1) == len(dl): SS = np.stack(S) yh = self.ts.C.predict(SS) for j, yhj in enumerate(yh): if yhj == 'E': mp = 'Enemy' c[0] += 1 elif yhj == 'C': mp = 'Closed' c[1] += 1 elif yhj == 'H': mp = 'Overlay' c[2] += 1 else: mp = 'Open' c[3] += 1 nfp = os.path.join(wp, mp, F[j]) ofp = os.path.join(p, 'Split', F[j]) os.rename(ofp, nfp) print('os.rename(' + str(ofp) + ', ' + str(nfp)) S, F = [], [] print(str(c)) def UpdatePosition(self): isMoving, hasMoved = self.IsMoving() if isMoving: #Bot is still moving return Bot.MOVE_INPR if hasMoved: #Bot was moving but has now stopped self.mm.MoveTo(self.CPE()) #Update char pos in movement map self.ShiftMap() #Translate projection map self.NextPathEle() #Move on to next element in the path self.mm.ClearBlocked() #Blocked cells may change after moving self.lwMut.acquire() self.lwuo = False #Turn off the updated flag self.lwMut.release() return Bot.MOVE_OKAY #Only executes if neither condition above is true; meaning the bot failed to move entirely if self.CPE() is None: return Bot.MOVE_OKAY #Path is none when bot first starts self.mm.InsertBlocked(self.CPE()) self.p = None #Path didn't work; find new one return Bot.MOVE_FAIL def UseHealthPotion(self): self.k.type_string(str(self.cpi + 1)) self.cpi = (self.cpi + 1) % 2 #Rotate through potions def UseManaPotion(self): self.k.type_string('3')
def add_coookie_login(bduss): global kk l5.insert(END,"请点击GOOG进行添加cookie") sleep() kk=1 l5.insert(END,"正在进行添加cookie请不要动鼠标") window_name = u'百度一下,你就知道 - Google Chrome' handow = win32gui.FindWindow(None, window_name) win32gui.SetWindowPos(handow, HWND_TOPMOST, 0, 0, 750, 723, SWP_SHOWWINDOW) # win32gui.ShowWindow(handow, SW_SHOWMAXIMIZED) m = PyMouse() m.click(652, 61, 1, 1) time.sleep(1) m.click(301, 87, 1, 1) time.sleep(1) m.click(273, 168, 1, 1) time.sleep(1) k = PyKeyboard() k.type_string('BDUSS') m.click(315, 236, 1, 1) time.sleep(1) k.type_string(bduss) time.sleep(1) m.click(369, 446, 1, 1) time.sleep(1) m.click(436, 141, 1, 3) time.sleep(1) m.click(297, 541, 1, 1) time.sleep(1) m.click(367, 656, 1, 1) time.sleep(1) m.click(70, 656, 1, 1) l5.insert(END,"cookie添加成功")
pyautogui.dragTo(int(xc[i]),int(yc[i]),button="left") i=i+1 time.sleep(delay) print i if (msgn[i] == "mouse left up"): delay = float(int(ntime[i + 1]) - int(ntime[i])) / 1000 print "mouse upp now" i=i+1 time.sleep(delay) elif (msgn[i]=="mouse left down" and msgn[i+1]=="mouse left up"): delay = float(int(ntime[i + 1]) - int(ntime[i])) / 1000 print delay m.click(int(xc[i]), int(yc[i]), 1) i=i+2 time.sleep(delay) elif (msgn[i] == "mouse right down" and msgn[i+1] == "mouse right up"): print i delay = float(int(ntime[i + 1]) - int(ntime[i])) / 1000 print delay m.click(int(xc[i]), int(yc[i]), 1) i = i + 2 time.sleep(delay) elif (msgn[i]=="key down"): if(fpos[i]=="shif"): key= fpos[i+1]
if pts[i - 1] is None or pts[i] is None: continue thickness = int(np.sqrt(5 / float(i + 1)) * 2.5) cv2.line(mask, pts[i - 1], pts[i], (0, 0, 255), thickness) # x_int = np.interp(pts[i][0], [1, 450], [1, 1366]) # y_int = np.interp(pts[i][1], [1, 600], [1, 768]) # mouse.move(x_int, y_int) mouse.move(pts[i][0], pts[i][1]) # ser.flushInput() read = ser.readline() if (read[0] == "0" or read[0] == "1"): print(read) read = int(read[0]) if read == 1: if prev != 1: x, y = mouse.position() mouse.click(x, y, 1) prev = read cv2.imshow("Frame", mask) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break camera.release() cv2.destroyAllWindows()
class GameAssist: def __init__(self, wdname): """初始化""" # 取得窗口句柄 self.hwnd = win32gui.FindWindow(0, wdname) if not self.hwnd: print("窗口找不到,请确认窗口句柄名称:【%s】" % wdname) exit() # 窗口显示最前面 win32gui.SetForegroundWindow(self.hwnd) # 小图标编号矩阵 self.im2num_arr = [] # 主截图的左上角坐标和右下角坐标 self.scree_left_and_right_point = (299, 251, 768, 564) # 小图标宽高 self.im_width = 39 # PyMouse对象,鼠标点击 self.mouse = PyMouse() def screenshot(self): """屏幕截图""" # 1、用grab函数截图,参数为左上角和右下角左标 # image = ImageGrab.grab((417, 257, 885, 569)) image = ImageGrab.grab(self.scree_left_and_right_point) # 2、分切小图 # exit() image_list = {} offset = self.im_width # 39 # 8行12列 for x in range(8): image_list[x] = {} for y in range(12): # print("show",x, y) # exit() top = x * offset left = y * offset right = (y + 1) * offset bottom = (x + 1) * offset # 用crop函数切割成小图标,参数为图标的左上角和右下角左边 im = image.crop((left, top, right, bottom)) # 将切割好的图标存入对应的位置 image_list[x][y] = im return image_list def image2num(self, image_list): """将图标矩阵转换成数字矩阵""" # 1、创建全零矩阵和空的一维数组 arr = np.zeros((10, 14), dtype=np.int32) # 以数字代替图片 image_type_list = [] # 2、识别出不同的图片,将图片矩阵转换成数字矩阵 for i in range(len(image_list)): for j in range(len(image_list[0])): im = image_list[i][j] # 验证当前图标是否已存入 index = self.getIndex(im, image_type_list) # 不存在image_type_list if index < 0: image_type_list.append(im) arr[i + 1][j + 1] = len(image_type_list) else: arr[i + 1][j + 1] = index + 1 print("图标数:", len(image_type_list)) self.im2num_arr = arr return arr # 检查数组中是否有图标,如果有则返回索引下表 def getIndex(self, im, im_list): for i in range(len(im_list)): if self.isMatch(im, im_list[i]): return i return -1 # 汉明距离判断两个图标是否一样 def isMatch(self, im1, im2): # 缩小图标,转成灰度 image1 = im1.resize((20, 20), Image.ANTIALIAS).convert("L") image2 = im2.resize((20, 20), Image.ANTIALIAS).convert("L") # 将灰度图标转成01串,即系二进制数据 pixels1 = list(image1.getdata()) pixels2 = list(image2.getdata()) avg1 = sum(pixels1) / len(pixels1) avg2 = sum(pixels2) / len(pixels2) hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pixels1)) hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pixels2)) # 统计两个01串不同数字的个数 match = sum(map(operator.ne, hash1, hash2)) # 阀值设为10 return match < 10 # 判断矩阵是否全为0 def isAllZero(self, arr): for i in range(1, 9): for j in range(1, 13): if arr[i][j] != 0: return False return True # 是否为同行或同列且可连 def isReachable(self, x1, y1, x2, y2): # 1、先判断值是否相同 if self.im2num_arr[x1][y1] != self.im2num_arr[x2][y2]: return False # 1、分别获取两个坐标同行或同列可连的坐标数组 list1 = self.getDirectConnectList(x1, y1) list2 = self.getDirectConnectList(x2, y2) # print(x1, y1, list1) # print(x2, y2, list2) # exit() # 2、比较坐标数组中是否可连 for x1, y1 in list1: for x2, y2 in list2: if self.isDirectConnect(x1, y1, x2, y2): return True return False # 获取同行或同列可连的坐标数组 def getDirectConnectList(self, x, y): plist = [] for px in range(0, 10): for py in range(0, 14): # 获取同行或同列且为0的坐标 if self.im2num_arr[px][py] == 0 and self.isDirectConnect( x, y, px, py): plist.append([px, py]) return plist # 是否为同行或同列且可连 def isDirectConnect(self, x1, y1, x2, y2): # 1、位置完全相同 if x1 == x2 and y1 == y2: return False # 2、行列都不同的 if x1 != x2 and y1 != y2: return False # 3、同行 if x1 == x2 and self.isRowConnect(x1, y1, y2): return True # 4、同列 if y1 == y2 and self.isColConnect(y1, x1, x2): return True return False # 判断同行是否可连 def isRowConnect(self, x, y1, y2): minY = min(y1, y2) maxY = max(y1, y2) # 相邻直接可连 if maxY - minY == 1: return True # 判断两个坐标之间是否全为0 for y0 in range(minY + 1, maxY): if self.im2num_arr[x][y0] != 0: return False return True # 判断同列是否可连 def isColConnect(self, y, x1, x2): minX = min(x1, x2) maxX = max(x1, x2) # 相邻直接可连 if maxX - minX == 1: return True # 判断两个坐标之间是否全为0 for x0 in range(minX + 1, maxX): if self.im2num_arr[x0][y] != 0: return False return True # 点击事件并设置数组为0 def clickAndSetZero(self, x1, y1, x2, y2): # print("click", x1, y1, x2, y2) # (299, 251, 768, 564) # 原理:左上角图标中点 + 偏移量 p1_x = int(self.scree_left_and_right_point[0] + (y1 - 1) * self.im_width + (self.im_width / 2)) p1_y = int(self.scree_left_and_right_point[1] + (x1 - 1) * self.im_width + (self.im_width / 2)) p2_x = int(self.scree_left_and_right_point[0] + (y2 - 1) * self.im_width + (self.im_width / 2)) p2_y = int(self.scree_left_and_right_point[1] + (x2 - 1) * self.im_width + (self.im_width / 2)) time.sleep(0.2) self.mouse.click(p1_x, p1_y) time.sleep(0.2) self.mouse.click(p2_x, p2_y) # 设置矩阵值为0 self.im2num_arr[x1][y1] = 0 self.im2num_arr[x2][y2] = 0 print("消除:(%d, %d) (%d, %d)" % (x1, y1, x2, y2)) # exit() # 程序入口、控制中心 def start(self): # 1、先截取游戏区域大图,然后分切每个小图 image_list = self.screenshot() # 2、识别小图标,收集编号 self.image2num(image_list) print(self.im2num_arr) # 3、遍历查找可以相连的坐标 while not self.isAllZero(self.im2num_arr): for x1 in range(1, 9): for y1 in range(1, 13): if self.im2num_arr[x1][y1] == 0: continue for x2 in range(1, 9): for y2 in range(1, 13): # 跳过为0 或者同一个 if self.im2num_arr[x2][y2] == 0 or (x1 == x2 and y1 == y2): continue if self.isReachable(x1, y1, x2, y2): self.clickAndSetZero(x1, y1, x2, y2)
def handle_watch(self, message, r_name='i', remind=False): self.speak_dialog('starting') keyboard = Controller() m = PyMouse() right = Key.right left = Key.left recognizer = cv2.face.LBPHFaceRecognizer_create() recognizer.read('/home/craghack/Documents/camera/trainer.yml') faceCascade = cv2.CascadeClassifier( "/home/craghack/Documents/camera/haarcascade_frontalface_default.xml" ) #font = cv2.FONT_HERSHEY_SIMPLEX names = ['unknown'] ids = open("/home/craghack/Documents/camera/ids.txt", "r") for line in ids: names.append(line.lower().split(' ')[2].rstrip()) ids.close() # Initialize and start realtime video capture cam = cv2.VideoCapture(0) #cam.set(3, 640) # set video widht #cam.set(4, 480) # set video height minW = 0.1 * cam.get(3) minH = 0.1 * cam.get(4) emoji = None cmd = subprocess.check_output("wmctrl -l | grep \".png\" | cut -b 23-", shell=True).decode("utf-8").rstrip() if not cmd == "": emoji = cmd.split('.')[0] os.system("wmctrl -a \"" + cmd + "\"") else: emoji = "suspicious" os.system( "xdg-open /home/craghack/Downloads/emojis/suspicious.png") sleep(.25) m.click(350, 460) sleep(.25) m.move(799, 479) if emoji == "happy": keyboard.press(left) keyboard.release(left) emoji = "suspicious" elif emoji == "suprised": keyboard.press(right) keyboard.release(right) emoji = "suspicious" self.speak_dialog('watching') confident = False try: count = 0 recognized = [] while not confident: rval, frame = cam.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(int(minW), int(minH))) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) ID, confidence = recognizer.predict(gray[y:y + h, x:x + w]) confidence = 100 - confidence # Confidence starts backwards for some reason name = names[ID - 1] if (name == "unknown") and (confidence > 50): # confident = True if emoji == "suspicious": keyboard.press(left) keyboard.release(left) sleep(.75) keyboard.press(right) keyboard.release(right) elif emoji == "happy": keyboard.press(right) keyboard.release(right) sleep(.75) keyboard.press(right) keyboard.release(right) emoji = "suspicious" recognized.append("unknown") self.speak_dialog('unknown') elif (confidence > 15): # confident = True if name not in recognized: if emoji == "suspicious": keyboard.press(right) keyboard.release(right) emoji = "happy" elif emoji == "happy": keyboard.press(right) keyboard.release(right) sleep(.75) keyboard.press(left) keyboard.release(left) recognized.append(name) #dialog = id + '.face' response = {'name': name} self.speak_dialog('recognized', data=response) if not r_name == 'i': if r_name in recognized: confident = True else: if emoji == "happy": sleep(.5) keyboard.press(left) keyboard.release(left) emoji = "suspicious" elif len(recognized) != 0 and len(recognized) >= len(faces): count += 1 if (count >= 50): confident = True else: count = 0 cam.release() except Exception as e: print(e) keyboard.press(Key.esc) keyboard.release(Key.esc) os.system("wmctrl -a \"start-mycroft.sh debug\"") os.system("wmctrl -a \"craghack@Cyclops: ~/mycroft-core\"") if not remind: sleep(1) keyboard.press(Key.esc) keyboard.release(Key.esc) os.system("wmctrl -a \"start-mycroft.sh debug\"") os.system("wmctrl -a \"craghack@Cyclops: ~/mycroft-core\"") return confident
#pattern = midi.read_midifile('/home/claudio/Downloads/EspanjaPrelude.mid') # pattern = midi.read_midifile('/home/claudio/Development/python-midi/mary.mid') # events = [] # for track in pattern: # for event in track: # if isinstance(event, midi.events.NoteOnEvent): # print(type(event)) # events.append(event) # events.sort() # pprint(events) # sys.exit() mouseButton = 1 # 1 for left 2 for right click mouse = PyMouse() screenSize = mouse.screen_size() scaleFactor = 32 scaledMaxValue = int(screenSize[1] / scaleFactor + 1) pause = 1 for i in range(0, scaledMaxValue): currentPosition = mouse.position() y = i * scaleFactor x = currentPosition[0] print(y) #mouse.move(currentPosition[0], y) mouse.click(x, y, 2) time.sleep(pause)
#!/usr/bin/python #importamos modulos import time from pymouse import PyMouse from pykeyboard import PyKeyboard k = PyKeyboard() m = PyMouse() # https://github.com/SavinaRoja/PyUserInput/wiki/PyMouse # click works about the same, except for int button possible values # are 1: left, 2: right, 3: middle #presionamos tecla meta y tecla D k.press_key(k.super_r_key) k.press_key('d') #dejamos de presionar las teclas k.release_key(k.super_r_key) k.release_key('d') #hacemos click derecho time.sleep(0.5) m.click(500, 220, 2)
k = PyKeyboard() differs = [] xx = [460, 635] t = threading.Timer(10, change_detect_area) t.start() day = Image.open('example1.jpg') night = Image.open('example2.png') switch = -1 differs = [] while True: img = ImageGrab.grab((xx[0], 480, xx[1], 600)) differ = image_contrast(night if switch == 1 else day, img) #print(differs) differs.append(differ) if len(differs) == 3: differs.pop(0) if differs[1] - differs[0] == 0: xx = [460, 635] m.click(460, 280) if differ > 1000: switch *= -1 if 60 < differ < 500: k.press_key(' ') time.sleep(0.15) k.release_key(' ')
def grabbing_screen(tmp_dir, number_pages=None, cmp_pages_after=None): """ grabbing screen, paging and yield images number_pages - count pages for grabbing cmp_images_after - after that count pages current page and previous page will be compared, and if they will be equal function stoped """ from pymouse import PyMouse m = PyMouse() grab_coords = [] raw_input("Set mouse to up left corner and press enter...") grab_coords += list(m.position()) raw_input("Set mouse to down left corner and press enter...") grab_coords += list(m.position()) grab_coords[2] -= grab_coords[0] grab_coords[3] -= grab_coords[1] grab_coords = map(lambda x: str(int(x)), grab_coords) raw_input("Set mouse to position for paging and press enter") paging_coords_args = list(m.position()) + [1] def make_screenshot(coords, filename): command = "screencapture -R%s %s" % (','.join(coords), filename) os.system(command) for i in xrange(number_pages): current_page_image = image_page(tmp_dir, i) make_screenshot(grab_coords, current_page_image) m.click(*paging_coords_args) time.sleep(1) if not i % 10: print 'grabing page #%d' % i if i > cmp_pages_after: prev_image = image_page(tmp_dir, i - 1) if filecmp.cmp(current_page_image, prev_image): os.system("rm %s" % current_page_image) break yield current_page_image