def showMouseLoc(self): try: m = PyMouse() # make mouse object while(1): print m.position() # print the position except KeyboardInterrupt: 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 move_down(): m = PyMouse() xt=random.randint(1400, 1600) yt=random.randint(700, 800) m.position() m.move(xt, yt)
def run(self): global mself global runflag m = PyMouse() #raw_input("请先将鼠标移动到数据位置的左上角,然后按下回车.") wx.MessageBox("请不要点击该对话框的确定按钮\n先将鼠标移动到数据位置的左上角,然后按下回车...", "获取数据位置", wx.OK) p1 = m.position() print p1 #raw_input("接下来将鼠标移动到数据位置的右下角,然后按下回车.") wx.MessageBox("接下来将鼠标移动到数据位置的右下角,然后按下回车...", "获取数据位置", wx.OK) p2 = m.position() print p2 print "完成数据位设定,开始获取数据工作." imbox = (p1[0], p1[1], p2[0], p2[1]) prev_s = [0,0,0,0] while runflag: try: im = ImageGrab.grab(imbox) s = image_to_string(im) s = s.split() if s != prev_s and len(s)==4 : sell = float(s[0]) - float(prev_s[0]) buy = float(s[2]) - float(prev_s[2]) prev_s = s print sell, buy mself.staticText1.SetLabel(mself.staticText2.GetLabel()) mself.staticText2.SetLabel(mself.staticText3.GetLabel()) mself.staticText3.SetLabel(mself.staticText4.GetLabel()) mself.staticText4.SetLabel(mself.staticText5.GetLabel()) mself.staticText5.SetLabel(mself.staticText6.GetLabel()) mself.staticText6.SetLabel(mself.staticText7.GetLabel()) mself.staticText7.SetLabel(mself.staticText8.GetLabel()) mself.staticText8.SetLabel(mself.staticText9.GetLabel()) mself.staticText9.SetLabel(mself.staticText10.GetLabel()) mself.staticText10.SetLabel("%.1f"%sell) mself.staticText11.SetLabel(mself.staticText12.GetLabel()) mself.staticText12.SetLabel(mself.staticText13.GetLabel()) mself.staticText13.SetLabel(mself.staticText14.GetLabel()) mself.staticText14.SetLabel(mself.staticText15.GetLabel()) mself.staticText15.SetLabel(mself.staticText16.GetLabel()) mself.staticText16.SetLabel(mself.staticText17.GetLabel()) mself.staticText17.SetLabel(mself.staticText18.GetLabel()) mself.staticText18.SetLabel(mself.staticText19.GetLabel()) mself.staticText19.SetLabel(mself.staticText20.GetLabel()) mself.staticText20.SetLabel("%.1f"%buy) im.save("show.jpg") pic=wx.Image("show.jpg") w,h = mself.staticBitmap1.GetSize() mself.staticBitmap1.SetBitmap(pic.Rescale(w,h).ConvertToBitmap()) except: traceback.print_exc() wx.MessageBox("无法正常识别,请重新开始.注意数据截取位置.", "提醒", wx.OK) runflag = 0
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 press_left(t=1.0) : m = PyMouse() xt=random.randint(1400, 1600) yt=random.randint(260, 500) m.position() m.move(xt, yt) time.sleep(0.2) m.press(xt, yt) time.sleep(t) m.release(xt, yt)
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 test_move(self): for size in screen_sizes: with Display(visible=VISIBLE, size=size): mouse = PyMouse() for p in positions: mouse.move(*p) eq_(expect_pos(p, size), mouse.position())
class MenuOjoData: DEGREEE_PATTERN = r"(?P<degree_data>[+-]\d{2})" def __init__(self): self.driver = PantojoBluetoothReceiver() self.pipes = [PantojoRealDataPipe()] self.mouse = PyMouse() (self.x_max, self.y_max) = self.mouse.screen_size() self.degree = re.compile(self.DEGREEE_PATTERN, re.VERBOSE) def open(self): self.driver.open() def recv(self): data = self.driver.recv() for pipe in self.pipes: data = pipe.apply(data) matched = self.degree.match(data) if matched: valor = int(data) (x, y) = self.mouse.position() if (valor != 0): #asumo que se mueve de a 15 mov = (self.x_max / 7) * (valor / 15) + x self.mouse.move(mov, y) else: self.mouse.move(self.x_max / 2, y) return data def close(self): self.driver.close()
class MouseLogger: def __init__(self): self.mouse = PyMouse() def recv(self): return self.mouse.position()
def ut_showMouseLoc(self): points = [(120,120), (600,480), (500,1000), (800,200)] m = PyMouse() # make mouse object for point in points: print "Moving to: ", point m.move(point[0],point[1]) print "The Mouse is at:", m.position() print "Test complete!"
def test_move(self): for size in screen_sizes: with Display(visible=VISIBLE, size=size) as d: time.sleep(0.5) mouse = PyMouse(display=d.new_display_var) for p in positions: mouse.move(*p) eq_(expect_pos(p, size), mouse.position())
def zoom_out(): m = PyMouse() (x, y) = m.position() m.move(700, 700) press() # m.move(200, 100) # m.move(300, 100) # m.move(400, 100) # m.move(500, 100) release()
def test_move(self): for size in screen_sizes: try: disp = Display(visible=VISIBLE, size=size).start() mouse = PyMouse() for p in positions: mouse.move(*p) eq_(expect_pos(p, size), mouse.position()) finally: disp.stop()
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 ActivityCheckLoop(): def __init__(self): self.lastPos = None self.last_interaction = datetime.datetime.now() self.platform = helpers.get_platform() self.m = PyMouse() def run(self): # if (self.platform == 'windows'): # flags, hcursor, newpos = win32gui.GetCursorInfo() newpos = self.m.position() if newpos != self.lastPos: self.lastPos = newpos self.last_interaction = datetime.datetime.now()
def flicker(self): """ This method simply jiggles the mouse. It is used as part of a workaround for LoopingCall's getting stuck on the first call when using qt4reactor on linux. """ from platform import system if (self.activeWindow is not None) and system() == "Linux": from pymouse import PyMouse m = PyMouse() x, y = m.position() m.move(x + 1, y)
def flicker(self): """ This method simply jiggles the mouse. It is used as part of a workaround for LoopingCall's getting stuck on the first call when using qt4reactor. """ w = self.get_active_window() from platform import system if w and system() == "Linux": from pymouse import PyMouse m = PyMouse() x,y = m.position() m.move(x + self.flickerDelta, y) self.flickerDelta *= -1
def reg(nu,name,call): m = PyMouse() poz = m.position() #dealer##box = (poz[0] -15,poz[1]-5,poz[0]+15,poz[1]+5) box = (poz[0] -35,poz[1]-15,poz[0]+35,poz[1]+15) got = False for t in call: if t[0] == nu: t[1].append([nu+name+'.png',box]) got = True break if not got: call.append([nu,[[nu+name+'.png',box]]]) ImageGrab.grab(bbox=box).save('add/%s'%(nu+name+'.png'),'BMP')
class KLMouse(object): ABSOLUTE = 1, RELATIVE = 2, def __init__(self): self.m = PyMouse() self.t = 0 self.lastX = 0 self.lastY = 0 self.mode = KLMouse.RELATIVE def mouse(self, evType, x, y, t): print evType, x, y, t if t > self.t: xDim, yDim = self.m.screen_size() if self.mode == KLMouse.ABSOLUTE: xNew = int(float(x) * xDim) yNew = int(float(y) * yDim) else: xNew = int(float(x) * xDim) yNew = int(float(y) * yDim) print xNew print yNew mX, mY = self.m.position() if evType == 'left' or evType == 'right': mX, mY = self.m.position(); self.m.click(mX, mY, {'left':1,'right':2}[evType]) else: if self.mode == KLMouse.ABSOLUTE: self.m.move(xNew, yNew) if self.mode == KLMouse.RELATIVE: self.m.move(mX + xNew, mY + yNew) self.t = t
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()
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 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 MouseWatcher(Thread): def __init__(self, listener=MouseListener()): Thread.__init__(self) self.m = PyMouse() self.ui = UInput() self.devices = [] self.running = True self.lastPosition = None self.device_search() self.listener = listener def stop(self): print "Desligando mouse." self.running = False def device_search(self): devices_list = [] files = [] for (dirpath, dirnames, filenames) in walk(INPUTDIR): files.extend(filenames) break for f in files: try: dev = InputDevice(path.join(INPUTDIR, f)) keymap = dev.capabilities().get(1) if ecodes.BTN_MOUSE in keymap and ecodes.BTN_LEFT in keymap: devices_list.append(dev) except (IOError, OSError, TypeError): pass self.devices = {dev.fd: dev for dev in devices_list} def run(self): while self.running: r,w,x = select(self.devices, [], []) for fd in r: if not self.running: break for event in self.devices[fd].read(): if event.type == ecodes.EV_KEY and event.value == 1: self.lastPosition = self.m.position() if self.listener.__class__ != MouseListener: self.listener.call_me(self.lastPosition) self.ui.close()
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
class Mouse_trace: """Mouse tracing Class. It uses vispy to visualization.""" def __init__(self, color = (1.0, 1.0, 1.0, 1.0)): self.mouse = PyMouse() self.pos = np.asarray([0, 0, 0]) self.color = color self.rad = 20.0 size = self.mouse.screen_size() boundaries = np.asarray([[0, size[0]], [0, size[1]], [0, 1]]) print(boundaries) self.sizexyz = [None] * 3 self.tail_steps = 200 self.set_bound(boundaries) self.visual = [None] def set_bound(self, boundaries): """Updates the boundaries.""" self.bound = boundaries self.sizexyz = np.abs(boundaries[:,1] - boundaries[:,0]) def step(self, time_step): """Calculate the new position and speed.""" mpos = self.mouse.position() self.pos = np.asarray([mpos[0], self.bound[1,1] - mpos[1], 0]) self.update_visual() def init_visual(self, view): """Initialize the object visual.""" self.trace = np.repeat(self.pos, self.tail_steps).reshape((3,self.tail_steps)).T pos = np.concatenate([sphere_pt * self.rad + self.pos, self.trace]) self.visual = Line(pos = pos, color=self.color) view.add(self.visual) def update_visual(self): """Updates the object visual.""" self.trace[1:] = self.trace[0:-1] self.trace[0] = self.pos pos = np.concatenate([sphere_pt * self.rad + self.pos, self.trace]) self.visual.set_data(pos = pos) def shake(self): """Inverts the z position and gives the ball a random velocity.""" pass
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
def main(): try: from pymouse import PyMouseEvent class event(PyMouseEvent): def move(self, x, y): print("Mouse moved to", x, y) 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.capture = True e.start() except ImportError: print("Mouse events are not yet supported on your platform") m = PyMouse() try: size = m.screen_size() print("size: %s" % (str(size))) i_pos = m.position() print("current mouse postion is :%s " % (str(i_pos))) # print(i_pos) # pos = (random.randint(0, size[0]), random.randint(0, size[1])) except: print("error occur") try: e.stop() except: pass
# @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')
def GetPosition(): while 1 == 1: m = PyMouse() tmp = m.position() print(tmp) time.sleep(2)
# -*- coding: utf-8 -*- """ ------------------------------------------------- File Name: main.py Description : Author : gongmb date: 2017/4/1 ------------------------------------------------- Change Activity: 2017/4/1: ------------------------------------------------- """ import time __author__ = 'gongmb' from pymouse import PyMouse m = PyMouse() m.position() # 获取当前的鼠标坐标 time.sleep(1) m.move(1153, 538) time.sleep(1) m.click(1153, 538) # 模拟点击 time.sleep(1) m.press(1153, 538) # 按下鼠标 time.sleep(1) m.release(1153, 538) # 释放鼠标 time.sleep(1)
class pilot(wx.Frame): def __init__(self, parent, id): self.winWidth, self.winHeight = wx.DisplaySize() self.initializeParameters() wx.Frame.__init__(self, parent, id, 'moviePilot', size=(self.width, self.height), pos=(self.winWidth - self.width - self.xBorder * (self.numberOfColumns[0] - 2), self.winHeight - self.height - self.xBorder * (self.numberOfRows[0] - 6))) self.SetBackgroundColour('black') style = self.GetWindowStyle() self.SetWindowStyle(style | wx.STAY_ON_TOP) self.parent = parent self.MakeModal(True) self.initializeBitmaps() self.createGui() self.createBindings() self.initializeTimer() #------------------------------------------------------------------------- def initializeParameters(self): with open('.pathToAP', 'r') as textFile: self.pathToAP = textFile.readline() sys.path.append(self.pathToAP) from reader import reader reader = reader() reader.readParameters() parameters = reader.getParameters() for item in parameters: try: setattr(self, item[:item.find('=')], int(item[item.find('=') + 1:])) except ValueError: setattr(self, item[:item.find('=')], item[item.find('=') + 1:]) self.flag = 'row' self.pressFlag = False self.pressedStopFlag = False self.rowIteration = 0 self.colIteration = 0 self.numberOfColumns = 2, self.numberOfRows = 7, self.numberOfEmptyIteration = 0 self.countRows = 0 self.countColumns = 0 self.countMaxRows = 2 self.countMaxColumns = 2 self.numberOfPresses = 0 self.trackCounter = 0 self.initCount = 0 #it's better to use another timer than conditions self.initCount2 = 0 self.mouseCursor = PyMouse() if self.control != 'tracker': self.mousePosition = self.winWidth - 8 - self.xBorder, self.winHeight - 48 - self.yBorder self.mouseCursor.move(*self.mousePosition) if self.switchSound.lower() == 'on' or self.pressSound.lower() == 'on': mixer.init() if self.switchSound.lower() == 'on': self.switchingSound = mixer.Sound(self.pathToAP + '/sounds/switchSound.ogg') if self.pressSound.lower() == 'on': self.pressingSound = mixer.Sound(self.pathToAP + '/sounds/pressSound.ogg') self.width = self.numberOfColumns[0] * 120 self.height = self.numberOfRows[0] * 100 #------------------------------------------------------------------------- def initializeBitmaps(self): buttonPaths = glob.glob(self.pathToAP + 'icons/pilots/moviePilot/*') #labelFiles self.buttons = {} for buttonPath in buttonPaths: buttonBitmap = wx.BitmapFromImage( wx.ImageFromStream(open(buttonPath, "rb"))) buttonLabel = buttonPath[buttonPath.rfind('/') + 1:buttonPath.rfind('.')] try: buttonPosition = int(buttonLabel.split('_')[0]) buttonName = buttonLabel[buttonLabel.find('_') + 1:] self.buttons[buttonPosition] = [buttonName, buttonBitmap] except ValueError: print 'Symbol %s w folderze %s ma nieprawidłową nazwę.' % ( symbolName.split('_')[0], page[page.rfind('/') + 1:]) pass #------------------------------------------------------------------------- def createGui(self): self.mainSizer = wx.BoxSizer(wx.VERTICAL) self.subSizer = wx.GridBagSizer(self.xBorder, self.yBorder) if self.control != 'tracker': event = eval('wx.EVT_LEFT_DOWN') else: event = eval('wx.EVT_BUTTON') for key, value in self.buttons.items(): if key == 1 or key == 2 or key == 3 or key == 4: b = bt.GenBitmapButton(self, -1, name=value[0], bitmap=value[1]) b.SetBackgroundColour(self.backgroundColour) b.SetBezelWidth(3) b.Bind(event, self.onPress) self.subSizer.Add(b, ((key - 1) / self.numberOfColumns[0], (key - 1) % self.numberOfColumns[0]), wx.DefaultSpan, wx.EXPAND) elif key == 5: b = bt.GenBitmapButton(self, -1, name=value[0], bitmap=value[1]) b.SetBackgroundColour(self.backgroundColour) b.SetBezelWidth(3) b.Bind(event, self.onPress) self.subSizer.Add(b, ((key - 1) / self.numberOfColumns[0], (key - 1) % self.numberOfColumns[0]), (1, 2), wx.EXPAND) elif key == 6 or key == 7 or key == 8 or key == 9 or key == 10 or key == 11: b = bt.GenBitmapButton(self, -1, name=value[0], bitmap=value[1]) b.SetBackgroundColour(self.backgroundColour) b.SetBezelWidth(3) b.Bind(event, self.onPress) self.subSizer.Add(b, ((key) / self.numberOfColumns[0], (key) % self.numberOfColumns[0]), wx.DefaultSpan, wx.EXPAND) elif key == 12: b = bt.GenBitmapButton(self, -1, name=value[0], bitmap=value[1]) b.SetBackgroundColour(self.backgroundColour) b.SetBezelWidth(3) b.Bind(event, self.onPress) self.subSizer.Add(b, ((key) / self.numberOfColumns[0], (key) % self.numberOfColumns[0]), (1, 2), wx.EXPAND) for number in range(self.numberOfRows[0]): self.subSizer.AddGrowableRow(number) for number in range(self.numberOfColumns[0]): self.subSizer.AddGrowableCol(number) self.mainSizer.Add(self.subSizer, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM, border=self.xBorder) self.SetSizer(self.mainSizer) #------------------------------------------------------------------------- def initializeTimer(self): self.stoper = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.timerUpdate, self.stoper) # if self.control != 'tracker': self.stoper.Start(self.timeGap) #------------------------------------------------------------------------- def createBindings(self): self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) #------------------------------------------------------------------------- def OnCloseWindow(self, event): if self.control != 'tracker': if True in [ 'debian' in item for item in os.uname() ]: #POSITION OF THE DIALOG WINDOW DEPENDS ON WINDOWS MANAGER NOT ON DESKTOP ENVIROMENT. THERE IS NO REASONABLE WAY TO CHECK IN PYTHON WHICH WINDOWS MANAGER IS CURRENTLY RUNNING, BESIDE IT IS POSSIBLE TO FEW WINDOWS MANAGER RUNNING AT THE SAME TIME. I DON'T SEE SOLUTION OF THIS ISSUE, EXCEPT OF CREATING OWN SIGNAL (AVR MICROCONTROLLERS). if os.environ.get('KDE_FULL_SESSION'): self.mousePosition = self.winWidth / 1.05, self.winHeight / 1.6 # elif ___: #for gnome-debian # self.mousePosition = self.winWidth/6.5, self.winHeight/6. else: self.mousePosition = self.winWidth / 1.07, self.winHeight / 1.61 else: self.mousePosition = self.winWidth / 1.12, self.winHeight / 1.6 self.mouseCursor.move(*self.mousePosition) dial = wx.MessageDialog( self, 'Czy napewno chcesz wyjść z programu?', 'Wyjście', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION | wx.STAY_ON_TOP) ret = dial.ShowModal() if ret == wx.ID_YES: try: if "smplayer" in [ psutil.Process(i).name() for i in psutil.pids() ]: os.system('smplayer -send-action quit') except TypeError: if "smplayer" in [ psutil.Process(i).name for i in psutil.pids() ]: os.system('smplayer -send-action quit') try: self.parent.parent.parent.Destroy() self.parent.parent.Destroy() self.parent.Destroy() self.Destroy() except AttributeError: try: self.parent.parent.Destroy() self.parent.Destroy() self.Destroy() except AttributeError: try: self.parent.Destroy() self.Destroy() except AttributeError: self.Destroy() else: event.Veto() if self.control != 'tracker': self.mousePosition = self.winWidth - 8 - self.xBorder, self.winHeight - 8 - self.yBorder self.mouseCursor.move(*self.mousePosition) #------------------------------------------------------------------------- def onExit(self): if __name__ == '__main__': self.stoper.Stop() self.Destroy() else: self.stoper.Stop() self.MakeModal(False) self.parent.Show(True) self.parent.stoper.Start(self.parent.timeGap) self.Destroy() #------------------------------------------------------------------------- def onPress(self, event): if self.pressSound.lower() == 'on': self.pressingSound.play() if self.control == 'tracker': if self.pressFlag == False: # self.trackCounter = 0 self.button = event.GetEventObject() self.button.SetBackgroundColour(self.selectionColour) self.pressFlag = True self.label = event.GetEventObject().GetName().encode('utf-8') # self.stoper.Start( 0.1 * self.timeGap ) if self.label == 'volume down': try: recentVolume = alsaaudio.Mixer( control='Master', cardindex=self.card_index).getvolume()[0] alsaaudio.Mixer(control='Master', cardindex=self.card_index).setvolume( recentVolume - 15, 0) time.sleep(1.5) except alsaaudio.ALSAAudioError: self.button.SetBackgroundColour('red') self.button.SetFocus() self.Update() time.sleep(1.5) elif self.label == 'volume up': try: recentVolume = alsaaudio.Mixer( control='Master', cardindex=self.card_index).getvolume()[0] alsaaudio.Mixer(control='Master', cardindex=self.card_index).setvolume( recentVolume + 15, 0) time.sleep(1.5) except alsaaudio.ALSAAudioError: self.button.SetBackgroundColour('red') self.button.SetFocus() self.Update() time.sleep(1.5) elif self.label == 'play pause': if self.pressedStopFlag == True: os.system('smplayer -send-action play') self.pressedStopFlag = False else: os.system('smplayer -send-action pause') elif self.label == 'stop': os.system( 'smplayer -send-action stop && smplayer -send-action stop %% smplayer -send-action fullcreen' ) self.pressedStopFlag = True elif self.label == 'fast backward': os.system('smplayer -send-action rewind3') elif self.label == 'fast forward': os.system('smplayer -send-action forward3') elif self.label == 'backward': os.system('smplayer -send-action rewind2') elif self.label == 'forward': os.system('smplayer -send-action forward2') elif self.label == 'slow backward': os.system('smplayer -send-action rewind1') elif self.label == 'slow forward': os.system('smplayer -send-action forward1') elif self.label == 'expand': os.system( 'wid=`xdotool search --onlyvisible --name SMPlayer` && xdotool windowfocus $wid && xdotool key --window $wid f &&wid=`xdotool search --onlyvisible --name moviePilot` && xdotool windowactivate $wid' ) elif self.label == 'back': if "smplayer" in [ psutil.Process(i).name() for i in psutil.pids() ]: os.system('smplayer -send-action quit') self.onExit() else: self.numberOfPresses += 1 self.numberOfEmptyIteration = 0 if self.numberOfPresses == 1: if self.flag == 'row': if self.rowIteration == 1 or self.rowIteration == 2: buttonsToHighlight = range( (self.rowIteration - 1) * self.numberOfColumns[0], (self.rowIteration - 1) * self.numberOfColumns[0] + self.numberOfColumns[0]) for button in buttonsToHighlight: item = self.subSizer.GetItem(button) b = item.GetWindow() b.SetBackgroundColour(self.selectionColour) b.SetFocus() self.flag = 'columns' self.colIteration = 0 elif self.rowIteration == 3: buttonsToHighlight = (self.rowIteration - 1) * self.numberOfColumns[0], for button in buttonsToHighlight: item = self.subSizer.GetItem(button) b = item.GetWindow() b.SetBackgroundColour(self.selectionColour) b.SetFocus() self.rowIteration = 0 os.system( 'wid=`xdotool search --onlyvisible --name SMPlayer` && xdotool windowfocus $wid && xdotool key --window $wid f &&wid=`xdotool search --onlyvisible --name moviePilot` && xdotool windowactivate $wid' ) elif self.rowIteration == 4 or self.rowIteration == 5 or self.rowIteration == 6: buttonsToHighlight = range( (self.rowIteration - 1) * self.numberOfColumns[0] - 1, (self.rowIteration - 1) * self.numberOfColumns[0] + self.numberOfColumns[0] - 1) for button in buttonsToHighlight: item = self.subSizer.GetItem(button) b = item.GetWindow() b.SetBackgroundColour(self.selectionColour) b.SetFocus() self.flag = 'columns' self.colIteration = 0 elif self.rowIteration == 7: buttonsToHighlight = (self.rowIteration - 1) * self.numberOfColumns[0] - 1, for button in buttonsToHighlight: item = self.subSizer.GetItem(button) b = item.GetWindow() b.SetBackgroundColour(self.selectionColour) b.SetFocus() if "smplayer" in [ psutil.Process(i).name() for i in psutil.pids() ]: os.system('smplayer -send-action quit') self.onExit() elif self.flag == 'columns': if self.rowIteration == 1 or self.rowIteration == 2: self.position = ( self.rowIteration - 1) * self.numberOfColumns[0] + self.colIteration elif self.rowIteration == 4 or self.rowIteration == 5 or self.rowIteration == 6: self.position = ( self.rowIteration - 1 ) * self.numberOfColumns[0] + self.colIteration - 1 item = self.subSizer.GetItem(self.position - 1) selectedButton = item.GetWindow() selectedButton.SetBackgroundColour(self.selectionColour) selectedButton.SetFocus() self.Update() if self.buttons[self.position][0] == 'volume down': try: recentVolume = alsaaudio.Mixer( control='Master', cardindex=self.card_index).getvolume()[0] alsaaudio.Mixer( control='Master', cardindex=self.card_index).setvolume( recentVolume - 15, 0) time.sleep(1.5) except alsaaudio.ALSAAudioError: selectedButton.SetBackgroundColour('red') selectedButton.SetFocus() self.Update() time.sleep(1.5) elif self.buttons[self.position][0] == 'volume up': try: recentVolume = alsaaudio.Mixer( control='Master', cardindex=self.card_index).getvolume()[0] alsaaudio.Mixer( control='Master', cardindex=self.card_index).setvolume( recentVolume + 15, 0) time.sleep(1.5) except alsaaudio.ALSAAudioError: selectedButton.SetBackgroundColour('red') selectedButton.SetFocus() self.Update() time.sleep(1.5) elif self.buttons[self.position][0] == 'play pause': if self.pressedStopFlag == True: os.system('smplayer -send-action play') self.pressedStopFlag = False else: os.system('smplayer -send-action pause') elif self.buttons[self.position][0] == 'stop': os.system( 'smplayer -send-action stop && smplayer -send-action stop %% smplayer -send-action fullcreen' ) self.pressedStopFlag = True elif self.buttons[self.position][0] == 'fast backward': os.system('smplayer -send-action rewind3') elif self.buttons[self.position][0] == 'fast forward': os.system('smplayer -send-action forward3') elif self.buttons[self.position][0] == 'backward': os.system('smplayer -send-action rewind2') elif self.buttons[self.position][0] == 'forward': os.system('smplayer -send-action forward2') elif self.buttons[self.position][0] == 'slow backward': os.system('smplayer -send-action rewind1') elif self.buttons[self.position][0] == 'slow forward': os.system('smplayer -send-action forward1') selectedButton.SetBackgroundColour( self.backgroundColour ) # depend on abilites comment or not self.flag = 'row' self.rowIteration = 0 self.colIteration = 0 self.countRows = 0 self.countColumns = 0 else: pass # print self.numberOfPresses #------------------------------------------------------------------------- def timerUpdate(self, event): if self.control == 'tracker': self.initCount += 1 self.mousePosition = self.mouseCursor.position() if self.mousePosition[0] < 1120 and self.initCount > 2: self.initCount2 += 1 if self.initCount2 == 2: self.stoper.Stop() suspend.suspend(self, id=2).Show(True) self.initCount = 0 self.initCont2 = 0 self.Hide() else: self.initCount2 = 0 if self.pressFlag == True: if self.button.GetBackgroundColour() == self.backgroundColour: self.button.SetBackgroundColour(self.selectionColour) else: self.button.SetBackgroundColour(self.backgroundColour) self.pressFlag = False else: if self.control != 'tracker': self.mouseCursor.move(*self.mousePosition) self.numberOfPresses = 0 if self.numberOfEmptyIteration < 2 * 0.9999999999: if self.switchSound.lower() == 'on': self.switchingSound.play() if self.flag == 'row': #flag == row ie. switching between rows self.numberOfEmptyIteration += 1. / self.numberOfRows[0] self.rowIteration = self.rowIteration % self.numberOfRows[0] items = self.subSizer.GetChildren() for item in items: b = item.GetWindow() b.SetBackgroundColour(self.backgroundColour) b.SetFocus() if self.rowIteration == 0 or self.rowIteration == 1: scope = range( self.rowIteration * self.numberOfColumns[0], self.rowIteration * self.numberOfColumns[0] + self.numberOfColumns[0]) elif self.rowIteration == 2: scope = self.rowIteration * self.numberOfColumns[0], elif self.rowIteration == 3 or self.rowIteration == 4 or self.rowIteration == 5: scope = range( self.rowIteration * self.numberOfColumns[0] - 1, self.rowIteration * self.numberOfColumns[0] + self.numberOfColumns[0] - 1) elif self.rowIteration == 6: scope = self.rowIteration * self.numberOfColumns[0] - 1, for i in scope: item = self.subSizer.GetItem(i) b = item.GetWindow() b.SetBackgroundColour(self.scanningColour) b.SetFocus() self.rowIteration += 1 elif self.flag == 'columns': #flag = columns ie. switching between cells in the particular row if self.countColumns == self.countMaxColumns: self.flag = 'row' self.rowIteration = 0 self.colIteration = 0 self.countColumns = 0 self.countRows = 0 items = self.subSizer.GetChildren() for item in items: b = item.GetWindow() b.SetBackgroundColour(self.backgroundColour) b.SetFocus() else: self.colIteration = self.colIteration % self.numberOfColumns[ 0] if self.colIteration == self.numberOfColumns[0] - 1: self.countColumns += 1 items = self.subSizer.GetChildren() for item in items: b = item.GetWindow() b.SetBackgroundColour(self.backgroundColour) b.SetFocus() if self.rowIteration == 1 or self.rowIteration == 2: item = self.subSizer.GetItem( (self.rowIteration - 1) * self.numberOfColumns[0] + self.colIteration) elif self.rowIteration == 4 or self.rowIteration == 5 or self.rowIteration == 6: item = self.subSizer.GetItem( (self.rowIteration - 1) * self.numberOfColumns[0] + self.colIteration - 1) b = item.GetWindow() b.SetBackgroundColour(self.scanningColour) b.SetFocus() self.colIteration += 1 else: self.stoper.Stop() self.numberOfEmptyIteration = 0 suspend.suspend(self, id=2).Show(True) self.Hide() items = self.subSizer.GetChildren() for item in items: b = item.GetWindow() b.SetBackgroundColour(self.backgroundColour) b.SetFocus() self.numberOfEmptyIteration = 0 self.countColumns = 0 self.countRows = 0 self.colIteration = 0 self.rowIteration = 0 self.countColumns = 0 self.countRows = 0 self.numberOfPresses = 1
#cv2.imwrite("camEye.jpg",roi_gray_le) """Method calling (finding pupil in given eye image)--->moksh function(;_*_;)""" val_r, px_r, py_r = locate(roi_gray_re) print "px_r ", px_r cv2.circle(roi_gray_re, (px_r, py_r), 3, (255, 0, 0), 3) #print "px_r ",px_r val_l, px_l, py_l = locate(roi_gray_le) cv2.circle(roi_gray_le, (px_l, py_l), 3, (255, 0, 0), 3) cv2.imshow("right eye", roi_gray_re) cv2.imshow("left eye", roi_gray_le) #print "px_l ",px_l """MOUSE MOVEMENT CONTROL""" px, py = mouse.position() if temp_x < 210: mx = 0 elif temp_x > 420: mx = screen_width else: mx = (temp_x - 210) * screen_width / (420 - 210) if temp_y < 200: my = 0 elif temp_y > 280: my = screen_height else: my = (temp_y - 200) * screen_height / (280 - 200) if abs(px_r - listRX[2]) < 3 and abs(px_l - listLX[2]) < 3:
# import pymouse module from pymouse import PyMouse m = PyMouse() import serial import time # initialize and connect to serial port ser = serial.Serial('/dev/ttyACM0', 9600) # set current position of mouse on the screen as reference reference = m.position() reference_x=reference[0] reference_y=reference[1] #initialize values of new_x and new_y new_x=reference[0] new_y=reference[1] # mapping table for HEX codes received from arduino # cursor movement move_right1 = list('A94\r\n') move_right2 = list('A95\r\n') move_left1 = list('B94\r\n') move_left2 = list('B95\r\n') move_up1 = list('C94\r\n') move_up2 = list('C95\r\n') move_down1 = list('D94\r\n') move_down2 = list('D95\r\n') moveUpRight = list('E94\r\n') moveUpLeft = list('E95\r\n') moveDownRight = list('F94\r\n')
#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)
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 myBase(): def __init__(self, hwnd): self.hwnd = hwnd self.get_parent_size() currfiletpath = Path(os.path.abspath(__file__)) self.currtpath = currfiletpath.parent self.mous = PyMouse() self.ditu_size = { "jianye": (287, 142), "donghaiwan": (119, 119), "changancheng": (548, 277), "jiangnanyewai": (159, 119), "aolaiguo": (222, 150), "huaguoshan": (159, 119), "beijuluzhou": (226, 169), "changshoujiaowai": (191, 167) } self.yunbiao_par = ["牛魔王", "观音姐姐", "镇元大仙", "孙婆婆", "地藏王"] self.padocr = PaddleOCR(use_angle_cls=True, lang="ch") # 调用百度的识别文字 APP_ID = '25963522' API_KEY = 'RIOFdoDKGXfhHr2uLYVmUG8w' SECRET_KEY = 'vnWA7s1WfWsrPwEvkgkiVI7MMxNTjFvw' self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY) def get_num_center(self, num1, num2): center_num = abs(num1 - num2) // 2 if num1 > num2: return num2 + center_num else: return num1 + center_num def get_filename(self, ptn): """ 返回一个临时文件 :param ptn: :return: """ times = time.time() timearray = time.localtime(times) times = time.strftime("%Y%m%d%H%M%S", timearray) return os.path.join(self.currtpath, "temp", ptn + times + ".png") def get_snapshot(self): """ 截取当前屏幕快照,返回临时文件名 :param ptn: :return: """ filename = self.get_filename("snapshot") im = ImageGrab.grab() im.save(filename) return filename def get_parent_size(self): print(self.hwnd) win32gui.SetForegroundWindow(self.hwnd) print("zhiqian") time.sleep(2) left, top, right, bottom = win32gui.GetWindowRect(self.hwnd) #left, top, right, bottom = self.get_window_rect() self.windowleft = left self.windowtop = top self.windowright = right self.windowbottom = bottom self.parent_size = (left, top, self.windowright, self.windowbottom) self.dows_center = (self.get_num_center(left, self.windowright), self.get_num_center(top, self.windowbottom)) def clear_scene(self): self.mous.move( random.choice( range(self.dows_center[0] - 10, self.dows_center[0] + 10)), random.choice( range(self.dows_center[1] - 10, self.dows_center[1] + 10))) time.sleep(1) pyautogui.click() time.sleep(1) #self.mous.click(self.dows_center[0],self.dows_center[1],1,1) pyautogui.press("F9") pyautogui.hotkey("alt", "h") time.sleep(1) def get_pic_centerforaytogui(self, picfile, confidence=0.7, is_npc=False): """ 根据基准图片获取操作对象位置 :param picfile: 基准图片 :param canel: 误差值 :return: 对像坐标 """ if is_npc: self.clear_scene() picpath = self.get_pic_fullpath(picfile) grids = pyautogui.locateCenterOnScreen(picpath, confidence=confidence, region=self.parent_size, grayscale=True) return grids def get_pic_bbox(self, picfile, confidence=0.6): """ 根据基准图片获取操作对象位置 :param picfile: 基准图片 :param canel: 误差值 :return: 对像box """ picpath = self.get_pic_fullpath(picfile) print(picfile, picpath) bbox = pyautogui.locateOnScreen(picpath, confidence=confidence, region=self.parent_size, grayscale=True) return bbox def get_allpic(self, picfile, confidence=0.6, index=None): """ 查找所有相似图片,返回bbox """ picpath = self.get_pic_fullpath(picfile) btm = pyautogui.locateAllOnScreen(picpath, confidence=confidence, region=self.parent_size, grayscale=True) if index != None: return list(btm)[index] return list(btm) def get_interval(self, basic, length): pt = length / 3 end = basic + pt * 2 start = basic return range(int(start), int(end)) def dhclick(self, grids, scenal=False): gridx = grids.x gridy = grids.y if scenal: if abs(self.dows_center[0] - gridx) < 100: gridx = gridx + 100 else: gridx = self.get_num_center(gridx, self.dows_center[0]) gridy = self.get_num_center(gridy, self.dows_center[1]) pyautogui.moveTo(gridx - 5, gridy - 5) time.sleep(1) pyautogui.click() time.sleep(1) def getpic_click(self, picfile, checkfile=None, confidence=0.7, is_npc=False, check_tag=True): if checkfile: if check_tag: while not self.get_pic_centerforaytogui(checkfile): grids = self.get_pic_centerforaytogui( picfile, confidence=confidence, is_npc=is_npc) if grids: self.dhclick(grids) if not self.get_pic_centerforaytogui(checkfile): self.dhclick(grids, scenal=True) else: while self.get_pic_centerforaytogui(checkfile): pyautogui.moveTo(self.dows_center) grids = self.get_pic_centerforaytogui( picfile, confidence=confidence, is_npc=is_npc) if grids: self.dhclick(grids) else: grids = self.get_pic_centerforaytogui(picfile, confidence=confidence, is_npc=is_npc) if grids: self.dhclick(grids) def calsimilar(self, imagef1, imagef2): """ 图片相似度对比 :param imagef1: :param imagef2: :return: """ image1 = Image.open(imagef1) image2 = Image.open(imagef2) image1 = image1.convert("L") image2 = image2.resize((image1.width, image1.height), Image.ANTIALIAS).convert("L") gray1 = list(image1.getdata()) gray2 = list(image2.getdata()) if gray1 == gray2: return 100 same = 0 for sm in range(len(gray1)): if gray1[sm] == gray2[sm]: same += 1 return same * 100 / len(gray1) def diff_ratio(self, diff1, diff2): if not isinstance(diff1, str) or not isinstance(diff2, str): print("非字符串数据不能比较相似度") return 0 res = difflib.SequenceMatcher(None, diff1, diff2) return res.quick_ratio() # 百度OCR接口 def get_pic_text(self, picfile): result = self.duqu_yemian(picfile) return result # 获取当前声景 def get_scene(self): tmpfilename = self.get_filename("scene") left, top = self.parent_size[:2] scene_size = (left, top + 80, left + 130, top + 130) img = ImageGrab.grab(scene_size) img.save(tmpfilename) time.sleep(1) result = self.padocr.ocr(tmpfilename, cls=True) print(result) scene_text = [line[-1][0] for line in result] texts = scene_text[-1] scene = "".join(re.findall(r'[\u4e00-\u9fa5]', texts)) zuobiao = re.findall(r"\d+", texts.split(scene)[-1][1:-1]) if len(zuobiao) == 1: zuobiao = [zuobiao[0][:-1], zuobiao[0][-1]] print("当前场景信息:%s %s" % (scene, zuobiao)) return [scene, zuobiao] def xiaozhun_weizhi(self, grid): while True: print("位置校准:%s" % grid) _, zb = self.get_scene() if len(zb) != 2: continue if (int(grid[0]) - int(zb[0])) > 10: px = 100 elif (int(grid[0]) - int(zb[0])) < -10: px = -100 else: px = 0 if (int(grid[1]) - int(zb[1])) > 10: py = -100 elif (int(grid[1]) - int(zb[1])) < -10: py = 100 else: py = 0 if px == py == 0: break print("校准值:%s %s" % (px, py)) self.yidongfx(px, py) # 通过百度接品获取定位 def get_pointpic_bybaidu(self): tmpfilename = self.get_filename("pointpic") px, py = self.mous.position() print(px, py) bbox = (px - 70, py - 80, px + 100, py + 50) im = ImageGrab.grab(bbox) im.save(tmpfilename) msg = self.get_pic_text(tmpfilename) text_list = msg.get("words_result") for text in text_list: texts = text.get("words") point = texts.split(",") if len(point) == 2: break else: point = self.get_pointpic_bybaidu() return point # aircv方式获取指定图片坐标 def get_pic_fullpath(self, pocname): myfile = Path(pocname) if myfile.is_file(): picpath = pocname else: picdir = pocname.strip("0123456789") + "pic" picpath = os.path.join(self.currtpath, "pic", picdir, pocname + ".png") return picpath def get_pic_foraircv(self, picfile, confidence=0.6): picpath = self.get_pic_fullpath(picfile) print(picpath) snapshot_pic = self.get_snapshot() bmp = aircv.imread(snapshot_pic) tim = aircv.imread(picpath) result = aircv.find_template(bmp, tim, threshold=confidence) if result: print(result.get("confidence")) rec = result.get('rectangle') return rec, snapshot_pic #return self.suoxiaosource(rec[0])+self.suoxiaosource(rec[-1],lv=0.015,is_add=False) else: return None # 截取屏幕上类似图片 def shear_pic(self, picfile): rec, snapshot_pic = self.get_pic_foraircv(picfile) bbox = rec[0] + rec[-1] return self.bbox_pic(bbox, snapshot_pic) def bbox_pic(self, bbox, source_image=None): if source_image == None: source_image = self.get_snapshot() #im_box=(bbox.left,bbox.top,bbox.left+bbox.width,bbox.top+bbox.height) im1 = Image.open(source_image) im2 = im1.crop(bbox) temfile = self.get_filename("renwutmp") im2.save(temfile) return temfile # 场景与PC的缩方比例 def get_pixel_rate(self, sec, bbox): max_grid = self.ditu_size.get(sec) x1, y1, x2, y2 = bbox xt = (x2 - x1) / max_grid[0] yt = (y2 - y1) / max_grid[1] return (xt, yt) def get_pic_center(self, picfile, confidence=0.6): rec, _ = self.get_pic_foraircv(picfile, confidence=confidence) bbox = self.suoxiaosource(rec[0]) + self.suoxiaosource( rec[-1], lv=0.015, is_add=False) if bbox: return (self.get_num_center(bbox[0], bbox[2]), self.get_num_center(bbox[1], bbox[3])) # 按比例缩小图片坐标 def suoxiaosource(self, bbox, lv=0.005, is_add=True): if is_add: lv = lv + 1 else: lv = 1 - lv return (bbox[0] * lv, bbox[1] * lv) # 去往场景内某一坐标 def positioning(self, scene_name, x, y): pyautogui.press("tab") time.sleep(2) rec, _ = self.get_pic_foraircv(scene_name) bbox = self.suoxiaosource(rec[0]) + self.suoxiaosource( rec[-1], lv=0.01, is_add=False) xt, yt = self.get_pixel_rate(scene_name, bbox) minix = bbox[0] maxy = bbox[-1] grids = (minix + int(x * xt), maxy - int(y * yt)) pyautogui.moveTo(grids) time.sleep(1) print(pyautogui.position()) pyautogui.click() time.sleep(1) pyautogui.press("tab") def positioningforzb(self, scene_name, x, y): pyautogui.press("tab") time.sleep(2) rec, _ = self.get_pic_foraircv(scene_name) bbox = self.suoxiaosource(rec[0]) + self.suoxiaosource( rec[-1], lv=0.01, is_add=False) xt, yt = self.get_pixel_rate(scene_name, bbox) minix = bbox[0] maxy = bbox[-1] grids = (minix + int(x * xt), maxy - int(y * yt)) pyautogui.moveTo(grids) time.sleep(1) print(pyautogui.position()) pyautogui.click() time.sleep(1) pyautogui.press("tab") # 使用物品 def shiyongwupin(self, wupinpic): while not self.get_pic_centerforaytogui(wupinpic, confidence=0.8): print("没找到") pyautogui.hotkey("alt", "e") grids = self.get_pic_centerforaytogui(wupinpic, confidence=0.8) pyautogui.moveTo(grids) pyautogui.click(button="right") def yidongfx(self, fx=100, fy=100): """ 方向移动 """ dx = self.dows_center[0] dy = self.dows_center[1] print(self.dows_center) px = dx + fx py = dy + fy print(px, py) pyautogui.moveTo(px, py) pyautogui.click() time.sleep(2) def is_zhantouz(self): """判断是否战斗状态""" if self.get_pic_centerforaytogui("zhandou1", confidence=0.8): return True return False def zhandoucz(self): """ 战斗时的操作 """ while self.is_zhantouz(): pyautogui.hotkey("alt", "q") pyautogui.hotkey("alt", "q") pyautogui.hotkey("alt", "a") pyautogui.hotkey("alt", "a") time.sleep(10) def huangdang(self, scene_name): """ 场景内转悠 """ px = random.choice(range(10, 100)) py = random.choice(range(10, 100)) self.positioning(scene_name, px, py) for i in range(5): if self.is_zhantouz(): self.zhandoucz() else: time.sleep(5) def duqu_yemian(self, picfile): print(picfile) temp_file = self.shear_pic(picfile) print(temp_file) # Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换 # 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。 result = self.padocr.ocr(temp_file, cls=True) result = [line[-1][0] for line in result] return result # 打开任务栏 def open_renwulan(self): while not self.get_pic_centerforaytogui( "renwu1") and not self.is_zhantouz(): pyautogui.hotkey("alt", "q")
class KeyboardControl(PyKeyboardEvent): """ This class is used to generate demonstrations from human through keyboard. Some tricks are used to make the keyboard controlling more user friendly. Move the agent around by key "W, A, S, D" and open or close gripper by key "E", and move the robot arm joints (if there is) by mouse and key "R, F". """ def __init__(self): super().__init__(capture=False) self._mouse = PyMouse() x, y = self._mouse.screen_size() self._x_center, self._y_center = x / 2.0, y / 2.0 self._speed = 0 self._turning = 0 self._arm_joints = [0, 0, 0] self._gripper_open = True self._wheel_step = 0.5 self._speed_decay = 0.9 self._turning_decay = 0.6 self.start() def reset(self): self._arm_joints = [0, 0, 0] self._gripper_open = True self._speed = 0 self._turning = 0 def get_agent_actions(self, agent_type): """ Args: agent_type(sting): the agent type Returns: actions generated by the keyboard accroding to agent type """ # decay the speed self._speed *= self._speed_decay self._turning *= self._turning_decay # get gripper pos mouse_x, mouse_y = self._get_mouse_pos() self._arm_joints[0] = mouse_x self._arm_joints[1] = mouse_y return self._convert_to_agent_action(agent_type) def tap(self, keycode, character, press): """ Keyboard event handler. Args: keycode(int): the key code character(string): the key name press(bool): True if the key was pressed and False if the key was released. """ if not press: return if character == "w": self._speed = 0 if self._speed < -0.01 else self._speed + self._wheel_step elif character == "s": self._speed = 0 if self._speed > 0.01 else self._speed - self._wheel_step elif character == "a": self._turning = 0 if self._turning > 0.01 else self._turning - self._wheel_step elif character == "d": self._turning = 0 if self._turning < -0.01 else self._turning + self._wheel_step # arm_joint[2] elif character == "r": self._arm_joints[2] -= 0.1 elif character == "f": self._arm_joints[2] += 0.1 # gripper finger elif character == "e": self._gripper_open = not self._gripper_open # set step size elif character == "+": self._wheel_step *= 1.5 elif character == "-": self._wheel_step *= 0.7 def _get_mouse_pos(self): """ Get the mouse position and normalize to (-1, 1). """ x, y = self._mouse.position() x, y = x / self._x_center - 1.0, y / self._y_center - 1.0 return x, y def _convert_to_agent_action(self, agent_type): if agent_type == 'pioneer2dx_noplugin' or agent_type == 'turtlebot': actions = self._to_diff_drive_action() elif agent_type == 'youbot_noplugin': actions = self._to_youbot_action() elif agent_type == 'pr2_noplugin': actions = self._to_pr2_action() elif agent_type == 'kuka_lwr_4plus': actions = self._to_lwr4_action() else: actions = [] logging.info("agent type not supported yet: " + agent_type) return actions def _to_diff_drive_action(self): left_wheel_joint = self._speed + self._turning right_wheel_joint = self._speed - self._turning actions = [left_wheel_joint, right_wheel_joint] return actions def _to_youbot_action(self): """ Convert to the wrapped youbot actions """ if self._gripper_open: finger_joint = 0.5 else: finger_joint = -0.5 actions = [ self._arm_joints[0], self._arm_joints[1], self._arm_joints[2], 0, finger_joint, self._speed, self._turning ] return actions def _to_lwr4_action(self): actions = [ self._speed, self._turning, self._arm_joints[0], self._arm_joints[1], self._arm_joints[2] ] return actions def _to_pr2_action(self): wheel_joint_bl = self._speed + self._turning wheel_joint_br = self._speed - self._turning wheel_joint_fl = self._speed + self._turning wheel_joint_fr = self._speed - self._turning actions = [ wheel_joint_fl, wheel_joint_fl, wheel_joint_fr, wheel_joint_fr, wheel_joint_bl, wheel_joint_bl, wheel_joint_br, wheel_joint_br ] return actions
45) * TIME_ONCE - 12 # +10秒随机 刷一次副本所需的时间 刚好和新产生的体力数量差不多,这样可以一直不停的刷下去 # TIME_FOREVER = 10 mouse = PyMouse() config_file_name = 'play.txt' config = configparser.ConfigParser() config.read(config_file_name) if not config.has_section('start_again'): print('检测到未配置【再来一次】按钮位置,即将开始进行配置...') time.sleep(2) config.add_section('start_again') print('请将鼠标移至【再来一次】按钮左侧...') for x in range(5, -1, -1): print('\r倒计时{0}'.format(x), end='', flush=True) time.sleep(1) if x == 0: print('\r成功记录鼠标位置...', mouse.position()) config.set('start_again', 'left', str(mouse.position())) print('请将鼠标移至【再来一次】按钮右侧...') for x in range(5, -1, -1): print('\r倒计时{0}'.format(x), end='', flush=True) time.sleep(1) if x == 0: print('\r成功记录鼠标位置...', mouse.position()) config.set('start_again', 'right', str(mouse.position())) if not config.has_section('confirm'): print('检测到未配置【确认】按钮位置,即将开始进行配置...') time.sleep(2) config.add_section('confirm') print('请将鼠标移至【确认】按钮左侧...') for x in range(5, -1, -1): print('\r倒计时{0}'.format(x), end='', flush=True)
# from ctypes import * # import time # # for i in range(1,45000000): # windll.user32.SetCursorPos(900,50); # time.sleep(2) # windll.user32.SetCursorPos(900,300); from pymouse import PyMouse import time import math import pyautogui import random m = PyMouse() a = m.position() #获取当前坐标的位置 print(a) time.sleep(1) for i in range(1,50000): if i%56 != 0: pyautogui.click(random.randint(425,472), random.randint(860,920)) time.sleep(0.07) else : pyautogui.click(173, 524) pyautogui.click(273, 524) pyautogui.click(373, 524) pyautogui.click(173, 650) pyautogui.click(273, 650) pyautogui.click(373, 650) pyautogui.click(106, 483) pyautogui.click(224, 512)
sleeps = raw_input("click interval(s):") while (not sleeps.isdigit()): sleeps = raw_input("click interval(s):") sleeps = int(sleeps) click_num = raw_input("click_num:") while (not click_num.isdigit()): click_num = raw_input("click_num:") click_num = int(click_num) points = [] key = "" while (index < click_num and key != 'q' and key != 'Q'): key = raw_input('press w get click %s pos,or q/Q for quit:' % (index + 1)) while (key == 'w' or key == 'W'): m = PyMouse() x, y = m.position() points.append((x, y)) index += 1 key = "" click_num = index index = 0 while (True): m = PyMouse() x, y = points[index] print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( time.time())), 'click at(', x, ',', y, ')' m.move(x, y) m.click(x, y) index += 1 if (index >= click_num): break
canvas.tostring_rgb()) pil_image = pil_image.resize((img_size, img_size), PIL.Image.ANTIALIAS) pil_image.save(path) #pil_image = None #canvas = None with warnings.catch_warnings(): warnings.simplefilter("ignore") learn = load_learner('ai_models', 'fastai_model01.pkl') sequence_len = 800 n_max_iteration = 10000000 data = np.zeros([sequence_len, 2]) mouse_position_index = 0 mouse = PyMouse() x_old, y_old = mouse.position() for _ in range(n_max_iteration): x, y = mouse.position() delta_x, x_old = x - x_old, x delta_y, y_old = y - y_old, y # Discard too small and too big mouse movements if (np.abs(delta_x) < 0.5 or np.abs(delta_x) > 100): continue if (np.abs(delta_y) < 0.5 or np.abs(delta_y) > 100): continue data[mouse_position_index, 1] = delta_x data[mouse_position_index, 0] = delta_y mouse_position_index = mouse_position_index + 1
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()
def transMousePosition( ): #PyMouse得到的是2维字符串,但是tkinter生成窗体时需要的是类似(100*100+x+y)的字符串,100*100是窗口大小,xy是坐标点。 m = PyMouse() return "100x100+" + str(m.position()[0]) + "+" + str(m.position()[1])
def click(): m = PyMouse() while CLICK_FLAG: x, y = m.position() m.click(x, y, 1) sleep(0.018)
import time import win32clipboard as winclip import win32con import threading global sock global mouse global mouse_pos #mouse_pos=[x,y] global servscr, cliscr, screenboundary global debugout, center global sleeptime global my_address, my_port, my_port_file, my_address_port, my_address_port_file global dest_address, dest_port, dest_port_file, dest_address_port, dest_address_port_file mouse = PyMouse() key = PyKeyboard() mouse_pos = list(mouse.position()) debugout = 0 sleeptime = 0 screenboundary = (1279, 719) center = 1 def init(): global mouse, key, sock global my_address, my_port, my_port_file, my_address_port, my_address_port_file global dest_address, dest_port, dest_port_file, dest_address_port, dest_address_port_file #my_address = '192.168.137.198' my_address = '172.16.7.12' my_port = 8001 my_port_file = 8002 #dest_address = '192.168.137.1'
def continuously_print_mouse_position(): m = PyMouse() while True: print(m.position())
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from pymouse import PyMouse service = service.Service('/home/chris/Documents/chromedriver') service.start() capabilities = {'chrome.binary': '/path/to/custom/chrome'} browser = webdriver.Remote(service.service_url, capabilities) browser.delete_all_cookies() browser.get('https://login.taobao.com') browser.maximize_window() mouse = PyMouse() print mouse.position() # browser.find_element_by_link_text('亲,请登录').click() # # wait = WebDriverWait(browser, 30) # element = wait.until(EC.element_to_be_clickable((By.ID,'J_SubmitStatic'))) ele_input_name = browser.find_element_by_id('TPL_username_1') x = ele_input_name.location['x'] y = ele_input_name.location['y'] mouse.move(x, y) # ActionChains(browser).move_to_element(ele_input_name).perform() # ele_input_name.send_keys('15313349378') # time.sleep(2) #
class MouseControl: def __init__(self,virtual_width=None, virtual_height=None): self.mouse = PyMouse() self.position = None self.max_x, self.max_y = self.mouse.screen_size() self.virtual_width = virtual_width self.virtual_height = virtual_height def mouse_position(self): return self.mouse.position() def virtual_mouse_position(self): pos = self.mouse_position() x_pos = pos[0] / float(self.max_x) * self.virtual_width y_pos = pos[1] / float(self.max_y) * self.virtual_height return (x_pos, y_pos) def target_x(self, value): fraction = value / float(self.virtual_width) value = fraction * self.max_x return value def target_y(self, value): fraction = value / float(self.virtual_height) value = fraction * self.max_y return value def target_position(self, pos): return ( self.target_x(pos[0]), self.target_y(pos[1]) ) def virtual_x(self, value): if (value > self.virtual_width): value = self.virtual_width elif (value < 0): value = 0 return value def virtual_y(self, value): if (value > self.virtual_height): value = self.virtual_height elif (value < 0): value = 0 return value def virtual_position(self, pos): return ( self.virtual_x(pos[0]), self.virtual_y(pos[1]) ) def location(self): if (self.position): location = self.position else: location = self.virtual_mouse_position() return location def to_target(self, target): if target: target = self.virtual_position(target) target = (self.virtual_width - target[0], target[1]) if (self.position != None): x_dist = target[0] - self.position[0] y_dist = target[1] - self.position[1] real_dist = (self.target_x(x_dist), self.target_y(y_dist)) mouse_pos = self.mouse_position() mouse_pos = (mouse_pos[0] + real_dist[0], mouse_pos[1] + real_dist[1]) self.mouse_to(mouse_pos) self.position = target return self def mouse_to(self, pos): self.mouse.move(pos[0], pos[1]) def reset(self): self.position = None
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)
def transMousePosition(): m = PyMouse() return "100x100+"+str(m.position()[0])+"+"+str(m.position()[1])
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)
if message == "Error": error_flag = True socket.send("Record") elif message == "ask": print "ODEBRANO PYTANIE: " + message if error_flag==True: if counter < 50: print "Odsylam" socket.send("record") print "Odeslano" counter +=1 else: error_flag = False counter = 0 socket.send("stop") else: socket.send("ok") elif message == "press": print 'else -- ' + message else: acceptationSound.play( ) socket.send("ok") # m.move(1320, 740) m.click(m.position()[0], m.position()[1], 1) #sending response to client
""" from sensor import Sensor from pymouse import PyMouse from os.path import isfile s = Sensor() m = PyMouse() # init sensor while not s.get_single_sample(): pass m.move(10000, 10000) x, y = m.position() m.move(x / 2, y / 2) X_THRESHOLD = 2. Y_THRESHOLD = 3. def convert(raw_value, threshold): PARAM = 1.5 PARAM2 = 1.5 if threshold < val < 2 * threshold: return val - threshold return val while not isfile('/tmp/stop'):
class Bot(): def __init__(self): self.mouse = PyMouse() self.keyboard = PyKeyboard() def Delay(self, n): time.sleep(n) """ ====== Mouse Macro ====== """ def Left_click(self, x, y, n=1, dl=0): """在屏幕某点左键点击若干次 """ self.Delay(dl) self.mouse.click(x, y, 1, n) def Right_click(self, x, y, n=1, dl=0): """在屏幕某点右键点击若干次 """ self.Delay(dl) self.mouse.click(x, y, 2, n) def Double_click(self, x, y, dl=0): """在屏幕的某点双击 """ self.Delay(dl) self.mouse.click(x, y, 1, n=2) def Scroll_up(self, n, dl=0): """鼠标滚轮向上n次 """ self.Delay(dl) self.mouse.scroll(vertical=n) def Scroll_down(self, n, dl=0): """鼠标滚轮向下n次 """ self.Delay(dl) self.mouse.scroll(vertical=-n) def Move_to(self, x, y, dl=0): """鼠标移动到x, y的坐标处 """ self.Delay(dl) self.mouse.move(x, y) def Drag_and_release(self, start, end, dl=0): """从start的坐标处鼠标左键单击拖曳到end的坐标处 start, end是tuple. 格式是(x, y) """ self.Delay(dl) self.mouse.press(start[0], start[1], 1) self.mouse.drag(end[0], end[1]) self.Delay(0.1) self.mouse.release(end[0], end[1], 1) def Screen_size(self): width, height = self.mouse.screen_size() return width, height def WhereXY(self): x_axis, y_axis = self.mouse.position() return x_axis, y_axis """ ====== Keyboard Macro ====== """ """COMBINATION组合键""" """Ctrl系列""" def Ctrl_c(self, dl=0): """Ctrl + c 复制 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("c") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_x(self, dl=0): """Ctrl + x 剪切 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("x") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_v(self, dl=0): """Ctrl + v 粘贴 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("v") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_z(self, dl=0): """Ctrl + z 撤销上一次操作 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("z") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_y(self, dl=0): """Ctrl + y 重复上一次操作 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("y") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_a(self, dl=0): """Ctrl + a 全选 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("a") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_Fn(self, n, dl=0): """Ctrl + Fn1~12 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key(self.keyboard.function_keys[n]) self.keyboard.release_key(self.keyboard.control_key) """Alt系列""" def Alt_Tab(self, dl=0): """Alt + Tab 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.tap_key(self.keyboard.tab_key) self.keyboard.release_key(self.keyboard.alt_key) def Alt_Fn(self, n, dl=0): """Alt + Fn1~12 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.tap_key(self.keyboard.function_keys[n]) self.keyboard.release_key(self.keyboard.alt_key) """SINGLE KEY单个键盘键""" def Up(self, n=1, dl=0): """上方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.up_key, n) def Down(self, n=1, dl=0): """下方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.down_key, n) def Left(self, n=1, dl=0): """左方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.left_key, n) def Right(self, n=1, dl=0): """右方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.right_key, n) def Enter(self, n=1, dl=0): """回车键/换行键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.enter_key, n) def Delete(self, n=1, dl=0): """删除键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.delete_key, n) def Back(self, n=1, dl=0): """退格键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.backspace_key, n) def Space(self, n=1, dl=0): """空格键n次 """ self.Delay(dl) self.keyboard.tap_key(" ", n) def Fn(self, n, dl=0): """功能键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.function_keys[n]) def Char(self, char, n=1, dl=0): """输入任意单字符n次,只要能在键盘上打出来的字符都可以 """ if len(char) == 1: self.Delay(dl) self.keyboard.tap_key(char) else: raise Exception("""method "Char()" can only take one character.""") def Type_string(self, text, interval=0, dl=0): """键盘输入字符串,interval是字符间输入时间间隔,单位"秒" """ self.Delay(dl) self.keyboard.type_string(text, interval)
DON'T MOVE THE WINDOW OF THE OBR-PROGRAM AFTERWARDS!\n """ # Using raw_input() I make sure that the program will halt here and wait # for ENTER. raw_input('Press ENTER when the mouse is over the <FILE> Button.') print "\n\n<<<<<<<< <FILE> Button position determined >>>>>>>>" # Here the position of the mouse pointer is determined. # .position() returns a tuple that contains the x- and y-coordinates # of the pointer at the time this method is called. file_button_position = mouse.position() # file_button_position[0] / file_button_position[1] retrieves the first / second # entry of the tuple that contains the x- and y-coordinates of the # <FILE> Button load_reference_button_position = (file_button_position[0], (file_button_position[1] + 70)) # Some more information for the user. print """\n ATTENTION: DON'T MOVE THE WINDOW OF THE OBR-PROGRAM After pressing ENTER again the OBR window will become active and the automatic analysis will start. DON'T make this console the active window after pressing ENTER. It will work even if you can't see this console.\n """
def op_mouse(interval, count, outfile, delay, clearflag=False): from pymouse import PyMouse mouse = PyMouse() # remove F_EXIT if os.path.exists(F_EXIT): os.remove(F_EXIT) # # let user drop his anchor # n = NSECS_DROP_ANCHOR print "please move the mouse to drop your anchor in %d secs ..." % n while n > 0: x, y = mouse.position() print "mouse is at point(%d, %d), %d secs left ..." % (x, y, n) time.sleep(1) n -= 1 x, y = mouse.position() print "mouse is locked to point(%d, %d) ..." % (x, y) if len(outfile) > 0: print "save mouse point(%d, %d) to file %s ..." % (x, y, outfile) os.system("echo %d,%d > %s" % (x, y, outfile)) # # delay for a while until guest is ready # if count == 0: return print "delay %d secs until my guest is ready ..." % delay time.sleep(delay) # # now repeat moving the mouse and click # if count == -1: count = 1 << 64 i = 0 while count > 0: # clear the screen so as to catch rqcode png file at ease if clearflag: os.system("clear") i += 1 print "%d: move mouse to point(%d, %d) and click, " % (i, x, y) + \ "touch %s to abort me ..." % F_EXIT x0, y0 = mouse.position() # save the old position mouse.move(x, y) mouse.click(x, y) mouse.move(x0, y0) # move back if os.path.exists(F_EXIT): print "Aha, DADY IS BACK, good bye ..." os.remove(F_EXIT) break if i >= count: print "Well, finished clicking %d times, good bye ..." % count break # abort if the buddy pid is not running pid = BUDDY_PID if pid != -1: if not check_pid(pid): print "I have to go as my buddy %d is gone, good bye ..." % pid break time.sleep(interval)
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 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
def showVideo(self,click_allow_arg): lower_IR = np.array([0,0,50]) # lower bound of CV filter upper_IR = np.array([255,255,255]) # upper bound of CV filter rawCapture = PiRGBArray(self.cam_handle, size=self.cam_res) # capture to array m = PyMouse() # make mouse object prev_maxLoc = (0,0) # init previous maxLoc try: for frame in self.cam_handle.capture_continuous(rawCapture, format = 'bgr', use_video_port=True): image = frame.array # get the array values for that frame mask = cv2.inRange(image, lower_IR, upper_IR) # mask according to lower/upper filter values (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(mask) # find min/max values of image true_pnt = self.get_true_point(maxLoc) print true_pnt #DEBUG INFO PRINTOUT adjusted_maxx = int(((true_pnt[0]) * self.output_res[0])/(self.screen_res[0])) # take the distance from the edge and perform dimensional analysis adjusted_maxy = int(((true_pnt[1]) * self.output_res[1])/(self.screen_res[1])) # take the distance from the edge and perform dimensional analysis if click_allow_arg: # if user wants to have clicks when IR found if maxLoc != (0,0): # if not on the default location m.press(adjusted_maxx,adjusted_maxy, 1) # press the "mouse" button at location found elif prev_maxLoc != (0,0) and maxLoc == (0,0): # if the current value is the default and the last value wasn't the default, release m.release(prev_maxLoc[0],prev_maxLoc[1], 1) # release the "mouse" button else: # only move the mouse, no clicking if maxLoc != (0,0): # if not on the default location m.move(adjusted_maxx,adjusted_maxy) # move the mouse prev_maxLoc = (adjusted_maxx,adjusted_maxy) # set previous to be current max loc cv2.circle(image, maxLoc, 21, (255,0,0), 2) # place circle on brightest spot cv2.imshow("TestWindow", cv2.resize(image,(160,120))) # show the image in a tiny window cv2.waitKey(1) & 0xFF # needed for preview rawCapture.seek(0) # return to the 0th byte rawCapture.truncate() # clear array for next frame print "Mouse at: ", m.position() #DEBUG INFO PRINTOUT except KeyboardInterrupt: # used to quit the function rawCapture.seek(0) # return to the 0th byte rawCapture.truncate() # clear array for next frame