def mouse_click(): mouse = Controller() mouse.press(Button.left) mouse.release(Button.left)
import re import webbrowser import time from pynput.mouse import Button, Controller mouse = Controller() from pynput.keyboard import Key, Controller keyboard = Controller() time.sleep(2) for x in range(30): mouse.click(Button.left, 3) keyboard.press(Key.ctrl) keyboard.press('a') keyboard.release('a') keyboard.release(Key.ctrl) keyboard.type('TEXT') mouse.move(0, 10) mouse.move(0, -30 * 10) mouse.click(Button.left, 1) mouse.scroll(0, -30 * 10 * 2)
1)) # average power of all channels for w past time points except: avPower = 0 movavPower = np.mean( avPower ) # average power of the past w signal points. We have effectively computed the moving average in real time if movavPower > t1 and not ( sw): # see getActiveFunc.py for a description of this sw = 1 block = [] if movavPower < t2 and sw: sw = 0 block = np.array(block) resultPose = predictPoseFunc(block, loaded_model, scaler) # predict the pose print(resultPose) if resultPose == 'two_fingers_up': def on_move(x, y): return (x, y) if mc.press(Button.left): val = on_move() mc.move(val) elif resultPose == 'fist_pull': mc.release(Button.left) if sw: block.append( frame[0] ) # keep forming the block until the active segment is over
# -*- coding: utf-8 -*- """ Created on Sun Mar 10 20:22:58 2019 @author: umaru """ import pynput from pynput.mouse import Button, Controller from pynput.keyboard import Key, Controller mouse = Controller() keyboard = Controller() # Set pointer position mouse.position = (1114, 440) print('The current pointer position is {0}'.format(mouse.position)) mouse.press(Button.left) mouse.release(Button.left) keyboard.press(Key.f) keyboard.release(Key.f)
class Mouse: def __init__(self): self.mouse = MouseController() def move(self, location: Location = None, duration: float = None): """Mouse move with tween. :param location: Location , image name or Pattern. :param duration: Speed of mouse movement from current mouse location to target. :return: None. """ if location is None: location = Location(0, 0) if duration is None: duration = Settings.move_mouse_delay def set_mouse_position(loc_x, loc_y): self.mouse.position = (int(loc_x), int(loc_y)) def smooth_move_mouse(from_x, from_y, to_x, to_y): num_steps = int(duration / 0.05) sleep_amount = 0 try: sleep_amount = duration / num_steps except ZeroDivisionError: pass steps = [ _get_point_on_line(from_x, from_y, to_x, to_y, n / num_steps) for n in range(num_steps) ] steps.append((to_x, to_y)) for tween_x, tween_y in steps: tween_x = int(round(tween_x)) tween_y = int(round(tween_y)) set_mouse_position(tween_x, tween_y) time.sleep(sleep_amount) return smooth_move_mouse(self.mouse.position[0], self.mouse.position[1], location.x, location.y) def press( self, location: Location = None, duration: float = None, button: Button = Button.left, ): """Mouse press. :param location: Mouse press location. :param duration: Speed of mouse movement from current mouse location to target. :param button: 'left','right' or 'middle'. :return: None """ self.move(location, duration) self.mouse.press(button) def release( self, location: Location = None, duration: float = None, button: Button = Button.left, ): """Mouse press. :param location: Mouse press location. :param duration: Speed of mouse movement from current mouse location to target. :param button: 'left','right' or 'middle'. :return: None """ self.move(location, duration) self.mouse.release(button) def general_click( self, location: Location = None, duration: float = None, button: Button = Button.left, clicks: int = 1, ): """General mouse click location. :param location: click location :param duration: Speed of mouse movement from current mouse location to target. :param button: 'left','right' or 'middle'. :param clicks: number of mouse clicks. :return: None. """ self.move(location, duration) self.mouse.click(button, clicks) def drag_and_drop(self, start: Location, end: Location, duration: float = None): """Mouse drag and drop. :param start: Starting location :param end: Drop location :param duration: Speed of mouse movement to the drag and drop location. :return: None. """ time.sleep(Settings.DEFAULT_UI_DELAY) self.move(start, duration) time.sleep(Settings.delay_before_mouse_down) self.mouse.press(Button.left) time.sleep(Settings.delay_before_drag) self.move(end, duration) time.sleep(Settings.delay_before_drop) self.mouse.release(Button.left) def scroll(self, dx: int = None, dy: int = None, iterations: int = 1): """Sends scroll events. :param int dx: The horizontal scroll. :param int dy: The vertical scroll. :param int iterations: Number of iterations for the scroll event. :return None. """ if dx is None: dx = Settings.mouse_scroll_step if dy is None: dy = Settings.mouse_scroll_step for i in range(iterations): self.mouse.scroll(dx, dy) time.sleep(0.5)
class Runner: def __init__(self): self.mouse = Controller() self.keyboard = KeyController() self.chat_colors = [ (1, 10, 19), # chatbox body color (14, 23, 30), ] self.lockin_colors = [ (92, 91, 87), # button border ] self.player_joined_colors = [ (66, 66, 65), (66, 66, 66), ] def move_and_type_first(self, lane, repeat, chatbox, lockin, timeout=5 * 60, stoprequest=None, debug=False): self._type_when_color( chatbox, lockin, self.lockin_colors, lane, repeat, rect2=chatbox, expected_colors2=self.chat_colors, timeout_sec=timeout, name='matchmake screen', stoprequest=stoprequest, debug=debug, ) def move_and_type_again(self, lane, repeat, chatbox, players_joined, timeout=5, stoprequest=None, debug=False, tolerance=0): self._type_when_color( chatbox, players_joined, self.player_joined_colors, lane, repeat, timeout_sec=timeout, name='player joined', wait_timeout=True, stoprequest=stoprequest, debug=debug, tolerance=tolerance, ) # # Utility # def _type_when_color(self, chatbox, rect, expected_colors, text, repeat, timeout_sec=5, name='', rect2=None, expected_colors2=None, wait_timeout=False, stoprequest=None, debug=False, tolerance=5): print('Type when %s...' % name) t_start = datetime.utcnow() x = chatbox.avg_x() y = chatbox.avg_y() while True: if stoprequest is not None and stoprequest.is_set(): print('Stopping runner thread gracefully') return if datetime.utcnow() - t_start > timedelta(seconds=timeout_sec): print('Done waiting %s to appear (timeout is set to %d sec)' % (name, timeout_sec)) break if self._rect_contains_color(rect, expected_colors, tolerance=tolerance): print('First color is correct') if rect2 is None or self._rect_contains_color( rect2, expected_colors2): print('Typing to coordinates: (%d, %d)' % (x, y)) self._move_and_type(x, y, text, repeat, debug=debug) if not wait_timeout or debug: break print('Done typing when %s' % name) def _move_and_type(self, x, y, lane, howManyTimes, debug=False): if debug: image = ImageGrab.grab() pixels = image.load() for i in range(-10, 10): pixels[x + i, y] = (0, 0, 255, 255) pixels[x, y + i] = (0, 0, 255, 255) image.show() return for i in range(howManyTimes): self.mouse.position = (x, y) self.mouse.press(Button.left) self.mouse.release(Button.left) self.keyboard.type(lane) self.keyboard.press(Key.enter) self.keyboard.release(Key.enter) time.sleep(0.1) def _color_at_pixel(self, x, y): px = ImageGrab.grab((x, y, x + 1, y + 1)).load() selected = px[0, 0] return selected[0], selected[1], selected[2] # R, G, B def _rect_contains_color(self, rect, colors, tolerance=5): px = ImageGrab.grab(rect.to_tuple()).load() for i in range(rect.length()): for j in range(rect.height()): if util.color_matches_at_least_one(px[i, j], colors, tolerance=tolerance): return True return False
def unlock(self): pyautogui.moveTo(687, 533, duration=0) mouse = Controller() mouse.press(Button.left) pyautogui.dragTo(1200, 533, duration=0.5) mouse.release(Button.left)
class StardewFisherEnv(gym.Env): def __init__(self): self.mouse = Controller() #Can be anywhere from fish at top, bar at bottom, to vice versa. self.top = 20 self.bottom = 515 self.observation_space = spaces.Discrete((self.bottom*2)+1) self.action_space = spaces.Discrete(len(actions)) #Variables related to location and determining if fish being caught self.catching = True self.fish_location = 497 self.bar_location = (385,543) #Variables related to timer self.start_time = 0 self.elapsed_time = 0 self.max_time = 10 #max time is 8 seconds to catch the fish? self.max_diff = self.bottom - self.top #cv2/obj locater vars self.show_screen = True self.finder = object_finder.object_finder(load_model_path='models\\batch100_fish_id.h5') self.fish_start_col = 3 self.fish_end_col = 38 self.bar_start_col = 1 self.bar_end_col = 40 self.bar_height = 158 self.catch_range = self.bar_height / 2 self.screen_dims = np.load('models\\numpy_data\\screen_dims.npy').tolist() #(800, 305, 840, 855) self.last_screen = None self.moving = 0 self.second = False def step(self, action): assert self.action_space.contains(action) #Do action if action == 0: self.mouse.press(Button.left) self.moving = 1 elif action == 1: pass elif action == 2: self.mouse.release(Button.left) self.moving = 0 #Get current locations self._get_obs() screen = np.array(ImageGrab.grab(bbox=self.screen_dims)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) temp = 1 if self.last_screen is not None: temp = np.sum(np.where(screen == self.last_screen, 1, 0)) / screen.size self.last_screen = screen if temp < 0.1 or self.bar_location == (0, 0): #fish caught print('Temp is {}'.format(temp)) done = True if self.elapsed_time < 0.3: self.catching = False else: #fish is somewhere done = False self._update_time() obs = self._get_obs() if self.bar_location == (0, 0): done = True if self.elapsed_time < 0.3: self.catching = False #Get reward for not being done reward = self._get_reward() return obs, reward, done, {} def reset(self): self.catching = True self.start_time = time.time() self.elapsed_time = 0 self.fish_location = 497 self.bar_location = (385,543) self.finder.last_bar_data = (385,543) self.last_screen = None self.moving = 0 return self._get_difference() + (self.observation_space.n - 1) * self.moving def _get_obs(self): #capture the window (wrote script to resize window) screen = np.array(ImageGrab.grab(bbox=self.screen_dims)) screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB) #if self.second: #pdb.set_trace() self.fish_location = self.finder.locate_fish(screen) self.bar_location = self.finder.locate_bar(screen) if self.show_screen: #Draw rect around fish cv2.rectangle(screen, (self.fish_start_col, self.fish_location), (self.fish_end_col, self.fish_location+27), [0, 255, 255], 1) #Draw rect around bar cv2.rectangle(screen, (self.bar_start_col, self.bar_location[0]), (self.bar_end_col, self.bar_location[1]), [255, 255, 0], 1) cv2.imshow('', screen) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() #Return roughly middle of bar return self._get_difference() + (self.observation_space.n - 1) * self.moving def _update_time(self): # pdb.set_trace() if abs(self._get_difference()) >= 75: #If was catching and now not, reset timers if self.catching: self.catching = False self.start_time = time.time() self.elapsed_time = 0 else: self.elapsed_time = self._check_elapsed() else: #If still catching, update timer if self.catching: self.elapsed_time = self._check_elapsed() else: self.catching = True self.start_time = time.time() self.elapsed_time = 0 #If fish is out of range, reset the timer def _check_elapsed(self): return time.time() - self.start_time #Get difference in two locations def _get_difference(self): return (self.fish_location+14) - int(self.bar_location[0] + (76)) #Get current reward def _get_reward(self): self._update_time() reward_factor = 1 if self.catching == True else -1 return reward_factor
mouse.click(Button.left, 1) sleep(1) ##minimize Terminal mouse.position = (968, 94) mouse.click(Button.left, 1) sleep(1) terminal() ##write command keyboard.type('firefox') sleep(0.5) keyboard.press(Key.enter) keyboard.release(Key.enter) sleep(10) ##close second page mouse.position = (434, 35) mouse.click(Button.left, 1) sleep(1) ##click on searchbar keyboard.press(Key.ctrl) keyboard.press('l') keyboard.release(Key.ctrl) keyboard.release('l') sleep(0.5) ##go to solver site
maskOpen = cv2.morphologyEx( mask, cv2.MORPH_OPEN, kernelClose ) #Applying Morphology, removing outside noise being scanned from video maskClose = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernelClose) #removing inner noise maskFinal = maskOpen _, conts, hier = cv2.findContours( maskFinal.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) #Obtaining countour box for the found object cv2.drawContours(img, conts, -1, (0, 0, 255), 3) #drawing contour box on original image if (len(conts) == 2): #case for two rectangles if pinchFlag == 1: pinchFlag = 0 mouse.release(Button.left) #reducing mutiple clicks x1, y1, w1, h1 = cv2.boundingRect(conts[0]) x2, y2, w2, h2 = cv2.boundingRect(conts[1]) cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 2) # Drawing contour rectangle for 1st box cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (255, 0, 0), 2) # Drawing contour rectangle for 1st box cx1, cy1 = int((x1 + w1 / 2)), int((y1 + h1 / 2)) cx2, cy2 = int((x2 + w2 / 2)), int( (y2 + h2 / 2)) #Finding middle point of both contours cv2.line(img, (cx1, cy1), (cx2, cy2), (255, 0, 0), 2) #Drawing a line between two contours cx, cy = int((cx1 + cx2) / 2), int((cy1 + cy2) / 2) cv2.circle(img, (cx, cy), 2, (0, 0, 255), 2) #plotting a center point between two lines mouseLoc = mLocOld + ((cx, cy) - mLocOld) / DampingFactor
def controll_mouse_pointer_and_click(): mouse=Controller() app=wx.App(False) (sx,sy)=wx.GetDisplaySize() (camx,camy)=(320,240) lowerBound=np.array([33,80,40]) upperBound=np.array([102,255,255]) cam= cv2.VideoCapture(0) kernelOpen=np.ones((5,5)) kernelClose=np.ones((20,20)) pinchFlag=0 while True: ret, img=cam.read() img=cv2.resize(img,(340,220)) #convert BGR to HSV imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV) # create the Mask mask=cv2.inRange(imgHSV,lowerBound,upperBound) #morphology maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen) maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose) maskFinal=maskClose conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) if(len(conts)==2): if(pinchFlag==1): pinchFlag=0 mouse.release(Button.left) x1,y1,w1,h1=cv2.boundingRect(conts[0]) x2,y2,w2,h2=cv2.boundingRect(conts[1]) cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2) cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2) cx1=int(x1+w1/2) cy1=int(y1+h1/2) cx2=int(x2+w2/2) cy2=int(y2+h2/2) cx=int((cx1+cx2)/2) cy=int((cy1+cy2)/2) cv2.line(img, (cx1,cy1),(cx2,cy2),(255,0,0),2) cv2.circle(img, (cx,cy),2,(0,0,255),2) mouseLoc=(int(sx-(cx*sx/camx)), int(cy*sy/camy)) mouse.position=mouseLoc while mouse.position!=mouseLoc: pass elif(len(conts)==1): x,y,w,h=cv2.boundingRect(conts[0]) if(pinchFlag==0): pinchFlag=1 mouse.press(Button.left) cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) cx=int(x+w/2) cy=int(y+h/2) cv2.circle(img,(cx,cy),int((w+h)/4),(0,0,255),2) mouseLoc=(int(sx-(cx*sx/camx)), int(cy*sy/camy)) mouse.position=mouseLoc while mouse.position!=mouseLoc: pass cv2.imshow("cam",img) cv2.waitKey(5)
class Epoch(Thread): def __init__(self): super().__init__() self.mouse = Controller() self.button = Button.left self.toggleable = False self.running = True self.held = False self.pattern = get_randomisation(cps) self.cycles = 0 self.keymap = { toggle: self.toggle, close: self.exit } def regenerate(self): if self.cycles % 2 == 0: offset = 0.5 / (raw_cps * uniform(1.075, 1.175)) * 1000 else: offset = 0.5 / (raw_cps * uniform(0.95, 1.05)) * 1000 self.pattern = get_randomisation(offset) self.cycles += 1 def exit(self): self.running = False system("cls") print(closing) time.sleep(0.4) system("cls") exit(0) def toggle(self): self.held = False self.toggleable = not self.toggleable if not self.toggleable: self.regenerate() def on_click(self, _, __, button, ___): if self.toggleable and button == self.button: self.held = not self.held def on_press(self, key): action = self.keymap.get(key, None) if action: action() def run(self): while self.running: while self.held: try: delay = self.pattern.__next__() except StopIteration: self.regenerate() delay = self.pattern.__next__() self.mouse.press(self.button) sleep(delay) self.mouse.release(self.button) sleep(delay) sleep(0.1)
w = WindowMgr() for wikiid in config: os.system("taskkill /f /im " + browserExe) webbrowser.open_new_tab("https://wiki.uxtrata.net/wiki/" + wikiid) time.sleep(5) mouse.position = (389, 242) time.sleep(0.5) mouse.press(Button.left) time.sleep(0.5) mouse.position = (300, 245) time.sleep(0.5) mouse.scroll(0, -100000) time.sleep(0.5) mouse.position = (384, 981) time.sleep(0.5) mouse.release(Button.left) time.sleep(0.5) keyboard.press(Key.ctrl) keyboard.press('c') keyboard.release('c') keyboard.release(Key.ctrl) time.sleep(0.5) #paste content to Word w.find_window_wildcard(".*Word.*") w.set_foreground() time.sleep(0.5) keyboard.press(Key.ctrl) keyboard.press('v') keyboard.release('v') keyboard.release(Key.ctrl)
def simple(): mouse = Controller() app = wx.App(False) cam = cv2.VideoCapture(0) print(cam.isOpened()) (sx, sy) = wx.GetDisplaySize() (camx, camy) = (640, 480) lowerBound = np.array([33, 80, 40]) upperBound = np.array([102, 255, 255]) kernelOpen = np.ones((5, 5)) kernelClose = np.ones((20, 20)) pinchFlag = 0 # time.sleep(2) while True: ret, img = cam.read() cv2.namedWindow("window") img = cv2.resize(img, (340, 220)) #convert BGR to HSV imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # create the Mask mask = cv2.inRange(imgHSV, lowerBound, upperBound) #morphology maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen) maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose) maskFinal = maskClose _, conts, h = cv2.findContours(maskFinal.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) if len(conts) == 2: if pinchFlag == 1: pinchFlag = 0 mouse.release(Button.left) x1, y1, w1, h1 = cv2.boundingRect(conts[0]) x2, y2, w2, h2 = cv2.boundingRect(conts[1]) cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 2) cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (255, 0, 0), 2) cx1 = x1 + w1 // 2 cy1 = y1 + h1 // 2 cx2 = x2 + w2 // 2 cy2 = y2 + h2 // 2 cx = (cx1 + cx2) // 2 cy = (cy1 + cy2) // 2 cv2.line(img, (cx1, cy1), (cx2, cy2), (255, 0, 0), 2) cv2.circle(img, (cx, cy), 2, (0, 0, 255), 2) mouseLoc = (sx - (cx * sx // camx), cy * sy // camy) mouse.position = mouseLoc while mouse.position != mouseLoc: pass elif len(conts) == 1: x, y, w, h = cv2.boundingRect(conts[0]) if pinchFlag == 0: pinchFlag = 1 mouse.press(Button.left) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) cx = x + w // 2 cy = y + h // 2 cv2.circle(img, (cx, cy), ((w + h) // 4), (0, 0, 255), 2) mouseLoc = (sx - (cx * sx // camx), cy * sy // camy) mouse.position = mouseLoc while mouse.position != mouseLoc: pass cv2.imshow("cam", img) if cv2.waitKey(5) & 0xFF == ord('q'): break
def send_mouse1_event(mouse_down_delay=0.05): """Send a left-click""" mouse = Controller() mouse.press(Button.left) time.sleep(mouse_down_delay) mouse.release(Button.left)
class Mouse: def __init__(self): self.mouse = Controller() self.position: tuple def move(self, x: int, y: int): """ move the mouse to select position """ self.mouse.position = (x, y) def click(self, button: str = 'left', total_click: int = 1): """ perform click on screen\n button is equal to:\n - right - left :param total_click: :param button: """ if button == 'right': self.mouse.click(Button.right, total_click) elif button == 'left': self.mouse.click(Button.left, total_click) else: raise Exception('The button selected is not allow') def press_button(self, button: str = 'left'): """ perform click but don't release it\n button is equal to:\n - right - left :param button: """ if button == 'right': self.mouse.press(Button.right) elif button == 'left': self.mouse.press(Button.left) else: raise Exception('The button selected is not allow') def release_button(self, button: str = 'left'): """ release button previously pressed :param button: """ if button == 'right': self.mouse.release(Button.right) elif button == 'left': self.mouse.release(Button.left) else: raise Exception('The button selected is not allow') def get_position(self): """ return mouse position """ while True: print(self.mouse.position) def __on_click(self, x, y, button, pressed): if pressed and button == Button.left: self.position = x, y if not pressed: # Stop listener return False def wait_click(self, message: str): """ wait mouse click :return: """ with mouse.Listener(on_click=self.__on_click) as listener: print(message) listener.join()
class winControl: def __init__(self): self.mouseControl = ControllerMouse() self.keyboardControl = Controllerkeyboard() self.mouse = mouse self.Key = keyboard.Key self.threading = threading self.Listener = Listener self.oldCpText = "" self.wc = wc def move(self, x, y): self.mouseControl.position = (x, y) pass def updateFile(self, imgSrc='test.jpg', winTitle='打开'): imgSrc = parse.unquote(imgSrc) windowBox = win32gui.FindWindow(None, winTitle) if (windowBox > 0): btn = win32gui.FindWindowEx(windowBox, None, 'Button', None) childWin = win32gui.FindWindowEx(windowBox, None, 'ComboBoxEx32', None) #逐级寻找 childWin = win32gui.FindWindowEx(childWin, None, 'ComboBox', None) childWin = win32gui.FindWindowEx(childWin, None, 'Edit', None) if (childWin > 0): win32gui.SendMessage(childWin, win32con.WM_SETTEXT, None, imgSrc) win32gui.PostMessage(btn, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32gui.PostMessage(btn, win32con.WM_KEYUP, win32con.VK_RETURN, 0) self.keyboardControl.press(Key.enter) self.keyboardControl.release(Key.enter) return "ok" else: return 'found eroor! please contact author...' else: return 'file upload not open!' def scrollbar(self, y=0): self.mouseControl.scroll(0, y) # 为正向上滚 pass def click(self, x, y): self.move(x, y) self.mouseControl.press(Button.left) self.mouseControl.release(Button.left) def rightClick(self, x, y): self.move(x, y) self.mouseControl.press(Button.right) self.mouseControl.release(Button.right) def setValue(self, x, y, value): value = parse.unquote(value) self.click(x, y) self.allSelect() self.writePaste(value) self.paste() def allSelect(self): # 全选 self.keyboardControl.press(self.Key.ctrl) self.keyboardControl.press('a') self.keyboardControl.release(self.Key.ctrl) self.keyboardControl.release('a') def paste(self): self.keyboardControl.press(self.Key.ctrl_l) self.keyboardControl.press('v') self.keyboardControl.release(self.Key.ctrl_l) self.keyboardControl.release('v') def writePaste(self, value): self.wc.OpenClipboard() self.wc.EmptyClipboard() self.wc.SetClipboardData(win32con.CF_UNICODETEXT, value) self.wc.CloseClipboard() def closeAPP(self, x, y): self.click(x, y) self.keyboardControl.press(self.Key.ctrl) self.keyboardControl.press(self.Key.f4) self.keyboardControl.release(self.Key.ctrl) self.keyboardControl.release(self.Key.f4) def getPosition(self): return self.mouseControl.position
#CMDOpenTest from pynput.mouse import Button, Listener, Controller from pynput.keyboard import Key, Listener, Controller import time keyboard = Controller() keyboard.press(Key.cmd) keyboard.release(Key.cmd) time.sleep(2) keyboard.type('cmd')
##################### re, bw = cv2.threshold(new_gray, 130, 255, cv2.THRESH_BINARY_INV) if x != 0 and y != 0: gray = bw[int(y - 10):int(y + 160), int(x - 40):int(x + 100)] gray = gray / 255.0 cv2.imshow("gray", gray) gray = cv2.resize(gray, (128, 128)) gray = gray.reshape(-1, 128, 128, 1) result = model.predict(gray) res = np.argmax(result) if flag: if res == 5: control.press(Button.right) control.release(Button.right) flag = False elif res == 2: control.press(Button.left) control.release(Button.left) flag = True ''' elif res == 3: control.press(Button.left) control.release(Button.left) elif res == 2: control.press(Button.left) control.release(Button.left) ''' cv2.putText(frame, res_[res], (125, 125), cv2.FONT_HERSHEY_SIMPLEX, 2,
def abort(event=None): mouse = Controller() mouse.release(Button.left)
#c = cv2.imread('rsrc/inspectElement.png') #matches = cv2.matchTemplate(s, c, cv2.TM_CCOEFF_NORMED) #loc = numpy.where(matches >= .9) #loc = list(zip(*loc[::-1])) #mouse.position = loc[0] #mouse.click(Button.left, 1) from pynput.keyboard import Key, Controller keyboard = Controller() print("Opening inspector") keyboard.press("Q") keyboard.release("Q") time.sleep(2) print("Switching to console") with keyboard.pressed(Key.ctrl): with keyboard.pressed(Key.shift): keyboard.press("k") keyboard.release("k") time.sleep(2.5) print("Switching to JS") keyboard.type("$0.in") # Seems to have an issue with the double n otherwise
class WpkMouse(): def __init__(self, deviation, debug = False): self.deviation = deviation self.flod_range_x = [1145, 1170] self.flod_range_y = [980, 1010] self.mouse = Controller() self.debug = debug def Flod(self): (raw_x, raw_y) = self.mouse.position x = random.randint(self.flod_range_x[0], self.flod_range_x[1]) + self.deviation[0] y = random.randint(self.flod_range_y[0], self.flod_range_y[1]) + self.deviation[1] if not self.debug: self.mouse.position = (x, y) self.mouse.click(Button.left, 1) self.mouse.position = (raw_x, raw_y) def PrintCoor(self): loggers.debug(self.mouse.position) def Open(self, level): (raw_x, raw_y) = self.mouse.position x = 0 y = 0 if level == "1/3": x = 1070 + random.randint(5, 10) + self.deviation[0] y = 850 + random.randint(5, 10) + self.deviation[1] if level == "1/2": x = 1190 + random.randint(5, 10) + self.deviation[0] y = 810 + random.randint(5, 10) + self.deviation[1] elif level == "2/3": x = 1310 + random.randint(5, 10) + self.deviation[0] y = 800 + random.randint(5, 10) + self.deviation[1] elif level == "1": x = 1440 + random.randint(5, 10) + self.deviation[0] y = 805 + random.randint(5, 10) + self.deviation[1] if not self.debug: self.mouse.position = (x, y) self.mouse.click(Button.left, 1) self.mouse.position = (raw_x, raw_y) def Call(self): (raw_x, raw_y) = self.mouse.position x = 1500 + random.randint(5, 10) + self.deviation[0] y = 1000 + random.randint(5, 10) + self.deviation[1] if not self.debug: self.mouse.position = (x, y) self.mouse.click(Button.left, 1) self.mouse.position = (raw_x, raw_y) def Allin(self): (raw_x, raw_y) = self.mouse.position x = 1320 + random.randint(5, 10) + self.deviation[0] y = 950 + random.randint(5, 10) + self.deviation[1] #x = 1320 + random.randint(5, 10) + self.deviation[0] #y = 850 + random.randint(5, 10) + self.deviation[1] if not self.debug: self.mouse.position = (x, y) self.mouse.press(Button.left) time.sleep(0.2) self.mouse.move(0, -200) time.sleep(0.1) self.mouse.move(0, -200) time.sleep(0.1) self.mouse.move(0, -200) time.sleep(0.2) self.mouse.release(Button.left) self.mouse.position = (raw_x, raw_y)
class LaserTracker(object): def __init__(self, cam_width=640, cam_height=480, hue_min=20, hue_max=160, sat_min=100, sat_max=255, val_min=200, val_max=256, display_thresholds=False): """ * ``cam_width`` x ``cam_height`` -- This should be the size of the image coming from the camera. Default is 640x480. HSV color space Threshold values for a RED laser pointer are determined by: * ``hue_min``, ``hue_max`` -- Min/Max allowed Hue values * ``sat_min``, ``sat_max`` -- Min/Max allowed Saturation values * ``val_min``, ``val_max`` -- Min/Max allowed pixel values If the dot from the laser pointer doesn't fall within these values, it will be ignored. * ``display_thresholds`` -- if True, additional windows will display values for threshold image channels. """ self.cam_width = cam_width self.cam_height = cam_height self.hue_min = hue_min self.hue_max = hue_max self.sat_min = sat_min self.sat_max = sat_max self.val_min = val_min self.val_max = val_max self.display_thresholds = display_thresholds self.capture = None # camera capture device self.channels = { 'hue': None, 'saturation': None, 'value': None, 'laser': None, } self.mouse = Controller() self.corners=[False,False,False,False] #TL, TR, BR, BL self.refPts=[] global screenRoot self.wRatio = 1/(self.cam_width/screenRoot.winfo_screenwidth()) self.hRatio = 1/(self.cam_height/screenRoot.winfo_screenheight()) print('Cam resolution : {} x {}'.format(self.cam_width, self.cam_height)) print('Screen resolution : {} x {}'.format(screenRoot.winfo_screenwidth(), screenRoot.winfo_screenheight())) print('calculated ratio {} x {}'.format(self.wRatio, self.hRatio)) self.emulate = Emulate() self.previous_pos = [] def simulateMouseClick(self, pos): #Cursor position is integer (current pixel) intPos = (int(self.wRatio * pos[0]), int(self.hRatio * pos[1])) self.mouse.position = intPos # Click the left button x = [p[0] for p in self.previous_pos] y = [p[1] for p in self.previous_pos] centroid = (numpy.median(x), numpy.median(y)) #print('{} - {}'.format(centroid, self.previous_pos)) flag = (abs(pos[0]-centroid[0])<5 or abs(pos[1]-centroid[1])<5 ) if self.emulate.seen and self.emulate.count > 30 and flag: self.mouse.press(Button.left) self.emulate.click_on = True def on_mouse(self, event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: if not self.corners[0]: self.corners[0]=True self.refPts=[(x, y)] elif not self.corners[1]: self.corners[1]=True self.refPts.append((x, y)) elif not self.corners[2]: self.corners[2]=True self.refPts.append((x, y)) elif not self.corners[3]: self.corners[3]=True self.refPts.append((x, y)) def create_and_position_window(self, name, xpos, ypos): """Creates a named widow placing it on the screen at (xpos, ypos).""" # Create a window cv2.namedWindow(name) # Resize it to the size of the camera image cv2.resizeWindow(name, self.cam_width, self.cam_height) # Move to (xpos,ypos) on the screen cv2.moveWindow(name, xpos, ypos) def setup_camera_capture(self, device_num=0): """Perform camera setup for the device number (default device = 0). Returns a reference to the camera Capture object. """ try: device = int(device_num) sys.stdout.write("Using Camera Device: {0}\n".format(device)) except (IndexError, ValueError): # assume we want the 1st device device = 0 sys.stderr.write("Invalid Device. Using default device 0\n") # Try to start capturing frames self.capture = cv2.VideoCapture(device) if not self.capture.isOpened(): sys.stderr.write("Faled to Open Capture device. Quitting.\n") sys.exit(1) # set the wanted image size from the camera self.capture.set( cv2.cv.CV_CAP_PROP_FRAME_WIDTH if cv2.__version__.startswith('2') else cv2.CAP_PROP_FRAME_WIDTH, self.cam_width ) self.capture.set( cv2.cv.CV_CAP_PROP_FRAME_HEIGHT if cv2.__version__.startswith('2') else cv2.CAP_PROP_FRAME_HEIGHT, self.cam_height ) return self.capture def handle_quit(self, delay=10): """Quit the program if the user presses "Esc" or "q".""" key = cv2.waitKey(delay) c = chr(key & 255) if c in ['c', 'C']: self.corners=[False,False,False,False] #TL, TR, BR, BL self.refPts=[] if c in ['q', 'Q', chr(27)]: sys.exit(0) def threshold_image(self, channel): if channel == "hue": minimum = self.hue_min maximum = self.hue_max elif channel == "saturation": minimum = self.sat_min maximum = self.sat_max elif channel == "value": minimum = self.val_min maximum = self.val_max (t, tmp) = cv2.threshold( self.channels[channel], # src maximum, # threshold value 0, # we dont care because of the selected type cv2.THRESH_TOZERO_INV # t type ) (t, self.channels[channel]) = cv2.threshold( tmp, # src minimum, # threshold value 255, # maxvalue cv2.THRESH_BINARY # type ) if channel == 'hue': # only works for filtering red color because the range for the hue # is split self.channels['hue'] = cv2.bitwise_not(self.channels['hue']) def track(self, frame, mask): """ Track the position of the laser pointer. Code taken from http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/ """ center = None # cv2.RETR_EXTERNAL countours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] # only proceed if at least one contour was found if len(countours) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(countours, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) moments = cv2.moments(c) if moments["m00"] > 0: center = int(moments["m10"] / moments["m00"]), \ int(moments["m01"] / moments["m00"]) else: center = int(x), int(y) # only proceed if the radius meets a minimum size if radius > 5 and radius < 20: # draw the circle and centroid on the frame, cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) self.emulate.seen = True self.emulate.count = self.emulate.count + 1 self.previous_pos.append(center) self.simulateMouseClick(center) else: if self.emulate.click_on: self.mouse.release(Button.left) self.emulate.reset() self.previous_pos = [] def detect(self, frame): hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # split the video frame into color channels h, s, v = cv2.split(hsv_img) self.channels['hue'] = h self.channels['saturation'] = s self.channels['value'] = v # Threshold ranges of HSV components; storing the results in place self.threshold_image("hue") self.threshold_image("saturation") self.threshold_image("value") # Perform an AND on HSV components to identify the laser! self.channels['laser'] = cv2.bitwise_and( self.channels['hue'], self.channels['value'] ) ''' self.channels['laser'] = cv2.bitwise_and( self.channels['saturation'], self.channels['laser'] ) ''' # Merge the HSV components back together. hsv_image = cv2.merge([ self.channels['hue'], self.channels['saturation'], self.channels['value'], ]) self.track(frame, self.channels['laser']) return hsv_image def display(self, img, frame): """Display the combined image and (optionally) all other image channels NOTE: default color space in OpenCV is BGR. """ cv2.imshow('RGB_VideoFrame', frame) cv2.imshow('LaserPointer', self.channels['laser']) if self.display_thresholds: cv2.imshow('Thresholded_HSV_Image', img) #cv2.imshow('Hue', self.channels['hue']) #cv2.imshow('Saturation', self.channels['saturation']) #cv2.imshow('Value', self.channels['value']) def setup_windows(self): sys.stdout.write("Using OpenCV version: {0}\n".format(cv2.__version__)) # create output windows self.create_and_position_window('LaserPointer', 0, 0) self.create_and_position_window('RGB_VideoFrame', 10 + self.cam_width, 0) if self.display_thresholds: self.create_and_position_window('Thresholded_HSV_Image', 10, 10 + self.cam_height) #self.create_and_position_window('Hue', 20, 20) #self.create_and_position_window('Saturation', 30, 30) #self.create_and_position_window('Value', 40, 40) def isCalibrate(self, frame): cv2.setMouseCallback('RGB_VideoFrame', self.on_mouse) for i in range(4): if self.corners[i]: cv2.circle(frame, self.refPts[i], 5, (0,255,0), 1) if self.corners[0] and self.corners[1] and self.corners[2] and self.corners[3]: warped = four_point_transform(frame, numpy.array(self.refPts, dtype = "float32"), (self.cam_width, self.cam_height)) return warped return frame def run(self): # Set up window positions self.setup_windows() # Set up the camera capture self.setup_camera_capture() # 1. capture the current image success, frame = self.capture.read() if not success: # no image captured... end the processing sys.stderr.write("Could not read camera frame. Quitting\n") sys.exit(1) while(self.capture.isOpened()): success, frame = self.capture.read() frame = self.isCalibrate(frame) hsv_image = self.detect(frame) self.display(hsv_image, frame) self.handle_quit()
print(mouse.position) time.sleep(3) print('The current pointer position is {0}'.format(mouse.position)) #set pointer positon mouse.position = (277, 645) print('now we have moved it to {0}'.format(mouse.position)) #鼠标移动(x,y)个距离 #param int x: The horizontal offset. #param int dy: The vertical offset. mouse.move(5, -5) print(mouse.position) mouse.press(Button.left) mouse.release(Button.left) mouse.press(Button.right) mouse.release(Button.right) #Double click #param int count: The number of clicks to send. mouse.click(Button.left, 2) #scroll two steps down #param int dx: The horizontal scroll. #param int dy: The vertical scroll. mouse.scroll(0, 500) # mouse listen '''
while True: try: data, addr = serversocket.recvfrom(64) if data != "": # print(addr[0]) time.sleep(0.0145) datas = [] datas = data.split("/") # if mouse if (datas[0] == "mouse"): from pynput.mouse import Button, Controller mouse = Controller() if (datas[1] == "left"): mouse.press(Button.left) mouse.release(Button.left) elif (datas[1] == "right"): mouse.press(Button.right) mouse.release(Button.right) elif (datas[1] == "scroll"): mouse.scroll(0, int(datas[2])) else: mouseX, mouseY = (mouse.position) mouse.position = (mouseX + (int(datas[1]) / 10), (mouseY + int(datas[2]) / 10)) else: if (datas[0] == "keyboard"): from pynput.keyboard import Key, Controller keyboard = Controller() if (datas[1]) == "Backspace": keyboard.press(Key.backspace)
class AutoClickerThread(Thread): def __init__(self, parent_ui=None, config=Config()): super(AutoClickerThread, self).__init__() self.ui = parent_ui self.config = config self.thread = None self._stop_event = Event() self.mouse_button = None self.current_keys = set() self.accepted_keys = set() self.reload_config() self.activated = False self.mouse_controller = MouseController() self.keyboard_controller = KeyboardController() self.last_state = False self.sequence_index = 0 self.sequence_length = len(self.config.output_sequence) def stop(self): self._stop_event.set() def stopped(self): return self._stop_event.is_set() def run(self): self.thread.start() while not self.stopped(): if self.activated: if self.config.output_type == 0: # Mouse output to_press = Button.right if self.config.mouse_output == 1 else Button.left self.mouse_controller.press(to_press) self.mouse_controller.release(to_press) elif self.config.output_type == 1 and self.config.output_sequence: # Keyboard output current_key = self.config.output_sequence[self.sequence_index] to_press = KeyCode.from_char(current_key) self.keyboard_controller.press(to_press) time.sleep(self.config.hold_time) self.keyboard_controller.release(to_press) self.sequence_index = (self.sequence_index + 1) % self.sequence_length if self.config.distribution_type == 0: time.sleep(self.config.wait_time + random.uniform(0, self.config.deviation_time)) else: time.sleep(abs(np.random.normal(loc=self.config.wait_time, scale=self.config.deviation_time))) self.last_state = self.activated self.thread.stop() def set_activated(self, value): if self.ui: self.ui.update_clicking(value) self.activated = value def on_press(self, key): if key in self.accepted_keys: self.current_keys.add(key) if self.accepted_keys.issubset(self.current_keys): if self.config.toggle: self.set_activated(not self.activated) else: self.set_activated(True) def on_click(self, x, y, button, pressed): if button == self.mouse_button: if self.config.toggle and pressed: self.set_activated(not self.activated) elif not self.config.toggle and pressed: self.set_activated(True) elif not self.config.toggle and not pressed: self.set_activated(False) def on_release(self, key): try: if key in self.accepted_keys: if not self.config.toggle: self.set_activated(False) self.current_keys.remove(key) except KeyError: pass def set_config(self, config): self.config = config self.reload_config() def reload_config(self): self.accepted_keys = set() if self.config.alt_modifier: self.accepted_keys.add(Key.alt_l) for key in self.config.key_combination: self.accepted_keys.add(KeyCode.from_char(key)) self.thread = {0: KeyboardListener(on_press=self.on_press, on_release=self.on_release), 1: MouseListener(on_click=self.on_click)}.get(self.config.input_mode) self.mouse_button = {0: Button.x1, 1: Button.x2}.get(self.config.special_mouse_press) self.sequence_length = len(self.config.output_sequence)
mouse = Controller() from pynput.keyboard import Key, Controller keyboard = Controller() ArduinoSerial = serial.Serial('/dev/cu.smart_control-SPPDev',57600) # create serial object called ArduinoSerial time.sleep(2); # wait for 2 seconds for the communication to get established while 1: incoming = ArduinoSerial.readline() data = str(incoming.decode('utf-8')) # decode data # use 'if' because there is no 'switch-case' in Python! # swipe left if 'Normal LEFT' in data: keyboard.press(Key.left) keyboard.release(Key.left) # swipe right if 'Normal RIGHT' in data: keyboard.press(Key.right) keyboard.release(Key.right) # scroll up if 'Normal UP' in data: mouse.scroll(0,100) # scroll down if 'Normal DOWN' in data: mouse.scroll(0,-100) # escape and change to other opened tab
def run(): """ Use the webcam to capture the Pictionary Air pen and control the mouse pointer. """ vc = cv2.VideoCapture(0) is_capturing = vc.isOpened() if is_capturing: # setup canvas cam_width = int(vc.get(cv2.CAP_PROP_FRAME_WIDTH)) cam_height = int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT)) # find screen dimensions root = tk.Tk() screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() factor_x = screen_width / cam_width factor_y = screen_height / cam_height mouse = Controller() running = True pressed = False while running and is_capturing: try: is_capturing, frame = vc.read() frame = cv2.flip(frame, 1) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # red mask # setting the lower and upper range for mask1 lower_red = np.array([0, 100, 100]) upper_red = np.array([20, 255, 255]) mask1 = cv2.inRange(hsv, lower_red, upper_red) # setting the lower and upper range for mask2 lower_red = np.array([160, 100, 100]) upper_red = np.array([180, 255, 255]) mask2 = cv2.inRange(hsv, lower_red, upper_red) mask_red = mask1 + mask2 _, contours, _ = cv2.findContours(mask_red.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # if the contours are formed if len(contours) > 0: # find the largest contour contour = sorted(contours, key=cv2.contourArea, reverse=True)[0] # get the radius and position of the enclosing circle ((x, y), _) = cv2.minEnclosingCircle(contour) x, y = int(x), int(y) # move the mouse mouse.position = (int(x * factor_x), int(y * factor_y)) # release the button is not released already if pressed: pressed = False mouse.release(Button.left) else: # green mask # setting the lower and upper range for mask_green lower_green = np.array([55, 40, 40]) upper_green = np.array([95, 255, 255]) mask_green = cv2.inRange(hsv, lower_green, upper_green) _, contours, _ = cv2.findContours(mask_green.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # if the contours are formed if len(contours) > 0: # find the largest contour contour = sorted(contours, key=cv2.contourArea, reverse=True)[0] # get the radius and position of the enclosing circle ((x, y), _) = cv2.minEnclosingCircle(contour) x, y = int(x), int(y) # move the mouse mouse.position = (int(x * factor_x), int(y * factor_y)) # press the button is not pressed already if not pressed: pressed = True mouse.press(Button.left) except KeyboardInterrupt: running = False is_capturing = False vc.release()
width = 1920 height = 1080 _width = int(math.ceil(float(width) / size)) _height = int(math.ceil(float(height) / size)) def index2coord(i): quotient, rem = divmod(i, _width) return ((rem % _width) * size, (quotient % _height) * size) index = 0 start = time.time() end = start while end - start < totalTime: x, y = index2coord(index) x += random.randint(-20, 20) y += random.randint(-20, 20) f = lambda z: max(0, min(width, z)) # move mouse mouse.position = (f(x), f(y)) mouse.press(Button.left) mouse.release(Button.left) end = time.time() index += 1
def arcball(window, target, helper): sleep(2) mouse = Controller() if target['shape']['type'] == 'rect': position = (window['x'] + target['shape']['x'] + target['shape']['width'] / 2, \ window['y'] + target['shape']['y'] + target['shape']['height'] / 2) elif target['shape']['type'] == 'circ': position = (window['x'] + target['shape']['centerX'], \ window['y'] + target['shape']['centerY']) elif target['shape']['type'] == 'poly': position = (window['x'] + target['shape']['centerX'], \ window['y'] + target['shape']['centerY']) short_delay = 0.1 pitch = 20 mouse.position = position sleep(0.1) mouse.press(Button.left) sleep(0.1) mouse.release(Button.left) for x in range(1, 26): print(position) mouse.position = position sleep(0.5) mouse.press(Button.left) for i in range(pitch / 2): mouse.position = (position[0], position[1] + i * 16) sleep(short_delay) mouse.release(Button.left) sleep(1) mouse.position = position sleep(0.1) mouse.press(Button.left) for i in range(pitch): mouse.position = (position[0], position[1] - i * 16) sleep(short_delay) screenshot(window) mouse.release(Button.left) # reset functionality # move mouse to helper # click helper # move back sleep(0.5) if helper['shape']['type'] == 'rect': mouse.position = (window['x'] + helper['shape']['x'] + helper['shape']['width'] / 2, \ window['y'] + helper['shape']['y'] + helper['shape']['height'] / 2) elif helper['shape']['type'] == 'circ': mouse.position = (window['x'] + helper['shape']['centerX'], \ window['y'] + helper['shape']['centerY']) elif helper['shape']['type'] == 'poly': mouse.position = (window['x'] + helper['shape']['centerX'], \ window['y'] + helper['shape']['centerY']) sleep(0.5) mouse.press(Button.left) sleep(0.1) mouse.release(Button.left) sleep(0.5) mouse.position = position sleep(1) mouse.position = position sleep(0.1) mouse.press(Button.left) #for i in range(5): mouse.position = (position[0] + x * (43), position[1]) sleep(short_delay) sleep(0.5) mouse.release(Button.left) sleep(1)