Exemple #1
0
def main(callback=None):
    m = PyMouse()

    def exit_handler():
        for button in range(3):
            m.release(0, 0, button + 1)

    atexit.register(exit_handler)
    width, height = m.screen_size()
    width /= 2
    height /= 2
    old_buttons = 0
    for f in falcon():
        x, y, z = xyz(f.getPosition())
        new_buttons = buttons(f)
        xx = int(width + x * width // 0.03)
        yy = int(height - y * height // 0.03)
        m.move(xx, yy)
        if callback:
            callback(f, xx, yy)
        for button in range(3):
            if new_buttons & 1 << button and not old_buttons & 1 << button:
                m.press(xx, yy, button + 1)
            if not new_buttons & 1 << button and old_buttons & 1 << button:
                m.release(xx, yy, button + 1)
        if new_buttons & 1 << 3:
            print("â–¶ pressed; exiting")
            break
        old_buttons = new_buttons
        time.sleep(1 / 60)
Exemple #2
0
def main():
    mouse = PyMouse()

    # 	wm = cwiid.Wiimote("00:25:A0:CE:3B:6D")
    wm = cwiid.Wiimote()
    wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_IR

    X, x = calibrate(wm)
    trans = getTransMatrix(x, X)

    screen = mouse.screen_size()
    lastTime = time.time()
    o = None

    state = NEUTRAL

    print("battery: %f%%" % (float(wm.state["battery"]) / float(cwiid.BATTERY_MAX) * 100.0))

    window = pygame.display.set_mode((200, 150))

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)

        pos = getWiiPointNonBlock(wm)

        if pos != None:
            ipt = numpy.matrix([[pos.x], [pos.y], [1]])
            optMat = trans.I * ipt
            o = Point(optMat[0] / optMat[2], optMat[1] / optMat[2])

            if o.x < 0:
                o.x = 0
            elif o.x >= screen[0]:
                o.x = screen[0] - 1

            if o.y < 0:
                o.y = 0
            elif o.y >= screen[1]:
                o.y = screen[1] - 1

            if state == NEUTRAL:
                state = FIRST_INPUT
                lastTime = time.time()
            elif state == FIRST_INPUT:
                if time.time() - FIRST_INPUT_TO_PRESSED > lastTime:
                    mouse.press(o.x, o.y)
                    state = PRESSED
            elif state == PRESSED:
                mouse.move(o.x, o.y)

        if state == FIRST_INPUT and pos == None and time.time() - FIRST_INPUT_TO_PRESSED < lastTime:
            mouse.click(o.x, o.y)
            time.sleep(0.2)
            state = NEUTRAL

        if state == PRESSED and pos == None and time.time() - 1 > lastTime:
            mouse.release(o.x, o.y)
            state = NEUTRAL
class WinMouseControl():
    
    mouseObject=''
    def __init__(self):
        self.mouseObject=PyMouse()
        print "mouse init success"
        pass
    
    def __del__(self):
        pass
    
    def mouseMove(self,x,y,button=None):
        self.mouseObject.move(x, y)
        return 0
    
    def mouseClick(self,x,y,button):
        self.mouseObject.click(x, y, button)   #button:1 left, button2: middle button3:right
        return 0
    
    def mousePress(self,x,y,button):
        self.mouseObject.press(x, y, button)   #button:1 left, button2: middle button3:right
        return 0
    
    def mouseRelease(self,x,y,button):
        self.mouseObject.release(x, y, button)   #button:1 left, button2: middle button3:right
        return 0
class Mouse:
    def __init__(self):
        self.m = PyMouse()
        # self.suppress_xlib_output(2)

    def get_mouse_position(self):
        return self.m.position()

    def press(self, x, y, button=1):
        self.m.press(x, y, button)

    def release(self, x, y, button=1):
        self.m.release(x, y, button)

    def click(self, x, y, button=1):
        self.m.click(x, y, button)

    def move_to(self, x, y):
        self.m.move(x, y)

    @staticmethod
    def suppress_xlib_output(num):
        for i in range(num):
            print '\r\033[1A' + 46 * ' ',
        print
Exemple #5
0
def drag(source, dest, speed=1000):
    """
    Simulates a smooth mouse drag

    Args:
        source (int, int) : location (x,y) to start the drag, in screen 
            coordinates
        dest (int, int) : location (x,y) to end the drag, in screen 
            coordinates
        speed (int) : rate at which to execute the drag, in pixels/second
    """
    m = PyMouse()
    m.press(*source)

    time.sleep(0.1)

    # number of intermediate movements to make for our given speed
    npoints = int(sqrt((dest[0]-source[0])**2 + (dest[1]-source[1])**2 ) / (speed/1000))
    for i in range(npoints):
        x = int(source[0] + ((dest[0]-source[0])/npoints)*i)
        y = int(source[1] + ((dest[1]-source[1])/npoints)*i)
        m.move(x,y)
        time.sleep(0.001)

    m.release(*dest)
Exemple #6
0
def slide_down(x, y, h):
    m = PyMouse()
    m.move(x, y)
    m.press(x, y, 1)
    m.move(x, y + h)
    m.release(x, y + h, 1)
    return
class Mouse:

	@property
	def vis(self):
		return self._vis

	def __init__(self,vis):
		self._vis = vis
		self.pm = PM()

	""" Takes a board coordinate (x,y) and uses self._vis to calculate the
	screen coordinates to click there. Then it waits 5.7 ms, and takes a 
	screenshot"""
	def left_click(self,x,y):
		#print "clicking at",x,y
		x0, y0 = self.vis.edge_coords
		x1, y1 = (x0 + 8 + x * 16, y0 + 8 + y * 16)
		self.pm.move(x1,y1)
		self.pm.click(x1,y1,1)
		#time.sleep(0.25)
	
	def right_click(self,x,y):
		x0, y0 = self.vis.edge_coords
		x1, y1 = (x0 + 8 + x * 16, y0 + 8 + y * 16)
		self.pm.click(x1,y1,2)
		#time.sleep(0.05)

	""" Uses self._vis to determine the position of the smiley reset button"""
	def reset_board(self):
		(resx, resy) = self.vis.get_reset()
		self.pm.move(resx,resy)
		self.pm.press(resx,resy,1)
		time.sleep(0.5)
		self.pm.release(resx,resy,1)
Exemple #8
0
class MouseController:
    """
    鼠标控制器
    """
    def __init__(self):
        self.m = PyMouse()

    def position(self):
        print('鼠标当前位置是:%s,%s' % self.m.position())

    def click(self, x, y, button=1, times=1):
        if button == 2:
            self.rclick(x, y)
        else:
            self.lclick(x, y, times)
        self.position()

    def move(self, x, y):
        self.m.move(x, y)
        print('mouse move to (%d,%d)' % (x, y))

    def lclick(self, x, y, times=1):
        self.m.click(x, y, 1, times)
        print('mouse lclick on (%d,%d)' % (x, y))

    def rclick(self, x, y):
        self.m.press(x, y, 2)
        time.sleep(0.1)
        self.m.release(x, y, 2)
        print('mouse rclick on (%d,%d)' % (x, y))
Exemple #9
0
def main():
    #Initialize positions
    L_pos = [(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0)]
    R_pos = [(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0)]

    #initialize controller, mouse, and keyboard
    controller = Leap.Controller()
    m = PyMouse()
    x_dim, y_dim = m.screen_size()
    k = PyKeyboard()
    
    
    
    
    #print positions every half second
    looping = True
    while looping:
        #update hand positions
        R_pos = update(controller,0);
        L_pos = update(controller,1);

        #Track mouse to right index finger
        #press if z is negative
        pressed = False
        mouseX = int(R_pos[1][0] * x_dim / 200.0 + x_dim / 2)
        if mouseX < 0:
            mouseX = 0
        elif mouseX > x_dim:
            mouseX = x_dim - 1

        mouseY = int(y_dim - ((R_pos[1][1] - 200) * y_dim / 100.0))
        if mouseY < 0:
            mouseY = 0
        if mouseY > y_dim - 1:
            mouseY = y_dim - 1

        mouseZ = R_pos[1][2]

        if mouseZ > 10:
            if pressed:
                m.release(mouseX,mouseY,1)
                pressed = False
            else:
                m.move(mouseX,mouseY)
        if mouseZ < -10:
            if not pressed:
                m.press(mouseX,mouseY,1)
                pressed = True
            else:
                #m.move(mouseX,mouseY)
                pass
                
        
        #End if either index finger is above the controller
        for positions in [L_pos, R_pos]:
            if (positions[1][0] < 50 and positions[1][0] > -50) and \
                (positions[1][1] < 50 and positions[1][1] > 0) and \
                (positions[1][2] < 50 and positions[1][2] > -50):
                looping = False
Exemple #10
0
def clickOnCorner():
    m = PyMouse()
    x = 1438
    y = 2558
    m.move(x, y)
    m.click(x, y)  #the third argument "1" represents the mouse button
    m.press(x, y)  #mouse button press
    m.release(x, y)  #mouse button release
Exemple #11
0
 def slide_down(self, x, y, h):
     log.log("move and location x,y '%s, %s' " % (x, y))
     m = PyMouse()
     m.move(x, y)
     m.press(x, y, 1)
     time.sleep(1)
     m.move(x, y + h)
     m.release(x, y + h, 1)
     return
Exemple #12
0
def draw():
    linedraw.draw_contours = draw_contours
    linedraw.draw_hatch = draw_hatch
    linedraw.hatch_size = hatch_size
    linedraw.contour_simplify = contour_simplify

    cache_name = get_cache_name()

    print(cache_name, input_path)

    try:
        file = open(cache_name, 'r')
        fc = file.read()
        lines = json.loads(fc)
        file.close()
    except FileNotFoundError:
        print("Unexpected error:", sys.exc_info()[0])
        file = open(cache_name, 'w')
        lines = linedraw.sketch(input_path)
        file.write(json.dumps(lines))
        file.close()

    mouse = PyMouse()

    mouse_x, mouse_y = mouse.position()

    time.sleep(delay)

    scale = 1

    def get_x(_x):
        return (_x * scale) + mouse_x

    def get_y(_y):
        return (_y * scale) + mouse_y

    def drag(_x, _y):
        # print("drag: {},{}".format(_x, _y))
        mouse.drag(_x, _y)

    for line in lines:

        start_x, start_y = line[0]

        mouse.press(get_x(start_x), get_y(start_y), 1)

        for (x, y) in line:
            time.sleep(speed)
            drag(get_x(x), get_y(y))

        end_x, end_y = line[len(line) - 1]

        mouse.release(get_x(end_x), get_y(end_y), 1)

    mouse.move(mouse_x, mouse_y)
Exemple #13
0
def run_CICFlowMeter():
    m = PyMouse()
    x, y = m.position()
    cmd = ['sudo ./CICFlowMeter']
    #open 3 app instances
    p = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
    #p = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
    #p = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
    #separate the instances on the screen
    time.sleep(5)
    m.press(650,300,1) #move 1
    m.release(60,20)
    time.sleep(0.5)
    m.move(740,300)
    time.sleep(0.5)
    '''m.press(740,300,1) #move 2
    m.release(100,500)
    m.move(750,300)
    time.sleep(0.5)
    m.press(750,300,1) #move 3
    m.release(800,20)
    time.sleep(0.5)'''
    m.click(60,370) #set load1
    time.sleep(2)
    '''m.click(750,370) #set load2
    time.sleep(0.5)
    m.click(60,850) #set load3
    time.sleep(0.5)'''
    m.click(300,490)
    m.click(300,480) #set any
    time.sleep(0.25)
    '''m.click(790,490)
    time.sleep(0.25)
    m.click(790,480) #set any
    time.sleep(0.25)
    m.click(300,870)
    time.sleep(0.25)
    m.click(300,960) #set any
    time.sleep(0.25)'''
    s = time.time()
    m.click(60,410) #start 1
    time.sleep(0.25)
    '''m.click(740,400) #start 2
    time.sleep(0.5)
    m.click(30,878) #start 3'''

    '''inst1 = threading.Thread(target = run1, args=(m,s))
    inst2 = threading.Thread(target = run2, args=(m,s))
    inst3 = threading.Thread(target = run3, args=(m,s))'''

    '''inst1.start()
    inst2.start()
    inst3.start()'''

    p.wait()
Exemple #14
0
def auto_submit_and_shutdown(result_name,
                             submit_time,
                             sleep_time=600,
                             shutdown_flag=False):
    while (True):
        if (submit_time == time.strftime('%H:%M')):
            browser = webdriver.Firefox()
            browser.maximize_window()
            browser.get(
                'https://account.aliyun.com/login/login.htm?oauth_callback=https%3A%2F%2Ftianchi.shuju.aliyun.com%2Fcompetition%2Finformation.htm%3Fspm%3D5176.100069.5678.2.Jypv0M%26raceId%3D231591%26_is_login_redirect%3Dtrue%26_is_login_redirect%3Dtrue'
            )
            browser.switch_to_frame('alibaba-login-box')
            element = browser.find_element_by_id('J_Quick2Static')
            time.sleep(4.3)
            element.click()  #选择账号密码登录

            mouse = PyMouse()
            keyboard = PyKeyboard()

            mouse.click(1200, 410)  #选中账号框格
            keyboard.type_string('hipton')
            time.sleep(5.2)
            keyboard.type_string('ese')
            mouse.click(1200, 480)
            keyboard.type_string('2008723lgy')
            time.sleep(3.2)

            mouse.press(1140, 570)  #拖动滑块
            time.sleep(0.04)
            mouse.move(1200, 560)
            time.sleep(0.03)
            mouse.move(1280, 575)
            time.sleep(0.06)
            mouse.move(1400, 587)
            time.sleep(0.13)
            mouse.release(1400, 590)
            time.sleep(1.3)
            browser.find_element_by_id("login-submit").click()

            time.sleep(sleep_time)  #给手机验证者预留的时间
            mouse.click(400, 630)  #选择提交结果
            time.sleep(2.3)
            mouse.click(800, 490)  #选中提交窗口
            time.sleep(2.5)
            keyboard.type_string('G:\\tc_koubei_newBird\\dyy\\results\\' +
                                 result_name + '.csv')
            time.sleep(1)
            keyboard.press_key('\n')
            time.sleep(1)
            keyboard.release_key('\n')
            time.sleep(2)
            if (shutdown_flag):
                shutdown(1)
            break
Exemple #15
0
def click(point):
    """
    Simulates a mouse click

    Args:
        point (int,int) : location (x,y) of the screen to click
    """
    m = PyMouse()
    m.move(*point)
    m.press(*point)
    m.release(*point)
Exemple #16
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)
Exemple #17
0
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)
Exemple #18
0
class Player:
    def __init__(self):
        self.mouse = PyMouse()

    def click(self, pos):
        self.mouse.click(*pos)

    def shoot(self, target, power, table_offset, cue_ball):
        adj_target = np.add(target, table_offset)
        self.mouse.press(*adj_target)

        adj_cue = np.add(cue_ball.get_pos(), table_offset)
        self.mouse.drag(*adj_cue)
        self.mouse.release(*adj_cue)
Exemple #19
0
    def test_event(self):
        for size in screen_sizes:
            with Display(visible=VISIBLE, size=size):
                time.sleep(1.0)  # TODO: how long should we wait?
                mouse = PyMouse()
                event = Event()
                event.start()
                # check move
                for p in positions:
                    event.reset()
                    mouse.move(*p)
                    time.sleep(0.01)
                    print(('check ', expect_pos(p, size), '=', event.pos))
                    eq_(expect_pos(p, size), event.pos)
                # check buttons
                for btn in buttons:
                    # press
                    event.reset()
                    mouse.press(0, 0, btn)
                    time.sleep(0.01)
                    print(("check button", btn, "pressed"))
                    eq_(btn, event.button)
                    eq_(True, event.press)
                    # release
                    event.reset()
                    mouse.release(0, 0, btn)
                    time.sleep(0.01)
                    print(("check button", btn, "released"))
                    eq_(btn, event.button)
                    eq_(False, event.press)
                # check scroll
                def check_scroll(btn, vertical=None, horizontal=None):
                    event.reset()
                    mouse.press(0, 0, btn)
                    time.sleep(0.01)
                    if vertical:
                        eq_(vertical, event.scroll_vertical)
                    elif horizontal:
                        eq_(horizontal, event.scroll_horizontal)

                print("check scroll up")
                check_scroll(4, 1, 0)
                print("check scroll down")
                check_scroll(5, -1, 0)
                print("check scroll left")
                check_scroll(6, 0, 1)
                print("check scroll right")
                check_scroll(7, 0, -1)
                event.stop()
Exemple #20
0
class HostDeviceClass:
    def __init__(self):
        self.m = PyMouse()
        self.screen = HelperClasses.ScreenClass(self.m.screen_size()[0],
                                                self.m.screen_size()[1])
        self.cursor = HelperClasses.CursorClass(self.screen.width / 2,
                                                self.screen.height / 2)

    def moveCursorToCenter(self):
        self.cursor = HelperClasses.CursorClass(self.screen.width / 2,
                                                self.screen.height / 2)
        print "Updated Cursor Position to center of screen (" + str(
            self.cursor.x) + ", " + str(self.cursor.y) + ")."
        self.moveCursor()

    def moveCursor(self):
        self.m.move(self.cursor.x, self.cursor.y)

    def mousePress(self):
        print "PRESS"
        self.m.press(self.cursor.x, self.cursor.y)

    def mouseRelease(self):
        print "RELEASE"
        self.m.release(self.cursor.x, self.cursor.y)

    def displaceCursor(self, disp):
        # update cursor
        new_x = self.cursor.x + disp.x
        new_y = self.cursor.y + disp.y

        # screen limits
        if new_x > self.screen.width:
            new_x = self.screen.width
        if new_x < 0:
            new_x = 0
        if new_y > self.screen.height:
            new_y = self.screen.height
        if new_y < 0:
            new_y = 0

        actualMovement = HelperClasses.CursorClass(self.cursor.x - new_x,
                                                   self.cursor.y - new_y)

        self.cursor.x = new_x
        self.cursor.y = new_y
        self.moveCursor()

        return actualMovement
Exemple #21
0
def drag(p1, p2, t = 0.05):
    m = PyMouse()
    m.press(p1.x, p1.y, 1)

    x = p1.x
    y = p1.y
    step = 5
    while distance(Point(x, y), p2) >= 1 :
        x += (p2.x - x)/step
        y += (p2.y - y)/step
        step -= 1
        m.drag(x, y)
#        time.sleep(0.01)
    m.release(p2.x, p2.y, 1)
    time.sleep(t)
Exemple #22
0
def fuck_with_arduino(port):
    print("F*****g with the arduino")
    ser = serial.Serial(port=port,
                        baudrate=9600,
                        bytesize=serial.EIGHTBITS,
                        parity=serial.PARITY_NONE,
                        stopbits=serial.STOPBITS_ONE,
                        timeout=1,
                        xonxoff=False,
                        rtscts=False,
                        dsrdtr=False,
                        writeTimeout=2)

    mouse = PyMouse()
    keyboard = PyKeyboard()
    screen_size = mouse.screen_size()
    screen_width = (screen_size[0]) / 244
    screen_height = (screen_size[1]) / 244
    assert (ser.isOpen())
    while True:
        ray = ser.readline()
        if (len(ray) > 2 and ray[0] == 109):
            mouse.move(int((ray[1] - 11) * screen_width),
                       int((ray[2] - 11) * screen_height))
        elif (len(ray) > 1 and ray[0] == ord('C')):
            print("mouse_down: " + str(ray[1]))
            pos = mouse.position()
            mouse.press(pos[0], pos[1], ray[1])
        elif (len(ray) > 1 and ray[0] == ord('U')):
            print("mouse_up: " + str(ray[1]))
            pos = mouse.position()
            mouse.release(pos[0], pos[1], ray[1])
        elif (len(ray) > 0 and ray[0] == ord('p')):
            print("PASTE M**********R")
            ray = ray.decode()
            if (len(ray) > 0):
                ray = ray[1:ray.find("\n")]
                clipboard_copy(ray)
                keyboard.type_string(ray)
        elif (len(ray) > 0 and ray[0] == ord('c')):
            keyboard.press_keys([
                "Command" if platform == "darwin" else keyboard.control_key,
                "c"
            ])
            sleep(1)
            replyCopy(ser)
        elif (len(ray) > 1 and ray[0] == ord('s')):
            print("Status: <" + str(ray[1]))
Exemple #23
0
 def test_event(self):
     for size in screen_sizes:
         with Display(visible=VISIBLE, size=size):
             time.sleep(1.0)  # TODO: how long should we wait?
             mouse = PyMouse()
             event = Event()
             event.start()
             # check move
             for p in positions:
                 event.reset()
                 mouse.move(*p)
                 time.sleep(0.01)
                 print('check ', expect_pos(p, size), '=', event.pos)
                 eq_(expect_pos(p, size), event.pos)
             # check buttons
             for btn in buttons:
                 # press
                 event.reset()
                 mouse.press(0, 0, btn)
                 time.sleep(0.01)
                 print("check button", btn, "pressed")
                 eq_(btn, event.button)
                 eq_(True, event.press)
                 # release
                 event.reset()
                 mouse.release(0, 0, btn)
                 time.sleep(0.01)
                 print("check button", btn, "released")
                 eq_(btn, event.button)
                 eq_(False, event.press)
             # check scroll
             def check_scroll(btn, vertical=None, horizontal=None):
                 event.reset()
                 mouse.press(0, 0, btn)
                 time.sleep(0.01)
                 if vertical:
                     eq_(vertical, event.scroll_vertical)
                 elif horizontal:
                     eq_(horizontal, event.scroll_horizontal)
             print("check scroll up")
             check_scroll(4, 1, 0)
             print("check scroll down")
             check_scroll(5, -1, 0)
             print("check scroll left")
             check_scroll(6, 0, 1)
             print("check scroll right")
             check_scroll(7, 0, -1)
             event.stop()
Exemple #24
0
class Mouse:
    mouse = None

    def __init__(self):
        self.mouse = PyMouse()

    def click(self, x, y, time=0.15):
        self.mouse.press(x, y)
        sleep(time)
        self.mouse.release(x, y)
        sleep(time)

    def click_and_back(self, x, y):
        coord_x, coord_y = self.mouse.position()
        self.click(x, y)
        self.mouse.move(coord_x, coord_y)
Exemple #25
0
    class MouseWand(Wand):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.left_color = "#2185d0"
            self.right_color = "#f2711c"
            self.left = False
            self.pressed_left = False

        def post_connect(self):
            print("Move the wand to move the mouse")
            print("Tilt the want to the left (blue light) to left click")
            print("Tilt the want to the right (orange light) to right click")
            print(
                "Tilt the wand left, hold the button, tilt the wand to the right, and release the button to disconnect"
            )
            # Create a mouse and get the screen dimensions
            self._m = PyMouse()
            self.x_dim, self.y_dim = self._m.screen_size()
            self.set_led(self.left_color)

            self.subscribe_button()
            self.subscribe_position()

        def on_position(self, x, y, pitch, roll):
            # Do some magic to get an adjusted x and y position
            x_pos = self.x_dim * ((x * 4 + 1000) / 2000)
            y_pos = self.y_dim * (1.0 - (y * 4 + 1000) / 2000)
            # Move the mouse
            self._m.move(int(round(x_pos)), int(round(y_pos)))

            # Change left mouse button status and set LED when necessary
            if roll > 0 and self.left:
                self.left = False
                self.set_led(self.right_color)
            elif roll < 0 and not self.left:
                self.left = True
                self.set_led(self.left_color)

        def on_button(self, pressed):
            x_pos, y_pos = self._m.position()
            if pressed:
                self._m.press(x_pos, y_pos, 1 if self.left else 2)
                self.pressed_left = self.left
            else:
                self._m.release(x_pos, y_pos, 1 if self.left else 2)
                if self.pressed_left and not self.left:
                    self.disconnect()
Exemple #26
0
def shoot(shift, mouse_start, mouse_end):
    # TODO: docs
    # TODO: this is just ugly => refactor
    m = PyMouse()

    m_x, m_y = mouse_start + shift
    m_end_x, m_end_y = mouse_start + shot_vector(mouse_start,
                                                 mouse_end) + shift

    # focus browser
    m.click(m_x, m_y)

    # do the dragging movement
    m.press(m_x, m_y)
    # sleeping required for the game to register the movement
    sleep(0.2)
    m.release(m_end_x, m_end_y)
class HostDeviceClass:

  def __init__(self):
    self.m = PyMouse()
    self.screen = HelperClasses.ScreenClass(self.m.screen_size()[0], self.m.screen_size()[1])
    self.cursor = HelperClasses.CursorClass(self.screen.width/2, self.screen.height/2)

  def moveCursorToCenter(self):
    self.cursor = HelperClasses.CursorClass(self.screen.width/2, self.screen.height/2)
    print "Updated Cursor Position to center of screen (" + str(self.cursor.x) + ", " + str(self.cursor.y) + ")."
    self.moveCursor()

  def moveCursor(self):
    self.m.move(self.cursor.x, self.cursor.y)

  def mousePress(self):
    print "PRESS"
    self.m.press(self.cursor.x, self.cursor.y)

  def mouseRelease(self):
    print "RELEASE"
    self.m.release(self.cursor.x, self.cursor.y)

  def displaceCursor(self, disp):
    # update cursor
    new_x = self.cursor.x + disp.x
    new_y = self.cursor.y + disp.y

    # screen limits
    if new_x > self.screen.width:
      new_x = self.screen.width
    if new_x < 0:
      new_x = 0
    if new_y > self.screen.height:
      new_y = self.screen.height
    if new_y < 0:
      new_y = 0

    actualMovement = HelperClasses.CursorClass(self.cursor.x - new_x, self.cursor.y - new_y)

    self.cursor.x = new_x
    self.cursor.y = new_y
    self.moveCursor()

    return actualMovement
Exemple #28
0
class Mouse:
    m = None

    def __init__(self, metrics_x=1920, metrics_y=1080):
        self.m = PyMouse()
        self.metrics_x = metrics_x
        self.metrics_y = metrics_y

    def mouse_to(self, x, y):
        self.m.move(x, y)

    def click(self, x, y):
        self.m.click(x, y, 1, 1)

    def double_click(self, x, y):
        self.m.click(x, y, 1, 2)

    # 发送esc退出事件r
    def send_esc(self, hwnd):
        win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_ESCAPE, 0)
        win32api.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_ESCAPE, 0)

        #win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_ESCAPE, 0)
        #win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_ESCAPE, 0)

    def gun_lun(self, n, m):
        self.m.scroll(n, m)

    def vertical_tuo(self, x, y, n):
        self.m.press(x, y, 1)
        self.m.move(x, y - n)
        self.m.release(x, y, 1)

    def left_down(self):
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  # 左键按下

    def left_up(self):
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

    def absolute(self, x, y, dx, dy):
        SW = self.metrics_x
        SH = self.metrics_y
        self.mouse_to(x, y)  # 鼠标移动到
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  # 左键按下
        i = 1
        ddx = int(dx / 10)
        ddy = int(dy / 10)

        while i < 10:
            time.sleep(0.02)
            self.mouse_to(x + ddx * i, y + ddy * i)  # 鼠标移动到
            i += 1
        self.mouse_to(x + dx, y + dy)  # 鼠标移动到
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        return
        time.sleep(0.5)
        self.mouse_to(x + 10, y + 10)  # 鼠标移动到
        time.sleep(0.5)
        mw = int((dx + x) * 65535 / SW)
        mh = int((dy + y) * 65535 / SH)
        win32api.mouse_event(
            win32con.MOUSEEVENTF_ABSOLUTE + win32con.MOUSEEVENTF_MOVE, mw, mh,
            0, 0)
        time.sleep(0.2)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
            for x in range(15):
                reference_y += 1
                reference_x += 1
                m.move(reference_x,reference_y)
                time.sleep(0.005)

    elif (input_hex == moveDownLeft):
        print 'down-left'

        if(reference_y<yUpper and reference_x>xLower):
            
            for x in range(15):
                reference_y += 1
                reference_x -= 1
                m.move(reference_x,reference_y)
                time.sleep(0.005)
                
                
    # mouse click events
    elif ( input_hex == leftClick):
        m.press(reference_x,reference_y,1)

    elif ( input_hex == leftRelease):
        m.release(reference_x,reference_y,1)

    elif ( input_hex == select2):
        m.click(reference_x,reference_y,1)
        m.click(reference_x,reference_y,1)
  
    
Exemple #30
0
from pymouse import PyMouse
import time

m = PyMouse()
i = 0
while i < 603:
    m.click(30, 300, 1)
    time.sleep(1)
    m.click(980, 515, 1)
    time.sleep(1)
    m.click(1125, 715, 1)
    time.sleep(1)
    m.press(638, 94, 1)
    time.sleep(1)
    m.release(1395, 1055, 1)
    time.sleep(1)
    m.click(1135, 675)
    time.sleep(1)
    m.click(1825, 60)
    time.sleep(1)
    i = i + 1
class ControlMainClass():
    def __init__(self):
        self.osName = platform.system()
        self.pcName = os.environ['computername']
        self.screenSize = pyautogui.size()
        print "Operation system is: ", self.osName
        print "PC name is: ", self.pcName

        self.mouseMap = ['left', 'middle', 'right']

        self.controller = pyautogui
        self.pyMouse = PyMouse()
        self.pyKeyboard = PyKeyboard()
        self.keyboardMap = {}
        self.initKeyboardMap()

    def __del__(self):
        pass

    def initKeyboardMap(self):
        self.keyboardMap[3] = ' '
        self.keyboardMap[10] = '\''
        self.keyboardMap[15] = ','
        self.keyboardMap[16] = '-'
        self.keyboardMap[17] = '.'
        self.keyboardMap[18] = '/'
        self.keyboardMap[19] = '0'
        self.keyboardMap[20] = '1'
        self.keyboardMap[21] = '2'
        self.keyboardMap[22] = '3'
        self.keyboardMap[23] = '4'
        self.keyboardMap[24] = '5'
        self.keyboardMap[25] = '6'
        self.keyboardMap[26] = '7'
        self.keyboardMap[27] = '8'
        self.keyboardMap[28] = '9'
        self.keyboardMap[30] = ';'
        self.keyboardMap[32] = '='
        self.keyboardMap[36] = '['
        self.keyboardMap[37] = '\\'
        self.keyboardMap[38] = ']'
        self.keyboardMap[41] = '`'
        self.keyboardMap[42] = 'a'
        self.keyboardMap[43] = 'b'
        self.keyboardMap[44] = 'c'
        self.keyboardMap[45] = 'd'
        self.keyboardMap[46] = 'e'
        self.keyboardMap[47] = 'f'
        self.keyboardMap[48] = 'g'
        self.keyboardMap[49] = 'h'
        self.keyboardMap[50] = 'i'
        self.keyboardMap[51] = 'j'
        self.keyboardMap[52] = 'k'
        self.keyboardMap[53] = 'l'
        self.keyboardMap[54] = 'm'
        self.keyboardMap[55] = 'n'
        self.keyboardMap[56] = 'o'
        self.keyboardMap[57] = 'p'
        self.keyboardMap[58] = 'q'
        self.keyboardMap[59] = 'r'
        self.keyboardMap[60] = 's'
        self.keyboardMap[61] = 't'
        self.keyboardMap[62] = 'u'
        self.keyboardMap[63] = 'v'
        self.keyboardMap[64] = 'w'
        self.keyboardMap[65] = 'x'
        self.keyboardMap[66] = 'y'
        self.keyboardMap[67] = 'z'
        self.keyboardMap[75] = self.pyKeyboard.alt_l_key
        self.keyboardMap[76] = self.pyKeyboard.alt_r_key
        self.keyboardMap[78] = self.pyKeyboard.backspace_key
        self.keyboardMap[90] = self.pyKeyboard.control_l_key
        self.keyboardMap[91] = self.pyKeyboard.control_r_key
        self.keyboardMap[93] = self.pyKeyboard.delete_key
        self.keyboardMap[94] = self.pyKeyboard.delete_key
        self.keyboardMap[96] = self.pyKeyboard.down_key
        self.keyboardMap[97] = self.pyKeyboard.end_key
        self.keyboardMap[98] = self.pyKeyboard.enter_key
        self.keyboardMap[99] = self.pyKeyboard.escape_key
        self.keyboardMap[102] = self.pyKeyboard.function_keys[1]
        self.keyboardMap[103] = self.pyKeyboard.function_keys[10]
        self.keyboardMap[104] = self.pyKeyboard.function_keys[11]
        self.keyboardMap[105] = self.pyKeyboard.function_keys[12]
        self.keyboardMap[113] = self.pyKeyboard.function_keys[2]
        self.keyboardMap[119] = self.pyKeyboard.function_keys[3]
        self.keyboardMap[120] = self.pyKeyboard.function_keys[4]
        self.keyboardMap[121] = self.pyKeyboard.function_keys[5]
        self.keyboardMap[122] = self.pyKeyboard.function_keys[6]
        self.keyboardMap[123] = self.pyKeyboard.function_keys[7]
        self.keyboardMap[124] = self.pyKeyboard.function_keys[8]
        self.keyboardMap[125] = self.pyKeyboard.function_keys[9]
	if self.osName=="Windows":
	    self.keyboardMap[129] = self.pyKeyboard.hangul_key
	elif self.osName=="Linux":
	    self.keyboardMap[129] = -1
	    pass
        self.keyboardMap[132] = self.pyKeyboard.home_key
        self.keyboardMap[141] = self.pyKeyboard.left_key
        self.keyboardMap[146] = '0'
        self.keyboardMap[147] = '1'
        self.keyboardMap[148] = '2'
        self.keyboardMap[149] = '3'
        self.keyboardMap[150] = '4'
        self.keyboardMap[151] = '5'
        self.keyboardMap[152] = '6'
        self.keyboardMap[153] = '7'
        self.keyboardMap[154] = '8'
        self.keyboardMap[155] = '9'
        self.keyboardMap[156] = self.pyKeyboard.num_lock_key

        self.keyboardMap[157] = self.pyKeyboard.page_down_key
        self.keyboardMap[158] = self.pyKeyboard.page_up_key
        self.keyboardMap[160] = self.pyKeyboard.page_down_key
        self.keyboardMap[161] = self.pyKeyboard.page_up_key

        self.keyboardMap[170] = self.pyKeyboard.right_key
        self.keyboardMap[171] = self.pyKeyboard.scroll_lock_key

        self.keyboardMap[175] = self.pyKeyboard.shift_l_key
        self.keyboardMap[176] = self.pyKeyboard.shift_r_key

        self.keyboardMap[180] = self.pyKeyboard.tab_key
        self.keyboardMap[181] = self.pyKeyboard.up_key

        pass

    def command(self,data):
        if data[0] == 'm':
            x = (ord(data[3])*100)+ord(data[4])
            y = (ord(data[5])*100)+ord(data[6])
            #print "x:",x," y:",y
            #self.controller.moveTo( x, y) # x,y
            self.pyMouse.move(x, y)
            if data[1] == 'p':
                #self.controller.mouseDown(x,y,self.mouseMap[ord(data[2])]) # x,y,b
                #print "press"
                self.pyMouse.press(x, y,ord(data[2]))
                pass
            elif data[1] == 'r' and ord(data[2]) != 0:
                #self.controller.mouseUp(x,y,self.mouseMap[ord(data[2])]) # x,y,b
                #print "release"
                self.pyMouse.release(x, y, ord(data[2]))
                pass
            if data[7] == 's':
                if data[8] == 'u':
                    #self.controller.scroll(10)
                    self.pyMouse.scroll(vertical=10)
                    pass
                else:
                    #self.controller.scroll(-10)
                    self.pyMouse.scroll(vertical=-10)
                    pass
            pass
        else:
	    print 'data: ',data[0],' '+data[1],' ',ord(data[2])
	    keyCode=ord(data[2])
            if data[1]=='p' and keyCode!=0:
                #self.controller.keyDown(self.controller.KEYBOARD_KEYS[ord(data[2])])
                print 'press '+str(ord(data[2]))
		if self.keyboardMap[ord(data[2])]!=-1:
	            self.pyKeyboard.press_key(self.keyboardMap[ord(data[2])])
                pass
            elif data[1]=='r' and keyCode!=0:
                #self.controller.keyUp(self.controller.KEYBOARD_KEYS[ord(data[2])])
	    	print 'release '+str(ord(data[2]))
		if self.keyboardMap[ord(data[2])]!=-1:
                    self.pyKeyboard.release_key(self.keyboardMap[ord(data[2])])
                pass
        pass
Exemple #32
0
                else:
                    keyboard_object.release_key(
                        map_special_key(event['key'], keyboard_object))
                print print_out
            elif event['type'] == 'mousemotion':
                mouse_object.move(event['pos'][0] * screen_x,
                                  event['pos'][1] * screen_y)
                print '[>] Moving mouse cursor to ' + str(
                    event['pos'][0] * screen_x) + ', ' + str(
                        event['pos'][1] * screen_y)
            elif event['type'] == 'mousebuttondown':
                button = 1 if event['button'] == 1 else 2
                mouse_object.press(event['pos'][0] * screen_x,
                                   event['pos'][1] * screen_y, button)
                print '[>] Mouse pressing at ' + str(
                    event['pos'][0] * screen_x) + ', ' + str(
                        event['pos'][1] * screen_y)
            elif event['type'] == 'mousebuttonup':
                button = 1 if event['button'] == 1 else 2
                mouse_object.release(event['pos'][0] * screen_x,
                                     event['pos'][1] * screen_y, button)
                print '[>] Mouse releasing at ' + str(
                    event['pos'][0] * screen_x) + ', ' + str(
                        event['pos'][1] * screen_y)
        connection.close()
        print '[>] Connection closed'
        break
socket_handle.close()
print '[>] Socket closed'
print '[>] Exit program'
def get_results(args, H):
    tf.reset_default_graph()
    x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3])
    if H['use_rezoom']:
        pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None)
        grid_area = H['grid_height'] * H['grid_width']
        pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2])
        if H['reregress']:
            pred_boxes = pred_boxes + pred_boxes_deltas
    else:
        pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        saver.restore(sess, args.weights)

        pred_annolist = al.AnnoList()

        data_dir = os.path.dirname(args.test_boxes)
        image_dir = get_image_dir(args)
        subprocess.call('mkdir -p %s' % image_dir, shell=True)
	
	memory = sysv_ipc.SharedMemory(123463)
	memory2 = sysv_ipc.SharedMemory(123464)
	size = 768, 1024, 3

	pedal = PyMouse()
	pedal.press(1)
	road_center = 320
	while True:
	    cv2.waitKey(1)
	    frameCount = bytearray(memory.read())
	    curve = bytearray(memory2.read())
	    curve = str(struct.unpack('i',curve)[0])
	    m = np.array(frameCount, dtype=np.uint8)
	    orig_img = m.reshape(size)
	   
	    img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic')
	    feed = {x_in: img}
	    (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed)
	    pred_anno = al.Annotation()
	    
	    new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes,
					    use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed)
	    flag = 0
	    road_center = 320 + int(curve)
	    print(road_center)
	    for rect in rects:
		print(rect.x1, rect.x2, rect.y2)
		if (rect.x1 < road_center and rect.x2 > road_center and rect.y2 > 200) and (rect.x2 - rect.x1 > 30):
			flag = 1

	    if flag is 1:
		pedal.press(2)
		print("break!")
	    else:
		pedal.release(2)
		pedal.press(1)
		print("acceleration!")
		
	    pred_anno.rects = rects
	    pred_anno.imagePath = os.path.abspath(data_dir)
	    pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1])
	    pred_annolist.append(pred_anno)
	    
	    cv2.imshow('.jpg', new_img)
	    
    return none;
Exemple #34
0
class MouseController:
    def __init__(self):
        self.m = PyMouse()
        self.mouseHook = TranslateInjectedMouse()
        self.mouseHook.start()
        self.mouseHookEvent = user32.mouse_event
        self.WINSIZE = self.m.screen_size()
        self.x = long(self.WINSIZE[0] / 2)
        self.y = long(self.WINSIZE[1] / 2)

    def moveAndClick(self, dx=0, dy=0, lmr=1):
        assert (dx != None and dy != None)
        self.x = self.x + long(dx) \
            if (self.x + long(dx)) >= 0 and (self.x + long(dx)) <= self.WINSIZE[0] \
            else (long(self.WINSIZE[0]) if (self.x + long(dx)) > self.WINSIZE[0]
                  else 0L)

        self.y = self.y + long(dy) \
            if (self.y + long(dy)) >= 0 and (self.y + long(dy)) <= self.WINSIZE[1] \
            else (long(self.WINSIZE[1]) if (self.y + long(dy)) > self.WINSIZE[1]
                  else 0L)
        self.m.release(self.x, self.y, lmr)
        self.m.click(self.x, self.y, lmr)
        #print(self.x, self.y)
        return

    def move(self, dx, dy):
        '''
        assert dx!=None and dy!=None
        self.x = self.x + long(dx) \
            if (self.x + long(dx)) >= 0 and (self.x + long(dx)) <= self.WINSIZE[0] \
            else (long(self.WINSIZE[0]) if (self.x + long(dx)) > self.WINSIZE[0]
                  else 0L)

        self.y = self.y + long(dy) \
            if (self.y + long(dy)) >= 0 and (self.y + long(dy)) <= self.WINSIZE[1] \
            else (long(self.WINSIZE[1]) if (self.y + long(dy)) > self.WINSIZE[1]
                  else 0L)
        '''
        self.mouseHookEvent(MOUSEEVENTF_MOVE, dx, dy, 0, 0)
        return

    def press(self, dx=0, dy=0, lmr=1):
        assert (dx != None and dy != None)
        self.x = self.x + long(dx) \
            if (self.x + long(dx)) >= 0 and (self.x + long(dx)) <= self.WINSIZE[0] \
            else (long(self.WINSIZE[0]) if (self.x + long(dx)) > self.WINSIZE[0]
                  else 0L)

        self.y = self.y + long(dy) \
            if (self.y + long(dy)) >= 0 and (self.y + long(dy)) <= self.WINSIZE[1] \
            else (long(self.WINSIZE[1]) if (self.y + long(dy)) > self.WINSIZE[1]
                  else 0L)
        self.m.press(self.x, self.y, lmr)
        return

    def release(self, dx=0, dy=0, lmr=1):
        assert dx != None and dy != None
        self.x = self.x + long(dx) \
            if (self.x + long(dx)) >= 0 and (self.x + long(dx)) <= self.WINSIZE[0] \
            else (long(self.WINSIZE[0]) if (self.x + long(dx)) > self.WINSIZE[0]
                  else 0L)

        self.y = self.y + long(dy) \
            if (self.y + long(dy)) >= 0 and (self.y + long(dy)) <= self.WINSIZE[1] \
            else (long(self.WINSIZE[1]) if (self.y + long(dy)) > self.WINSIZE[1]
                  else 0L)
        self.m.release(self.x, self.y, lmr)
        return

    def stop(self):
        if self.mouseHook.is_alive():
            self.mouseHook.stop()
        else:
            pass
Exemple #35
0
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)
	def run(self):
		global running
		last_time = datetime.datetime.now()
		m=PyMouse()
		measure = str(datetime.datetime.now() - last_time).split(":", 1)[1].split(":", 1)[1]
		data = None
		X,Y,CX,CY,LX,LY,dirX,dirY=0,0,0,0,0,0,0,0
		CXX,CYY,LXX,LYY=0,0,0,0
		max_steps=120
		click_lock=0
		accelX=0
		accelY=0
		
		XX = range(0)
		YY = range(0)
			
		screen_dim = m.screen_size()
		print "Screen Xdim = " + str(screen_dim[0])
		print "Screen Ydim = " + str(screen_dim[1])
		
		while running:
			if not reciever.empty():
				data=reciever.get_nowait()
				print "data = " + str(data)
				reciever.task_done()
				current_pos = m.position()
				last_time = datetime.datetime.now()
				
			if (len(XX)>2 or len(YY)>2) and float(measure)>0.2:
				print "timout..."
				print "measure = " + str(measure)
				print "len XX = " + str(len(XX))
				if len(XX)>0:XX.pop(0)
				if len(YY)>0:YY.pop(0)
				
			if re.match("ROW", str(data)) is not None:
				CY = int(data.split("COL",1)[0].split("ROW",1)[1])
				CX = int(data.split("ROW",1)[1].split("COL",1)[1])
				data=None
				
				if CY is not None and CX is not None and float(measure)<0.125:
					print "\n"
					
					if (CX-LX)>0 and CX!=LX:XX.append(1)
					elif (CX-LX)<0 and CX!=LX:XX.append(-1)
					elif CX!=LX:XX.append(0)

					if (CY-LY)>0 and CY!=LY:YY.append(1)
					elif (CY-LY)<0 and CY!=LY:YY.append(-1)
					elif CY!=LY:YY.append(0)

					dirX=sum(XX)+(self.divider(1,LX))
					dirY=sum(YY)+(self.divider(1,LY))
					
					if float(measure)<0.025:accelX,accelY=4,3
					elif float(measure)>0.025 and float(measure)<0.05:accelX,accelY=3,2
					elif float(measure)>0.05 and float(measure)<0.075:accelX,accelY=2,1.25
					elif float(measure)>0.075 and float(measure)<0.1:accelX,accelX=1,0.75
					else:accelX,accelY=0.75,0.5
						
					targetX=current_pos[0]+(dirX*accelX)
					targetY=current_pos[1]+(dirY*accelY)
					
					if (current_pos[0]>0 and current_pos[0]<screen_dim[0]) and (current_pos[1]>0 and current_pos[1]<screen_dim[1]):
						
						if (targetX<screen_dim[0]) and (targetX>0) and (targetY<screen_dim[1]) and (targetY>0):
							if click_lock==1:m.drag((targetX),targetY)
							else:m.move((targetX),targetY)
							
					elif (current_pos[0]<=0) or (current_pos[1]<=0):
						if dirX>0:
							m.move((1+dirX),current_pos[1])
						elif dirY>0:
							m.move(current_pos[0],(dirY+1))
							
					elif current_pos[0]>=screen_dim[0]:
						if dirX<0:
							m.move(((screen_dim[0]-2)+dirX),(current_pos[1]-2))
					
					if len(XX)>=5 or len(YY)>=5:
						if len(XX)>0:XX.pop(0)
						if len(YY)>0:YY.pop(0)
						
					last_time = datetime.datetime.now()
					LX,LY=CX,CY
					CX,CY=None,None
			
			if re.match("LCLK", str(data)) is not None:
				data=None
				click_lock=0
				print "LEFT CLICK" + str(current_pos)
				m.click(current_pos[0],current_pos[1],1)
				#m.release(current_pos[0],current_pos[0])
				#m.release(current_pos[0],current_pos[0])
				#m.move(current_pos[0],current_pos[1])
				print "LEFT CLICK" + str(current_pos)
				
			if re.match("LPRS", str(data)) is not None:
				data=None
				click_lock=1
				print "LEFT PRESS" + str(current_pos)
				m.press(current_pos[0],current_pos[1])
				#m.move(current_pos[0],current_pos[1])
				print "LEFT PRESS" + str(current_pos)
				
			if re.match("LREL", str(data)) is not None:
				data=None
				click_lock=0
				print "LEFT RELEASE" + str(current_pos)
				#m.release(current_pos[0],current_pos[0])
				m.release(current_pos[0],current_pos[0])
				#m.move(current_pos[0],current_pos[1])
				print "LEFT RELEASE" + str(current_pos)
			
			if re.match("RCLK", str(data)) is not None:
				data=None
				click_lock=0
				print "RIGHT CLICK" + str(current_pos)
				m.click(current_pos[0],current_pos[1],2)
				#m.release(current_pos[0],current_pos[1])
				#m.release(current_pos[0],current_pos[1])
				#m.move(current_pos[0],current_pos[1])
				print "RIGHT CLICK" + str(current_pos)
				
			if re.match("RPRS", str(data)) is not None:
				data=None
				click_lock=1
				print "RIGHT PRESS" + str(current_pos)
				m.press(current_pos[0],current_pos[1])
				#m.move(current_pos[0],current_pos[1])
				print "RIGHT PRESS" + str(current_pos)
				
			if re.match("RREL", str(data)) is not None:
				data=None
				click_lock=0
				print "RIGHT RELEASE" + str(current_pos)
				m.release(current_pos[0],current_pos[1])
				#m.release(current_pos[0],current_pos[1])
				#m.move(current_pos[0],current_pos[1])
				print "RIGHT RELEASE" + str(current_pos)
				
			time.sleep(0.0001)
			measure = str(datetime.datetime.now() - last_time).split(":", 1)[1].split(":", 1)[1]
Exemple #37
0
class BaseCursorListener(Listener):
	"""
	Defines generic callback functions in order to convert frame events into mouse actions.
	"""
	# After this amount of time, a click event is generared (ms)
	click_timeout = 600
	# After this amount of time, a press event is generared (ms)
	press_timeout = 600
	# Number of frames to average
	numframes = 8

	def __init__(self, *args, **kwargs):
		super(BaseCursorListener, self).__init__(*args, **kwargs)

		# The following sets are maintained to determine the actions to be performed from
		# the average values fetched from the last 10 frames
		self.frames_set = list()
		self.previousframes_set = list()

		# Fetch a given mouse object or instanciate it if necessary
		if 'mouse' in kwargs and isinstance(kwargs['mouse'], PyMouse):
			self.mouse = kwargs['mouse']
		else:
			self.mouse = PyMouse()

		# Fetch a given keyboard object or instanciate it if necessary
		if 'keyboard' in kwargs and isinstance(kwargs['keyboard'], PyKeyboard):
			self.keyboard = kwargs['keyboard']
		else:
			self.keyboard = PyKeyboard()

		if 'click' in kwargs:
			print("test")
			self.click_timeout = kwargs['click']
		if 'press' in kwargs:
			self.press_timeout = kwargs['press']

		# Init mouse position tracking
		self.previous_pos = self.mouse.position()

		# Init timers
		self.click_dtstart = 0
		self.press_dtstart = 0

		# Init flags
		self.active_fist = False #Indicates if a clenched fist is considered
		self.press_requested = False # Indicates if a mouse press event is requested

	def is_clicking(self, data):
		"""
		Determines whether the mouse is clicking or not.
		The default behavior is to cause a click when the mouse position remains the same
		during a fixed amount of time. This value is given by the click_timeout attribute.
		"""
		# Get the required data
		hand_state = data['leap_state']['current']

		if self.previous_pos == data['pos']:
			current_time = time.time()
			elapsed_time = current_time - self.click_dtstart
			if (elapsed_time * 1000) >= self.click_timeout and hand_state.av_fingers >=4:
				self.click_dtstart = time.time()
				return True
		else:
			self.click_dtstart = time.time()
		return False

	def is_pressing(self, data):
		"""
		Determines whether the mouse is pressing or not.
		The default behavior is to cause a press action when no fingers are available (the hand
		is closed).
		"""
		# Get the required data
		hand_state = data['leap_state']['current']
		hand_state_prev = data['leap_state']['prev']

		current_time = time.time()
		elapsed_time = current_time - self.press_dtstart

		if hand_state.av_fingers <= 1.2:
			if hand_state.av_numhands == 1 and hand_state.is_horizontal and ((hand_state_prev.av_fingers >= 3 and hand_state.av_fingers <= 1.2)
					or (self.active_fist and hand_state.av_fingers < 1)):
				self.press_requested = True
			elif hand_state.av_numhands == 1 and hand_state.av_fingers <= 1.2 and self.press_requested == True and (elapsed_time * 1000) >= self.press_timeout:
				self.active_fist = True
				return True
		else:
			self.press_dtstart = time.time()
			self.press_requested = False
		return False

	def is_releasing(self, data):
		"""
		Determines whether the mouse is releasing or not.
		The default behabior is to cause a release action when the hand is not closed.
		"""
		# Get the required data
		hand_state = data['leap_state']['current']
		hand_state_prev = data['leap_state']['prev']

		if hand_state.av_fingers >= 2.5 and hand_state.av_palm_pos[2] < 0 and self.active_fist:
			self.active_fist = False
			self.want_press = False
			return True
		return False

	def is_scrolling_up(self, data):
		"""
		Determines whether the mouse is scrolling up or not.
		"""
		# Get the required data
		hand_state = data['leap_state']['current']

		if hand_state.av_fingers >= 4 and not self.active_fist and not self.press_requested:
			if hand_state.av_fingers_speed - hand_state.av_palm_vel < -150:
				repeats = abs(int(hand_state.av_fingers_speed / 50.))
				repeats = max(repeats, 0)
				repeats = min(repeats, 5)
				return repeats
		return False

	def is_scrolling_down(self, data):
		"""
		Determines whether the mouse is scrolling down or not.
		"""
		# Get the required data
		hand_state = data['leap_state']['current']

		if hand_state.av_fingers >= 4 and not self.active_fist and not self.press_requested:
			if hand_state.av_fingers_speed -hand_state.av_palm_vel > 150:
				repeats = abs(int(hand_state.av_fingers_speed / 50.))
				repeats = max(repeats, 0)
				repeats = min(repeats, 5)
				return repeats
		return False

	def is_switching_desktop(self, latest_frame):
		"""
		Determines whether a desktop switch must be induced or not.
		"""
		for gesture in latest_frame.gestures():
			if gesture.type == Gesture.TYPE_SWIPE:
				swipe = SwipeGesture(gesture)
				if swipe.state >= 3:
					if swipe.direction[0] >= 0.8:
						return 'right'
					elif swipe.direction[0] <= -0.8:
						return 'left'
		return False

	def on_init(self, controller):
		# Force the listener to stop if therse is no controller and no leapd daemon launched
		showmessage("Initializing listener", Status.RUNNING, Colors.BLUE)
		i_try = 0
		while not controller.frame(2*self.numframes).is_valid:
			i_try += 1
			if i_try >= 1e6:
				showmessage("Initializing listener", Status.ERROR, Colors.RED, update=True)
				sys.exit(1)
		showmessage("Initializing listener", Status.SUCCESS, Colors.GREEN, update=True)

		# Fill the frames and previousframes lists with the first available frames
		for i_frame in range(self.numframes):
			self.frames_set.append(controller.frame(i_frame))
			self.previousframes_set.append(controller.frame(self.numframes + i_frame))

	def on_connect(self, controller):
		# Enable gestures
		controller.enable_gesture(Gesture.TYPE_SWIPE);
		showmessage("Initializing listener", Status.SUCCESS, Colors.GREEN, update=True, newline=True)

	def on_disconnect(self, controller):
		print("Disconnected")

	def on_exit(self, controller):
		print("Exit")

	def on_frame(self, controller):
		# Get the most recent frame
		latest_frame = controller.frame()

		# Update the frames sets
		self.previousframes_set.pop(0)
		self.previousframes_set.append(self.frames_set[0])
		self.frames_set.pop(0)
		self.frames_set.append(latest_frame)

		data = dict()

		# Fetch some usefull information
		pos = self.mouse.position()
		current_state = CursorState(self.frames_set, self.numframes)
		previous_state = CursorState(self.previousframes_set, self.numframes)

		data.update({
			'pos': pos,
			'leap_state': {
				'current': current_state,
				'prev': previous_state,
			}
		})

		# Determine what is going on above the Leap
		click = self.is_clicking(data)
		press = self.is_pressing(data)
		release = self.is_releasing(data)
		scroll_up = self.is_scrolling_up(data)
		scroll_down = self.is_scrolling_down(data)
		switch_desk = self.is_switching_desktop(latest_frame)

		data.update({
			'actions': {
				'click': click,
				'press': press,
				'release': release,
				'scroll_up': scroll_up,
				'scroll_down': scroll_down,
				'switch_desk': switch_desk,
			}
		})

		# Update the previous mouse position
		self.previous_pos = pos

		self.update(latest_frame, data)

	def update(self, frame, data):
		"""
		Translates frames dispatched by the Leap Motion to mouse events.
		"""
		raise NotImplementedError

	def move(self, x, y):
		"""
		Moves the mouse by using x and y pixels offsets.
		"""
		# Get the mouse position
		current_x, current_y = self.mouse.position()
		# Move!
		self.mouse.move(current_x + round(x), current_y + round(y))

	def click(self):
		"""
		Do a click at the current mouse position.
		"""
		# Get the mouse position
		current_pos = self.mouse.position()
		# Click!
		self.mouse.click(*current_pos)

	def press(self):
		"""
		Do a press at the current mouse position.
		"""
		# Get the mouse position
		current_pos = self.mouse.position()
		# Click!
		self.mouse.press(*current_pos)

	def release(self):
		"""
		Do a release at the current mouse position.
		"""
		# Get the mouse position
		current_pos = self.mouse.position()
		# Click!
		self.mouse.release(*current_pos)

	def scroll_up(self, repeats=0):
		"""
		Do a scroll up action.
		"""
		# Scroll!
		for irep in range(repeats):
			self.mouse.scroll(vertical=4)
			time.sleep(.1)

	def scroll_down(self, repeats=0):
		"""
		Do a scroll down action.
		"""
		# Scroll!
		for irep in range(repeats):
			self.mouse.scroll(vertical=-4)
			time.sleep(.1)

	def switch_desktop(self, direction):
		"""
		Switch to the desktop to the left, right, top or bottom.
		"""
		if direction == 'right':
			self.keyboard.press_key(self.keyboard.alt_key)
			self.keyboard.press_key(self.keyboard.control_l_key)
			self.keyboard.tap_key(self.keyboard.right_key)
			self.keyboard.release_key(self.keyboard.control_l_key)
			self.keyboard.release_key(self.keyboard.alt_key)
		elif direction == 'left':
			self.keyboard.press_key(self.keyboard.alt_key)
			self.keyboard.press_key(self.keyboard.control_l_key)
			self.keyboard.tap_key(self.keyboard.left_key)
			self.keyboard.release_key(self.keyboard.control_l_key)
			self.keyboard.release_key(self.keyboard.alt_key)
Exemple #38
0
class Action(object):
    def __init__(self, file):
        self.file = file
        self.m = PyMouse()
        self.k = PyKeyboard()

    def action_read(self):
        # 从filename中读取操作序列
        action_list = []
        with open(self.file) as f:
            f_csv = csv.reader(f)
            for row in f_csv:
                action_list.append(row)
        return action_list

    def key_translate(self, key_data):
        # 识别序列中的键
        if len(key_data) == 1:
            return key_data
        elif len(key_data) == 2 and key_data.startswith('F'):
            return self.k.function_keys[int(key_data[1])]
        elif key_data == 'enter':
            return self.k.enter_key
        elif key_data == 'space':
            return self.k.space_key
        elif key_data == 'control':
            return self.k.control_key
        elif key_data == 'alt':
            return self.k.alt_key
        else:
            raise Exception("未定义此操作!")

    def action_translte(self, action_unit):
        # 将序列语言翻译成指令
        if action_unit[0] == '0':
            try:
                xy = eval(action_unit[1])
            except Exception as e:
                return False
            x, y = xy[0], xy[1]
            self.m.click(x, y, 1)
            return True
        elif action_unit[0] == '1':
            try:
                keyboard_action_list = action_unit[1].split('_')
                if keyboard_action_list[0] == 'tap':  # 敲击某个键
                    self.k.tap_key(key_data)
                elif keyboard_action_list[0] == 'press':  # 按住某个键
                    key_data = self.key_translate(keyboard_action_list[1])
                    self.k.press_key(key_data)
                elif keyboard_action_list[0] == 'presses':  # 按住某两个键
                    key_data1 = self.key_translate(keyboard_action_list[1])
                    key_data2 = self.key_translate(keyboard_action_list[2])
                    self.k.press_keys([key_data1, key_data2])
                elif keyboard_action_list[0] == 'release':  # 松开某个键
                    key_data = self.key_translate(keyboard_action_list[1])
                    self.k.release_key(key_data)
                elif keyboard_action_list[0] == 'type':  # 输入
                    self.k.type_string(keyboard_action_list[1])
                elif keyboard_action_list[0] == 'callcpp':

                    x = int(keyboard_action_list[1])
                    y = int(keyboard_action_list[2])

                    #i_path = keyboard_action_list[3]
                    #t_path = keyboard_action_list[4]

                    result, x_t, y_t = call_c(x, y)
                    #x_tt = x_t.value
                    #y_tt = y_t.value
                    print(x_t, y_t)
                    x_tt = x_t.value
                    y_tt = y_t.value
                    self.k.type_string(str(x_tt) + '_' + str(y_tt))
                elif keyboard_action_list[0] == 'drawc':

                    self.draw_circle()

            except Exception:
                traceback.print_exc()
                return False
        else:
            print("对象输入错误")
            return False

    def run(self):
        action_list = self.action_read()
        action_lens = len(action_list)
        for i in range(1, action_lens):
            self.action_translte(action_list[i])
            delay = eval(
                action_list[i][2]
            )  ######################################################???????????????????????????????
            time.sleep(delay)

    def draw_circle(self):

        self.m.click(421, 62, 1)
        self.m.click(422, 61, 1)
        self.m.click(419, 59, 1)
        time.sleep(0.5)
        self.m.press(421, 500)
        time.sleep(0.5)
        self.m.move(600, 500)
        time.sleep(0.5)
        self.m.move(600, 670)
        time.sleep(0.5)
        self.m.release(600, 600)

        # type some words
        self.m.click(293, 68)
        self.m.click(478, 549)
        self.k.type_string("Hello World!")
Exemple #39
0
            print('Click')
            print distClick[0]
        if (click_flg and (time.time()-stime)*1000>=lagValue and not drag_flg): #so its been 1/2 second, 
            if (distClick[0]>=newClickValue): #if finger is up, then delete flag. Else 
                click_flg=0
                drag_flg=0
                print("reset")
                print distClick[0]
            elif ((dragX-buff[0].mean()>5) or (dragY-buff[1].mean()>5)): #Drag situation
                m.press(dragX,dragY)
                drag_flg=1
                print ("dragging")
                print distClick[0]
        if drag_flg and distClick[0]>=int(1.2*newClickValue): #released the drag
            drag_flg=0
            m.release(buff[0].mean(),buff[1].mean())
            dragX,dragY=0,0
            print("release drag")
            print distClick[0]

##################implement click with finger movement
        if inrange and mouse_flg and not click_flg and not yeah_flg: #raise flg and store current values
            ASDFTTD=depthBuff[0].mean()
            ASDFTKD=depthBuff[1].mean()
            ASDFITD=depthBuff[2].mean()
            ASDFIKD=depthBuff[3].mean()
            yeah_flg=1
            print "yeah"

#         if mouse_flg:        
#             print depthBuff[0].mean(), ASDFTTD
     m = PyMouse()
     while True:
         data = client_sock.recv(6)
         if len(data) == 0: break
         if data[0] == START_BYTE:
             position = m.position()
             movement = unpack('hh', data[1:5])
             left = ord(data[5]) & 2 != 0
             right = ord(data[5]) & 1 != 0
             scroll = ord(data[5]) & 4 != 0
             if leftPressed != left:
                 leftPressed = left
                 if left:
                     m.press(position[0], position[1], 1)
                 else:
                     m.release(position[0], position[1], 1)
             if rightPressed != right:
                 rightPressed = right
                 if right:
                     m.press(position[0], position[1], 2)
                 else:
                     m.release(position[0], position[1], 2)
             if not scroll and (movement[0] != 0 or movement[1] != 0):
                 m.move(position[0] + movement[0], position[1] + movement[1])
             elif scroll:
                 if movement[1] >= 0:
                     m.scroll(position[0], position[1], False, movement[1])
                 else:
                     m.scroll(position[0], position[1], True, -movement[1])
 except IOError:
     pass
Exemple #41
0
        if (distClick[0]>=newClickValue): #if finger is up, then delete flag. Else 
            vals.click_flg=0
            vals.drag_flg=0
            print("reset")
            print >>mouseF, 'reset'
            print distClick[0]
        elif ((vals.dragX-vals.buff[0].mean()>5) or (vals.dragY-vals.buff[1].mean()>5)): #Drag situation
            m.press(vals.dragX,vals.dragY)
            #time.sleep(0.3)
            vals.drag_flg=1
            print ("dragging")
            print >>mouseF, 'dragging'
            print distClick[0]
    if vals.drag_flg and distClick[0]>=int(1.2*newClickValue): #released the drag
        vals.drag_flg=0
        m.release(vals.buff[0].mean(),vals.buff[1].mean())
        vals.dragX,vals.dragY=0,0
        print("release drag")
        print >>mouseF, 'release drag'
        print distClick[0]
    mouseF.close()
##################implement click with finger movement Still testing#################################
    if vals.inrange and vals.mouse_flg and not vals.click_flg and not vals.yeah_flg: #raise flg and store current values
        vals.ASDFTTD=vals.depthBuff[0].mean()
        vals.ASDFTKD=vals.depthBuff[1].mean()
        vals.ASDFITD=vals.depthBuff[2].mean()
        vals.ASDFIKD=vals.depthBuff[3].mean()
        vals.yeah_flg=1
        print "yeah"

#         if vals.mouse_flg:        
Exemple #42
0
        nux = int(x[0]+joystick.get_axis(0)*350)
        nuy = int(x[1]+joystick.get_axis(1)*350)
    if joystick.get_button(3):
        x_dim, y_dim = m.screen_size()
        x_dim = x_dim/2
        y_dim = y_dim/2
        #m.move(x_dim, y_dim)
        x = m.position()
        nux = int(x_dim+joystick.get_axis(0)*100+xoffset)
        nuy = int(y_dim+joystick.get_axis(1)*100+yoffset)
        m.move(nux, nuy)
    if joystick.get_button(5):
        if not r1new:
            m.press(nux, nuy, 1)
            r1new = True
    else:
        if r1new:
            m.release(nux, nuy, 1)
            r1new = False
    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
    
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # Limit to 20 frames per second
    clock.tick(20)
    
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()
Exemple #43
0
     m = PyMouse()
     while True:
         data = client_sock.recv(6)
         if len(data) == 0: break
         if data[0] == START_BYTE:
             position = m.position()
             movement = unpack('hh', data[1:5])
             left = ord(data[5]) & 2 != 0
             right = ord(data[5]) & 1 != 0
             scroll = ord(data[5]) & 4 != 0
             if leftPressed != left:
                 leftPressed = left
                 if left:
                     m.press(position[0], position[1], 1)
                 else:
                     m.release(position[0], position[1], 1)
             if rightPressed != right:
                 rightPressed = right
                 if right:
                     m.press(position[0], position[1], 2)
                 else:
                     m.release(position[0], position[1], 2)
             if not scroll and (movement[0] != 0 or movement[1] != 0):
                 m.move(position[0] + movement[0],
                        position[1] + movement[1])
             elif scroll:
                 if movement[1] >= 0:
                     m.scroll(position[0], position[1], False, movement[1])
                 else:
                     m.scroll(position[0], position[1], True, -movement[1])
 except IOError:
a_i = [0,0,0,0,0,0,0]
a_m = [0,0,0,0,0,0,0]
position = [0,0,0,0]
add_move = [0,0,0,0]
while(1):
  line = ser.readline()
  a = str(line).split(",")
  if len(a) == 9:
      convert(a_m, a)
      x, y = m.position()
      if (a_m[0]-a_i[0] > 5 or a_m[0] > 180):
          x += 5
          #RotateView(1,0,0, 5)
      if (a_i[0]-a_m[0] > 5 or a_m[0] < 80):
          x -= 5
      if (a_m[1]-a_i[1] > 5 or  a_m[1] > 175):
          y -= 5
      elif(a_i[1]-a_m[1] > 5 or a_m[1] < 67):
          y += 5
      m.move(x,y)
      
      if(a_m[2]):
          m.press(x,y,3)
      elif(a_m[3]):
          m.press(x,y,1)
      else :
          m.release(x,y,1)
          m.release(x,y,3)
      a_i = a_m

Exemple #45
0
class TabPad(QWidget):
    def __init__(self):
        super(TabPad, self).__init__()
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint
                            | QtCore.Qt.FramelessWindowHint
                            | QtCore.Qt.X11BypassWindowManagerHint
                            | QtCore.Qt.WA_ShowWithoutActivating
                            | QtCore.Qt.WindowDoesNotAcceptFocus)
        if transparent_background:
            self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.setFocusPolicy(QtCore.Qt.NoFocus)
        self.setAttribute(QtCore.Qt.WA_AcceptTouchEvents)
        QApplication.setQuitOnLastWindowClosed(False)
        # print (QStyleFactory.keys())
        QApplication.setStyle(QStyleFactory.create("Fusion"))
        QtCore.QCoreApplication.setAttribute(
            QtCore.Qt.AA_SynthesizeTouchForUnhandledMouseEvents)
        QtCore.QCoreApplication.setAttribute(
            QtCore.Qt.AA_SynthesizeMouseForUnhandledTouchEvents)
        self.appicon = QIcon.fromTheme("input-gaming")
        self.screen_resolution = QApplication.desktop().screenGeometry()
        self.screen_width, self.screen_height = self.screen_resolution.width(
        ), self.screen_resolution.height()
        self.set_overlay(overlay_x_position, overlay_y_position, overlay_width,
                         overlay_height)
        self.dpad_coords = []
        self.quadrant_list = []
        self.leftstick_deadzone_coords = []
        self.rightstick_deadzone_coords = []
        self.keydown_list = []
        self.autorepeat_keylist = []
        self.sticky_keylist = []
        self.multitouch_points = []
        self.dpad_keys = [
            self.useable_keys('U'),
            self.useable_keys('D'),
            self.useable_keys('L'),
            self.useable_keys('R')
        ]
        self.leftstick_keys = [
            self.useable_keys('leftstick_U'),
            self.useable_keys('leftstick_D'),
            self.useable_keys('leftstick_L'),
            self.useable_keys('leftstick_R')
        ]
        self.rightstick_keys = [
            self.useable_keys('rightstick_U'),
            self.useable_keys('rightstick_D'),
            self.useable_keys('rightstick_L'),
            self.useable_keys('rightstick_R')
        ]
        self.xdotool = "xdotool"
        self.pyuserinput_process = None
        self.py_mouse = PyMouse()
        self.py_keyboard = PyKeyboard()
        signal.signal(signal.SIGINT, self.signal_handler)
        self.installEventFilter(self)
        self.set_input_type()
        self.initUI()

    def initUI(self):
        if hide_on_close:
            button_layout["Hide"] = button_layout.pop("Close")
        else:
            button_layout["Close"] = button_layout.pop("Close")
        for k, v in button_layout.items():
            if v[0] >= 0 or v[1] >= 0:
                if k == "dpad":
                    self.create_dpad(k, v[0], v[1], (v[2], v[3]), v[4],
                                     [v[6], v[7]])
                elif k == "leftstick":
                    self.create_sticks(k, v[0], v[1], (v[2], v[3]), v[4],
                                       [v[6], v[7]])
                elif k == "rightstick":
                    self.create_sticks(k, v[0], v[1], (v[2], v[3]), v[4],
                                       [v[6], v[7]])
                else:
                    self.createandmove(k, v[0], v[1], (v[2], v[3]), v[4],
                                       [v[6], v[7]])
        self.systraysetup()
        self.setGeometry(self.overlay_x_position, self.overlay_y_position,
                         self.overlay_width, self.overlay_height)
        self.setWindowTitle('TabPad')
        self.show()
        if start_minimized:
            self.hidepad()
        # self.activateWindow()

    def createandmove(self, label, xper, yper, btnsize, color, command):
        qbtn = QPushButton(label, self)
        stl = self.get_style(button_border_size, button_border_radius, \
         button_border_color, color, button_opacity)
        qbtn.setStyleSheet(stl)
        if override_button_size:
            qbtn.resize(*btnsize)
        else:
            qbtn.resize(button_width, button_height)
        xpos = self.percentconvertor(xper, self.overlay_width)
        ypos = self.percentconvertor(yper, self.overlay_height)
        qbtn.move(xpos, ypos)
        qbtn.setFocusPolicy(QtCore.Qt.NoFocus)

    def percentconvertor(self, value, dimension):
        value = self.roundify((value * dimension) / 100)
        return value

    def hextorgb(self, hexcolor):
        h = hexcolor.strip('#')
        h = (tuple(int(h[i:i + 2], 16) for i in (0, 2, 4)))
        return (str(h[0]) + "," + str(h[1]) + "," + str(h[2]))

    def systraysetup(self):
        self.tray_icon = QSystemTrayIcon(self)
        self.tray_icon.setIcon(self.appicon)

        self.show_action = QAction("Show", self)
        self.quit_action = QAction("Quit", self)
        self.hide_action = QAction("Hide", self)
        self.settings_action = QAction("Settings", self)
        self.layout_action = QAction("Edit Current Layout", self)
        self.restart_action = QAction("Restart", self)
        self.autorepeat_action = QAction("Stop All Inputs", self)
        self.about_action = QAction("About TabPad", self)

        self.show_action.setIcon(QIcon.fromTheme("go-home"))
        self.hide_action.setIcon(QIcon.fromTheme("go-down"))
        self.settings_action.setIcon(QIcon.fromTheme("preferences-other"))
        self.layout_action.setIcon(QIcon.fromTheme("edit-find-replace"))
        self.quit_action.setIcon(QIcon.fromTheme("application-exit"))
        self.autorepeat_action.setIcon(QIcon.fromTheme("process-stop"))
        self.restart_action.setIcon(QIcon.fromTheme("view-refresh"))
        self.about_action.setIcon(QIcon.fromTheme("help-about"))

        self.show_action.triggered.connect(self.showpad)
        self.hide_action.triggered.connect(self.hidepad)
        self.quit_action.triggered.connect(self.quithandler)
        self.settings_action.triggered.connect(self.show_settings_window)
        self.layout_action.triggered.connect(self.show_layout_window)
        self.restart_action.triggered.connect(self.restart_program)
        self.autorepeat_action.triggered.connect(self.finish_all_inputs)
        self.about_action.triggered.connect(self.show_about_dialog)

        self.tray_menu = QMenu()
        self.tray_menu.addAction(self.show_action)
        self.tray_menu.addAction(self.hide_action)
        self.tray_menu.addAction(self.autorepeat_action)
        self.tray_menu.addAction(self.layout_action)
        self.tray_menu.addAction(self.settings_action)
        self.tray_menu.addAction(self.restart_action)
        self.tray_menu.addAction(self.about_action)
        self.tray_menu.addAction(self.quit_action)
        self.tray_icon.setContextMenu(self.tray_menu)
        self.tray_icon.show()
        self.tray_icon.activated.connect(self.catchclick)

    def catchclick(self, value):
        if value == self.tray_icon.Trigger:  #left click!
            self.tray_menu.exec_(QCursor.pos())

    def quithandler(self):
        self.cleanup_before_exit()
        QtCore.QCoreApplication.instance().quit()
        sys.exit(0)

    def hidepad(self):
        self.cleanup_before_exit()
        self.hide()

    def showpad(self):
        for widget in QApplication.allWidgets():
            if type(widget).__name__ == 'MainSettings' \
             or type(widget).__name__ == 'LayoutSettings':
                widget.close()
        self.show()
        # self.activateWindow()

    def create_dpad(self, label, xper, yper, btnsize, color, command):
        dpad_frame = QWidget(self)
        dpad_frame.setObjectName("dpad_frame")
        stl = self.get_style(dpad_background_border_size, dpad_background_border_radius, \
         dpad_background_border_color, dpad_background_color, dpad_background_opacity)
        dpad_frame.setStyleSheet(stl)

        if override_button_size:
            dpad_frame.resize(*btnsize)
        else:
            dpad_frame.resize(button_width, button_height)

        xpos = self.percentconvertor(xper, self.overlay_width)
        ypos = self.percentconvertor(yper, self.overlay_height)
        dpad_frame.move(xpos, ypos)
        dpad_frame.setFocusPolicy(QtCore.Qt.NoFocus)

        upbutton = QPushButton("U", dpad_frame)
        upbutton.resize(self.roundify(btnsize[0] * .25),
                        self.roundify(btnsize[1] * .4))
        upbutton.move(self.roundify(btnsize[0] * .5 - btnsize[0] * .125), 0)
        self.dpad_coords.append(self.dpad_geometry(dpad_frame, upbutton))

        downbutton = QPushButton("D", dpad_frame)
        downbutton.resize(self.roundify(btnsize[0] * .25),
                          self.roundify(btnsize[1] * .4))
        downbutton.move(self.roundify(btnsize[0] * .5 - btnsize[0] * .125),
                        self.roundify(btnsize[1] * .6))
        self.dpad_coords.append(self.dpad_geometry(dpad_frame, downbutton))

        leftbutton = QPushButton("L", dpad_frame)
        leftbutton.resize(self.roundify(btnsize[0] * .4),
                          self.roundify(btnsize[1] * .25))
        leftbutton.move(0, self.roundify(btnsize[1] * .5 - btnsize[1] * .125))
        self.dpad_coords.append(self.dpad_geometry(dpad_frame, leftbutton))

        rightbutton = QPushButton("R", dpad_frame)
        rightbutton.resize(self.roundify(btnsize[0] * .4),
                           self.roundify(btnsize[1] * .25))
        rightbutton.move(self.roundify(btnsize[0] * .6),
                         self.roundify(btnsize[1] * .5 - btnsize[1] * .125))
        self.dpad_coords.append(self.dpad_geometry(dpad_frame, rightbutton))

        stl = self.get_style(dpad_border_size, dpad_border_radius, \
         dpad_border_color, dpad_color, button_opacity)

        upbutton.setStyleSheet(stl)
        downbutton.setStyleSheet(stl)
        leftbutton.setStyleSheet(stl)
        rightbutton.setStyleSheet(stl)
        upbutton.setFocusPolicy(QtCore.Qt.NoFocus)
        downbutton.setFocusPolicy(QtCore.Qt.NoFocus)
        leftbutton.setFocusPolicy(QtCore.Qt.NoFocus)
        rightbutton.setFocusPolicy(QtCore.Qt.NoFocus)

        self.set_dpad_quadrants()

    def roundify(self, value):
        return int(round(value))

    def dpad_geometry(self, frame, btn):
        startx = frame.x() + btn.x()
        endx = startx + btn.width()
        starty = frame.y() + btn.y()
        endy = starty + btn.height()
        return (btn.text(), startx, endx, starty, endy)

    def set_dpad_quadrants(self):
        l = self.dpad_coords
        ur_quadrant = (l[0][0] + l[3][0], l[3][1], l[3][2], l[0][3], l[0][4])
        dr_quadrant = (l[1][0] + l[3][0], l[3][1], l[3][2], l[1][3], l[1][4])
        dl_quadrant = (l[1][0] + l[2][0], l[2][1], l[2][2], l[1][3], l[1][4])
        ul_quadrant = (l[0][0] + l[2][0], l[2][1], l[2][2], l[0][3], l[0][4])
        self.quadrant_list.append(ur_quadrant)
        self.quadrant_list.append(dr_quadrant)
        self.quadrant_list.append(dl_quadrant)
        self.quadrant_list.append(ul_quadrant)

    def get_style(self,
                  border_size,
                  border_radius,
                  border_color,
                  background_color,
                  opacity,
                  extrastyle=None):
        stl = "background-color:rgba(0, 0, 0, 0%);" \
        + "border-width:" + str(border_size)  + "px;" \
        + "border-style:solid;" \
        + "border-radius:" + str(border_radius) + "px;" \
        + "border-color:" + str(border_color) + ";" \
        + "background-color:rgba(" + str(self.hextorgb(background_color)) + "," \
        + str(opacity) + '%)' + ";"
        if extrastyle != None:
            stl += extrastyle
        return stl

    def set_overlay(self, x, y, w, h):
        self.overlay_x_position = self.percentconvertor(x, self.screen_width)
        self.overlay_y_position = self.percentconvertor(y, self.screen_height)
        self.overlay_width = self.percentconvertor(w, self.screen_width)
        self.overlay_height = self.percentconvertor(h, self.screen_height)

    def create_sticks(self, label, xper, yper, btnsize, color, command):
        stick_widget = QWidget(self)
        nub = QWidget(self)
        dz = QWidget(stick_widget)
        stick_widget.raise_()

        if btnsize[0] != btnsize[1]:
            if btnsize[0] > btnsize[1]:
                btnsize = (btnsize[0], btnsize[0])
            else:
                btnsize = (btnsize[1], btnsize[1])

        if button_width != button_height:
            if button_width > button_height:
                bs = (button_width, button_width)
            elif button_height > button_width:
                bs = (button_height, button_height)
        if button_width == button_height:
            bs = (button_width, button_height)

        if override_button_size:
            stick_widget.resize(*btnsize)
            nub_width, nub_height = btnsize[0] / 2, btnsize[1] / 2
            nub.resize(nub_width, nub_height)
            dz_size = self.percentconvertor(deadzone, btnsize[0])
            dz.resize(dz_size, dz_size)
            extrastyle = "max-width:" + str(btnsize[0]) + "px;" \
             + "max-height:" + str(btnsize[0]) + "px;" \
             + "min-width:" + str(btnsize[0]) + "px;" \
             + "min-height:" + str(btnsize[0]) + "px;"
            stl = self.get_style(sticks_border_size, btnsize[0]/2, \
             sticks_border_color, sticks_color, button_opacity, extrastyle)
            stick_widget.setStyleSheet(stl)
            extrastyle = "max-width:" + str(nub_width) + "px;" \
             + "max-height:" + str(nub_width) + "px;" \
             + "min-width:" + str(nub_width) + "px;" \
             + "min-height:" + str(nub_width) + "px;"
            stl = self.get_style(sticks_border_size, nub_width/2, \
             sticks_border_color, sticks_nubs_color, button_opacity, extrastyle)
            nub.setStyleSheet(stl)
            extrastyle = "max-width:" + str(dz_size) + "px;" \
             + "max-height:" + str(dz_size) + "px;" \
             + "min-width:" + str(dz_size) + "px;" \
             + "min-height:" + str(dz_size) + "px;"
            stl = self.get_style(deadzone_border_size, dz_size/2, \
             deadzone_border_color, deadzone_color, button_opacity, extrastyle)
            dz.setStyleSheet(stl)
        else:
            stick_widget.resize(*bs)
            nub_width, nub_height = bs[0] / 2, bs[1] / 2
            nub.resize(nub_width, nub_height)
            dz_size = self.percentconvertor(deadzone, bs[0])
            dz.resize(dz_size, dz_size)
            extrastyle = "max-width:" + str(bs[0]) + "px;" \
             + "max-height:" + str(bs[0]) + "px;" \
             + "min-width:" + str(bs[0]) + "px;" \
             + "min-height:" + str(bs[0]) + "px;"
            stl = self.get_style(sticks_border_size, bs[0]/2, \
             sticks_border_color, sticks_color, button_opacity, extrastyle)
            stick_widget.setStyleSheet(stl)
            extrastyle = "max-width:" + str(nub_width) + "px;" \
             + "max-height:" + str(nub_width) + "px;" \
             + "min-width:" + str(nub_width) + "px;" \
             + "min-height:" + str(nub_width) + "px;"
            stl = self.get_style(sticks_border_size, nub_width/2, \
             sticks_border_color, sticks_nubs_color, button_opacity, extrastyle)
            nub.setStyleSheet(stl)
            extrastyle = "max-width:" + str(dz_size) + "px;" \
             + "max-height:" + str(dz_size) + "px;" \
             + "min-width:" + str(dz_size) + "px;" \
             + "min-height:" + str(dz_size) + "px;"
            stl = self.get_style(deadzone_border_size, dz_size/2, \
             deadzone_border_color, deadzone_color, button_opacity, extrastyle)
            dz.setStyleSheet(stl)

        xpos = self.percentconvertor(xper, self.overlay_width)
        ypos = self.percentconvertor(yper, self.overlay_height)
        stick_widget.move(xpos, ypos)
        dzx = self.roundify(stick_widget.width() / 2 - dz.width() / 2)
        dzy = self.roundify(stick_widget.height() / 2 - dz.height() / 2)
        dz.move(dzx, dzy)
        nubx = xpos + self.roundify(stick_widget.width() / 2 - nub.width() / 2)
        nuby = ypos + self.roundify(stick_widget.height() / 2 -
                                    nub.height() / 2)
        nub.move(nubx, nuby)
        if not show_deadzone:
            dz.hide()
        if not show_analog_sticks_nub:
            nub.hide()
        stick_widget.setFocusPolicy(QtCore.Qt.NoFocus)
        dz.setFocusPolicy(QtCore.Qt.NoFocus)
        nub.setFocusPolicy(QtCore.Qt.NoFocus)

        dz_startx = stick_widget.x() + dz.x()
        dz_endx = dz_startx + dz.width()
        dz_starty = stick_widget.y() + dz.y()
        dz_endy = dz_starty + dz.height()
        if label == "leftstick":
            stick_widget.setObjectName('leftstick')
            dz.setObjectName('leftstick_deadzone')
            nub.setObjectName('leftstick_nub')
            self.leftstick_deadzone_coords = [
                dz_startx, dz_endx, dz_starty, dz_endy
            ]
        if label == "rightstick":
            stick_widget.setObjectName('rightstick')
            dz.setObjectName('rightstick_deadzone')
            nub.setObjectName('rightstick_nub')
            self.rightstick_deadzone_coords = [
                dz_startx, dz_endx, dz_starty, dz_endy
            ]

    def move_nubs(self, widget, name, event_pos):
        nub = self.findChildren(QWidget, name)
        nub = nub[0]
        widget_startx = widget.x()
        widget_endx = widget_startx + widget.width()
        widget_starty = widget.y()
        widget_endy = widget_starty + widget.height()
        widget_xc = self.roundify((widget_startx + widget_endx) / 2)
        widget_yc = self.roundify((widget_starty + widget_endy) / 2)
        r = widget.width() / 2
        eventx = event_pos.x()
        eventy = event_pos.y()
        if ((eventx - widget_xc) * (eventx - widget_xc) +
            (eventy - widget_yc) * (eventy - widget_yc)) < r * r:
            x = widget_startx + (event_pos.x() - widget_startx) / 2
            y = widget_starty + (event_pos.y() - widget_starty) / 2
            nub.move(x, y)
            self.execute_nub_commands(widget, widget_xc, widget_yc, \
             widget_startx, widget_endx, widget_starty, widget_endy, name, eventx, eventy)

    def execute_nub_commands(self, stick, stick_xc, stick_yc, \
     stick_startx, stick_endx, stick_starty, stick_endy, name, x, y):
        if name == "leftstick_nub":
            dz = self.leftstick_deadzone_coords
            u, d, l, r = "leftstick_U", "leftstick_D", "leftstick_L", "leftstick_R"
        if name == "rightstick_nub":
            dz = self.rightstick_deadzone_coords
            u, d, l, r = "rightstick_U", "rightstick_D", "rightstick_L", "rightstick_R"
        if not self.is_point_inside_button(x, y, dz[0], dz[1], dz[2], dz[3]):
            percent = 13
            xc_minus_mod = self.roundify(
                stick_xc - self.percentconvertor(percent, stick.width()))
            xc_plus_mod = self.roundify(
                stick_xc + self.percentconvertor(percent, stick.width()))
            yc_minus_mod = self.roundify(
                stick_yc - self.percentconvertor(percent, stick.height()))
            yc_plus_mod = self.roundify(
                stick_yc + self.percentconvertor(percent, stick.height()))
            if self.is_point_inside_button(x, y, xc_minus_mod, xc_plus_mod, \
             stick_starty, yc_minus_mod):
                self.keyhandler(u, x, y)
            if self.is_point_inside_button(x, y, xc_minus_mod, xc_plus_mod, \
             yc_plus_mod, stick_endy):
                self.keyhandler(d, x, y)
            if self.is_point_inside_button(x, y, stick_startx, xc_minus_mod, \
             yc_minus_mod, yc_plus_mod):
                self.keyhandler(l, x, y)
            if self.is_point_inside_button(x, y, xc_plus_mod, stick_endx, \
             yc_minus_mod, yc_plus_mod):
                self.keyhandler(r, x, y)
            if self.is_point_inside_button(x, y, stick_startx, xc_minus_mod, \
             stick_starty, yc_minus_mod):
                self.keyhandler("", x, y, [u, l])
            if self.is_point_inside_button(x, y, xc_plus_mod, stick_endx, \
             stick_starty, yc_minus_mod):
                self.keyhandler("", x, y, [u, r])
            if self.is_point_inside_button(x, y, stick_startx, xc_minus_mod, \
             yc_plus_mod, stick_endy):
                self.keyhandler("", x, y, [d, l])
            if self.is_point_inside_button(x, y, xc_plus_mod, stick_endx, \
             yc_plus_mod, stick_endy):
                self.keyhandler("", x, y, [d, r])

    def recenter_nubs(self, widget, nub):
        widget_startx = widget.x()
        widget_endx = widget_startx + widget.width()
        widget_centerx = (widget_startx + widget_endx) / 2
        widget_starty = widget.y()
        widget_endy = widget_starty + widget.height()
        widget_centery = (widget_starty + widget_endy) / 2
        nub_startx = nub.x()
        nub_endx = nub_startx + nub.width()
        nub_center = (nub_startx + nub_endx) / 2
        x = self.roundify(widget_centerx - nub.width() / 2)
        y = self.roundify(widget_centery - nub.height() / 2)
        if widget_centerx != nub_center or abs(widget_centerx -
                                               nub_center) > 1:
            nub.move(x, y)

    def keyhandler(self, label, x=0, y=0, names=None):
        if names == None:
            # cmd = button_layout[label][2]
            cmd = self.useable_keys(label)
            if hide_on_close and label == "Hide":
                self.hidepad()
            elif not hide_on_close and label == "Close":
                self.quithandler()
            elif cmd:
                self.diagonal_movement_overlap_fix(label, x, y)
                self.execute_keypress(cmd, 'down', x, y, label)
        else:
            if names:
                for n in names:
                    cmd = self.useable_keys(n)
                    self.execute_keypress(cmd, 'down', x, y, n)

    def multitouch_fix(self, touch_points):
        tp = touch_points
        diff_list = []
        last_list = []
        second_last_list = []
        self.multitouch_points.append(tp)
        if len(self.multitouch_points) >= 2 and tp:
            last = self.multitouch_points[-1]
            second_last = self.multitouch_points[-2]
            if len(last) < len(second_last):
                for s in second_last:
                    for l in last:
                        s_pos = s.pos().toPoint()
                        l_pos = l.pos().toPoint()
                        ws = self.childAt(s_pos)
                        wl = self.childAt(l_pos)
                        if ws:
                            if hasattr(ws, 'text'):
                                second_last_list.append((s_pos, ws.text()))
                            elif ws.objectName() != '':
                                second_last_list.append(
                                    (s_pos, ws.objectName()))
                        if wl:
                            if hasattr(wl, 'text'):
                                last_list.append((l_pos, wl.text()))
                            elif wl.objectName() != '':
                                last_list.append((l_pos, wl.objectName()))
                        if second_last_list and last_list:
                            for sl in second_last_list:
                                for l in last_list:
                                    if sl[1] != l[1]:
                                        diff_list.append(sl)
        if diff_list:
            for d in diff_list:
                if d[1] == 'dpad_frame':
                    self.trigger_key_up(d[0].x(), d[0].y(), self.dpad_keys,
                                        d[1])
                elif d[1] == 'leftstick' or d[1] == 'leftstick_nub' or d[
                        1] == 'leftstick_deadzone':
                    self.trigger_key_up(d[0].x(), d[0].y(),
                                        self.leftstick_keys, d[1])
                elif d[1] == 'rightstick' or d[1] == 'rightstick_nub' or d[
                        1] == 'rightstick_deadzone':
                    self.trigger_key_up(d[0].x(), d[0].y(),
                                        self.rightstick_keys, d[1])
                else:
                    cmd = self.useable_keys(d[1])
                    self.trigger_key_up(d[0].x(), d[0].y(), [cmd], d[1])
        self.multitouch_points = self.multitouch_points[-2:]

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.TouchBegin \
         or event.type() == QtCore.QEvent.TouchUpdate:
            tp = event.touchPoints()
            self.multitouch_fix(tp)
            for t in tp:
                event_pos = t.pos().toPoint()
                event_x = event_pos.x()
                event_y = event_pos.y()
                widget = self.childAt(event_pos)
                if widget:
                    widget_name = widget.objectName()
                    if hasattr(widget, 'text'):
                        text = widget.text()
                        if text == "Stop All Inputs":
                            self.finish_all_inputs(event_x, event_y)
                        if text == "Settings":
                            self.show_settings_window()
                        self.keyhandler(text, event_x, event_y)
                    elif widget_name == "leftstick":
                        nub_name = 'leftstick_nub'
                        self.move_nubs(widget, nub_name, event_pos)
                    elif widget_name == "rightstick":
                        nub_name = 'rightstick_nub'
                        self.move_nubs(widget, nub_name, event_pos)
                    else:
                        self.check_other_possible_clickables(event_x, event_y)
                else:
                    self.trigger_key_up(event_x, event_y)
            return True
        if event.type() == QtCore.QEvent.TouchEnd:
            tp = event.touchPoints()
            for t in tp:
                event_pos = t.pos().toPoint()
                event_x = event_pos.x()
                event_y = event_pos.y()
                self.trigger_key_up(event_x, event_y)
            nub = self.findChildren(QWidget, 'leftstick_nub')
            if nub:
                nub = nub[0]
            widget = self.findChildren(QWidget, 'leftstick')
            if widget:
                widget = widget[0]
            if nub and widget:
                self.recenter_nubs(widget, nub)
            nub = self.findChildren(QWidget, 'rightstick_nub')
            if nub:
                nub = nub[0]
            widget = self.findChildren(QWidget, 'rightstick')
            if widget:
                widget = widget[0]
            if nub and widget:
                self.recenter_nubs(widget, nub)
            self.multitouch_points = []
            return True
        return False

    def check_other_possible_clickables(self, event_x, event_y):
        widget = self.childAt(event_x, event_y)
        if widget:
            name = widget.objectName()
            if name == "dpad_frame":
                for t in self.quadrant_list:
                    if self.is_point_inside_button(event_x, event_y, t[1],
                                                   t[2], t[3], t[4]):
                        self.move_diagonally(t[0], event_x, event_y)

    def move_diagonally(self, name, x, y):
        if name == "UR":
            self.keyhandler("", x, y, ["U", "R"])
        if name == "DR":
            self.keyhandler("", x, y, ["D", "R"])
        if name == "DL":
            self.keyhandler("", x, y, ["D", "L"])
        if name == "UL":
            self.keyhandler("", x, y, ["U", "L"])

    def diagonal_movement_overlap_fix(self, name, x, y):
        if name == "U" or name == "D" or name == "L" or name == "R":
            cmd = self.useable_keys(name)
            keys = list(self.dpad_keys)
            if cmd in keys:
                keys.remove(cmd)
                self.trigger_key_up(x, y, keys, name)
        if name == "leftstick_U" or name == "leftstick_D" or name == "leftstick_L" or name == "leftstick_R":
            cmd = self.useable_keys(name)
            keys = list(self.leftstick_keys)
            if cmd in keys:
                keys.remove(cmd)
                self.trigger_key_up(x, y, keys, name)
        if name == "rightstick_U" or name == "rightstick_D" or name == "rightstick_L" or name == "rightstick_R":
            cmd = self.useable_keys(name)
            keys = list(self.rightstick_keys)
            if cmd in keys:
                keys.remove(cmd)
                self.trigger_key_up(x, y, keys, name)

    def is_point_inside_button(self, x, y, startx, endx, starty, endy):
        if x >= startx and x <= endx \
         and y >= starty and y <= endy:
            return True
        return False

    def trigger_key_up(self, x=0, y=0, keys=None, label=None):
        if keys != None and label != None:
            if keys:
                for k in keys:
                    self.execute_keypress(k, 'up', x, y, label)
                    if k in self.keydown_list:
                        self.keydown_list.remove(k)
        if keys == None and label == None:
            if self.keydown_list:
                while self.keydown_list:
                    for i in self.keydown_list:
                        self.execute_keypress(i, 'up', x, y, label)
                        self.keydown_list.remove(i)

    def finish_all_inputs(self, x=0, y=0):
        if self.autorepeat_keylist:
            while self.autorepeat_keylist:
                for key in self.autorepeat_keylist:
                    if key[1] != None:
                        proc = key[1]
                        subprocess.call("ps -ef | awk '$3 == \"" +
                                        str(proc.pid) +
                                        "\" {print $2}' | xargs kill -9",
                                        shell=True)
                        proc.terminate()
                        proc.kill()
                        self.autorepeat_keylist.remove(key)
                    if key[1] == None:
                        if self.pyuserinput_process != None:
                            if self.pyuserinput_process.is_alive():
                                self.pyuserinput_process.kill_process()
                            self.autorepeat_keylist.remove(key)
        if self.sticky_keylist:
            while self.sticky_keylist:
                for i in self.sticky_keylist:
                    self.execute_keypress(i, 'up', x, y, None)
                    self.sticky_keylist.remove(i)

    def set_input_type(self):
        if input_method == "xdotool":
            self.keydown_string = "keydown"
            self.keyup_string = "keyup"
            self.mousedown_string = "mousedown"
            self.mouseup_string = "mouseup"
            self.key_tap_string = 'key'
            self.click_once_string = 'click'
        if input_method == "pyuserinput":
            self.keydown_string = "press_key"
            self.keyup_string = "release_key"
            self.mousedown_string = "press"
            self.mouseup_string = "release"
            self.key_tap_string = 'key'
            self.click_once_string = 'click'

    def useable_keys(self, label):
        keylist = []
        if not label == '':
            l = button_layout[label]
            for i in range(len(l) - 1):
                if l[i] == 'key' or l[i] == 'click':
                    keylist.append([l[i], l[i + 1]])
        return keylist

    def modify_keys(self, input_list, input_type):
        if input_list[0] == "key" and input_type == "down":
            input_list[0] = self.keydown_string
        if input_list[0] == "key" and input_type == "up":
            input_list[0] = self.keyup_string
        if input_list[0] == "click" and input_type == "down":
            input_list[0] = self.mousedown_string
        if input_list[0] == "click" and input_type == "up":
            input_list[0] = self.mouseup_string
        return input_list

    def execute_keypress(self, cmnd, keytype, x, y, name):
        if name != None and name != 'dpad_frame' and name != 'leftstick' \
         and name != 'rightstick' and name != 'leftstick_nub' \
         and name != 'rightstick_nub' and name != 'leftstick_deadzone' \
         and name != 'rightstick_deadzone':
            values = button_layout[name]
            value = values[5]
        else:
            value = 'normal'
        if cmnd:
            c = copy.deepcopy(cmnd)
            if input_method == "xdotool":
                if value == 'normal':
                    for a in c:
                        a = self.modify_keys(a, keytype)
                        a.insert(0, self.xdotool)
                        subprocess.Popen(a, stdout=subprocess.PIPE)
                    if not cmnd in self.keydown_list and keytype == 'down':
                        self.keydown_list.append(cmnd)
                if value == 'sticky':
                    for a in c:
                        a = self.modify_keys(a, keytype)
                        a.insert(0, self.xdotool)
                        subprocess.Popen(a, stdout=subprocess.PIPE)
                    if self.sticky_keylist:
                        if not cmnd in self.sticky_keylist and keytype == 'down':
                            self.sticky_keylist.append(cmnd)
                    else:
                        if keytype == 'down':
                            self.sticky_keylist.append(cmnd)
                if value == 'combo':
                    for a in c:
                        a.insert(0, self.xdotool)
                        subprocess.Popen(a, stdout=subprocess.PIPE)
                        time.sleep(combo_interval)
                if value == 'autorepeat':
                    if keytype == 'down':
                        if self.autorepeat_keylist:
                            l = []
                            for b in self.autorepeat_keylist:
                                if not cmnd in b:
                                    l.append(b)
                            if len(self.autorepeat_keylist) == len(l):
                                for a in c:
                                    a = 'while true; do ' + self.xdotool + ' ' + a[
                                        0] + ' --repeat ' + str(
                                            autorepeat_count
                                        ) + ' --delay ' + str(
                                            self.roundify(
                                                autorepeat_interval *
                                                1000)) + ' ' + a[1] + '; done'
                                    p = subprocess.Popen(
                                        a, stdout=subprocess.PIPE, shell=True)
                                    self.autorepeat_keylist.append((cmnd, p))
                        else:
                            for a in c:
                                a = 'while true; do ' + self.xdotool + ' ' + a[
                                    0] + ' --repeat ' + str(
                                        autorepeat_count) + ' --delay ' + str(
                                            self.roundify(
                                                autorepeat_interval *
                                                1000)) + ' ' + a[1] + '; done'
                                p = subprocess.Popen(a,
                                                     stdout=subprocess.PIPE,
                                                     shell=True)
                                self.autorepeat_keylist.append((cmnd, p))
            if input_method == "pyuserinput":
                if value == 'normal':
                    for a in c:
                        a = self.modify_keys(a, keytype)
                        if a[0] == "press_key":
                            self.py_keyboard.press_key(a[1])
                        if a[0] == "release_key":
                            self.py_keyboard.release_key(a[1])
                        if a[0] == "press":
                            self.py_mouse.press(x, y, int(a[1]))
                        if a[0] == "release":
                            self.py_mouse.release(x, y, int(a[1]))
                    if not cmnd in self.keydown_list and keytype == 'down':
                        self.keydown_list.append(cmnd)
                if value == 'combo':
                    for a in c:
                        if a[0] == "key":
                            self.py_keyboard.tap_key(a[1])
                        if a[0] == "click":
                            self.py_mouse.click(x, y, int(a[1]))
                        time.sleep(combo_interval)
                if value == 'sticky':
                    for a in c:
                        a = self.modify_keys(a, keytype)
                        if a[0] == "press_key":
                            self.py_keyboard.press_key(a[1])
                        if a[0] == "release_key":
                            self.py_keyboard.release_key(a[1])
                        if a[0] == "press":
                            self.py_mouse.press(x, y, int(a[1]))
                        if a[0] == "release":
                            self.py_mouse.release(x, y, int(a[1]))
                    if self.sticky_keylist:
                        if not cmnd in self.sticky_keylist and keytype == 'down':
                            self.sticky_keylist.append(cmnd)
                    else:
                        if keytype == 'down':
                            self.sticky_keylist.append(cmnd)
                if value == 'autorepeat':
                    if keytype == 'down':
                        if self.autorepeat_keylist:
                            l = []
                            for b in self.autorepeat_keylist:
                                if not cmnd in b:
                                    l.append(b)
                            if len(self.autorepeat_keylist) == len(l):
                                p = None
                                for a in c:
                                    if a[0] == "key":
                                        self.pyuserinput_autorepeater(
                                            x, y, a[1], 'key')
                                    if a[0] == "click":
                                        self.pyuserinput_autorepeater(
                                            x, y, int(a[1]), 'click')
                                self.autorepeat_keylist.append((cmnd, p))
                        else:
                            p = None
                            for a in c:
                                if a[0] == "key":
                                    self.pyuserinput_autorepeater(
                                        x, y, a[1], 'key')
                                if a[0] == "click":
                                    self.pyuserinput_autorepeater(
                                        x, y, int(a[1]), 'click')
                            self.autorepeat_keylist.append((cmnd, p))

    def pyuserinput_autorepeater(self, x, y, key, method):
        self.pyuserinput_process = newProcess(
            1, 'PyUserInput Autorepeat Process', x, y, key, method)
        self.pyuserinput_process.start()

    def restart_program(self):
        """Restarts the current program.
		Note: this function does not return. Any cleanup action (like
		saving data) must be done before calling this function."""
        self.cleanup_before_exit()
        python = sys.executable
        os.execl(python, python, *sys.argv)

    def cleanup_before_exit(self):
        self.finish_all_inputs()
        self.trigger_key_up()
        if self.pyuserinput_process != None:
            if self.pyuserinput_process.is_alive():
                self.pyuserinput_process.kill_process()

    def signal_handler(self, signal, frame):
        print('You forced killed the app.')
        self.quithandler()

    def show_settings_window(self):
        self.hidepad()
        for widget in QApplication.allWidgets():
            if type(widget).__name__ == 'MainSettings':
                widget.close()

        self.settings_window = MainSettings(self)
        r = self.settings_window.exec_()

    def show_layout_window(self):
        self.hidepad()
        for widget in QApplication.allWidgets():
            if type(widget).__name__ == 'LayoutSettings':
                widget.close()
        self.layout_window = LayoutSettings(self)
        r = self.layout_window.exec_()

    def show_about_dialog(self):
        self.hidepad()
        for widget in QApplication.allWidgets():
            if type(widget).__name__ == 'Dialog':
                widget.close()
        text = (
            "TabPad is an onscreen gamepad for Linux touchscreen devices (mainly tablets).<br><br>"
            "Developed by nitg16.<br><br>"
            "<a href=\"https://github.com/nitg16/TabPad\">Source Code</a>")
        d = Dialog(self, text, 'About TabPad')
        r = d.exec_()
Exemple #46
0
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)
		my=int((int(h_screen)/480)*cy)
		print mx,my


		#always keep maximum of 20 points in the trail
		if len(trail)==20:
			trail.pop(0)

		#append the centroid to the trail
		trail.append((cx,cy))

		#mouse events by hand pose 
		if int(resp)==1 and mouse_enable:
			if pressed:
				pressed=False
				m1.release(mx,my)
			m1.move(mx,my)
		if int(resp)==2 and mouse_enable:
			pressed=True
			m1.press(mx,my)


		#put the hand pose on the display
		cv2.putText(img,resp,(x,y), font,1,(255,255,255),2,cv2.LINE_AA)
	

	#draw the trail
	for i in xrange(0,len(trail)-1):
		cv2.line(img,trail[i],trail[i+1],[0,0,255],2)

Exemple #48
0
				else:
					keyboard_object.press_key(map_special_key(event['key'], keyboard_object))
				print print_out

			elif event['type'] == 'keyup':
				print_out = '[>] Releasing key ' + str(event['key'])
				if 'unicode' in event and ascii_printable(event['unicode']):
					keyboard_object.release_key(event['unicode'])
					print_out += ' (' + event['unicode'] + ')'
				else:
					keyboard_object.release_key(map_special_key(event['key'], keyboard_object))
				print print_out
			elif event['type'] == 'mousemotion':
				mouse_object.move(event['pos'][0]*screen_x, event['pos'][1]*screen_y)
				print '[>] Moving mouse cursor to ' + str(event['pos'][0]*screen_x) + ', ' + str(event['pos'][1]*screen_y)
			elif event['type'] == 'mousebuttondown':
				button = 1 if event['button'] == 1 else 2
				mouse_object.press(event['pos'][0]*screen_x, event['pos'][1]*screen_y, button)
				print '[>] Mouse pressing at ' + str(event['pos'][0]*screen_x) + ', ' + str(event['pos'][1]*screen_y)
			elif event['type'] == 'mousebuttonup':
				button = 1 if event['button'] == 1 else 2
				mouse_object.release(event['pos'][0]*screen_x, event['pos'][1]*screen_y, button)
				print '[>] Mouse releasing at ' + str(event['pos'][0]*screen_x) + ', ' + str(event['pos'][1]*screen_y)
		connection.close()
		print '[>] Connection closed'
		break
socket_handle.close()
print '[>] Socket closed'
print '[>] Exit program'

        M = cv2.moments(cnt)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        l.append((cx, cy))
        #mark the centroid in the image
        cv2.circle(img, (cx, cy), 5, [0, 255, 0], -1)

        #get mouse location
        mx = int((int(w_screen) / 640) * cx)
        my = int((int(h_screen) / 480) * cy)
        print mx, my
        #mouse events by hand pose
        if int(resp) == 1 and mouse_enable:
            if pressed:
                pressed = False
                m1.release(mx, my)
            m1.move(mx, my)
        if int(resp) == 2:
            press_count += 1
        if int(resp) == 2 and mouse_enable:
            pressed = True
            m1.press(mx, my)
        #put the hand pose on the display
        cv2.putText(img, resp, (x, y), font, 1, (255, 255, 255), 2,
                    cv2.LINE_AA)
    if len(l) == 2:
        if len(event_que) == 10:
            angle_change = int(event_que[9][1])
            dist_change = int(event_que[9][0])
            event_que.pop(0)
Exemple #50
0
from pymouse import PyMouse
import time

m = PyMouse()
i = 0
while i < 603:
    m.click(30,300,1)
    time.sleep(1)
    m.click(980,515,1)
    time.sleep(1)
    m.click(1125,715,1)
    time.sleep(1)
    m.press(638,94,1)
    time.sleep(1)
    m.release(1395,1055,1)
    time.sleep(1)
    m.click(1135,675)
    time.sleep(1)
    m.click(1825,60)
    time.sleep(1)
    i = i + 1
Exemple #51
0
class Controller():

	def __init__(self,speed=8):
		self.m = PyMouse()
		self.k = PyKeyboard()
		self.speed = speed*8

		self.xy = {
				'x': 0,
				'y': 0,
				'd_x': 0,
				'd_y': 0,
			}

	def run(self):
		pipe = open('/dev/input/js0','rb')

		try:
			self._move_mouse_thread = Thread(target=self._move_mouse)
			self._move_mouse_thread.start()

			while 1:
				nb = pipe.read(8)
				self._handle(gen_inf(nb))
		except RuntimeError as e:
			raise e

	def _handle(self,inf):
		try:
			if inf['btn'] == 'a_lx':
				self.xy['d_x'] = int(inf['lvl']/self.speed)
			if inf['btn'] == 'a_ly':	
				self.xy['d_y'] = int(inf['lvl']/self.speed)
			if inf['btn'] == 'a_rx':
				self.xy['d_x'] = int(inf['lvl']/(self.speed*4))
			if inf['btn'] == 'a_ry':	
				self.xy['d_y'] = int(inf['lvl']/(self.speed*4))

			if inf['btn'] == 'b_lb' and inf['lvl'] == 1:
				self.m.press(
						x=self.xy['x'],
						y=self.xy['y'],
						button=1,
				)
			elif inf['btn'] == 'b_lb' and inf['lvl'] == 0:
				self.m.release(
						x=self.xy['x'],
						y=self.xy['y'],
						button=1,
				)

			if inf['btn'] == 'b_rb' and inf['lvl'] == 1:
				self.m.press(
						x=self.xy['x'],
						y=self.xy['y'],
						button=2,
				)
			elif inf['btn'] == 'b_rb' and inf['lvl'] == 0:
				self.m.release(
						x=self.xy['x'],
						y=self.xy['y'],
						button=2,
				)
		except RuntimeError as e:
			raise e


	def _move_mouse(self):
		try:
			while 1:
				self.xy['x'] += self.xy['d_x']
				self.xy['y'] += self.xy['d_y']
				self.m.move(
						x=self.xy['x']+self.xy['d_x'],
						y=self.xy['y']+self.xy['d_y'],
				)
				sleep(1/120)
		except RuntimeError as e:
			raise e

	def __del__(self):
		print('Stopping driver...')
Exemple #52
0
# -*- 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)
Exemple #53
0
def get_results(args, H):
    tf.reset_default_graph()
    x_in = tf.placeholder(tf.float32,
                          name='x_in',
                          shape=[H['image_height'], H['image_width'], 3])
    if H['use_rezoom']:
        pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(
            H, tf.expand_dims(x_in, 0), 'test', reuse=None)
        grid_area = H['grid_height'] * H['grid_width']
        pred_confidences = tf.reshape(
            tf.nn.softmax(
                tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])),
            [grid_area, H['rnn_len'], 2])
        if H['reregress']:
            pred_boxes = pred_boxes + pred_boxes_deltas
    else:
        pred_boxes, pred_logits, pred_confidences = build_forward(
            H, tf.expand_dims(x_in, 0), 'test', reuse=None)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        saver.restore(sess, args.weights)

        pred_annolist = al.AnnoList()

        data_dir = os.path.dirname(args.test_boxes)
        image_dir = get_image_dir(args)
        subprocess.call('mkdir -p %s' % image_dir, shell=True)

        memory = sysv_ipc.SharedMemory(123463)
        memory2 = sysv_ipc.SharedMemory(123464)
        size = 768, 1024, 3

        pedal = PyMouse()
        pedal.press(1)
        road_center = 320
        while True:
            cv2.waitKey(1)
            frameCount = bytearray(memory.read())
            curve = bytearray(memory2.read())
            curve = str(struct.unpack('i', curve)[0])
            m = np.array(frameCount, dtype=np.uint8)
            orig_img = m.reshape(size)

            img = imresize(orig_img, (H["image_height"], H["image_width"]),
                           interp='cubic')
            feed = {x_in: img}
            (np_pred_boxes,
             np_pred_confidences) = sess.run([pred_boxes, pred_confidences],
                                             feed_dict=feed)
            pred_anno = al.Annotation()

            new_img, rects = add_rectangles(
                H, [img],
                np_pred_confidences,
                np_pred_boxes,
                use_stitching=True,
                rnn_len=H['rnn_len'],
                min_conf=args.min_conf,
                tau=args.tau,
                show_suppressed=args.show_suppressed)
            flag = 0
            road_center = 320 + int(curve)
            print(road_center)
            for rect in rects:
                print(rect.x1, rect.x2, rect.y2)
                if (rect.x1 < road_center and rect.x2 > road_center
                        and rect.y2 > 200) and (rect.x2 - rect.x1 > 30):
                    flag = 1

            if flag is 1:
                pedal.press(2)
                print("break!")
            else:
                pedal.release(2)
                pedal.press(1)
                print("acceleration!")

            pred_anno.rects = rects
            pred_anno.imagePath = os.path.abspath(data_dir)
            pred_anno = rescale_boxes((H["image_height"], H["image_width"]),
                                      pred_anno, orig_img.shape[0],
                                      orig_img.shape[1])
            pred_annolist.append(pred_anno)

            cv2.imshow('.jpg', new_img)

    return none
class TestSaisieCarhab(object):

    '''Speed of mouse cursor move, 0.01 =< float =< 1 (0.2 for realistic move)'''
    MOVE_SPEED = 0.2
    # Do full test or a part only
    TESTS_TO_CHECK = '' 
    ''' available values : 'up_to_fusion',
                           'up_to_undo_fusion', 
                           'up_to_quit_edit_fusion' 
                           'up_to_fusion_import_feat',
                           'up_to_undo_import_feat', 
                           'up_to_quit_edit_import_feat',
                           every thing else for full test'''
    
    def __init__(self):
        '''Constructor.
        
        :param iface: An interface instance that will be passed to this class
        which provides the hook by which you can manipulate the QGIS
        application at run time.
        :type iface: QgsInterface
        '''
        self.mouse = PyMouse()
        self.keyboard = PyKeyboard()

    def screenShot(self, imgName = 'debugScreenshot'):
        '''Shoot screen.
        
            :param imgName: Text value of the action to click on.
            :type imgName: QString
            :default imgName: 'debugScreenshot.png'
        '''
        
        iface.mapCanvas().saveAsImage(imgName)
    
    def addTestVectorLayer(self, path='test/data/segment.shp'):
        '''Add a vector layer to the map.
        
            :param path: Shapefile name to add.
            :type path: QString
            :default path: 'test/data/segement.shp'
            
            :return: Vector layer which has been added to map canvas
            :rtype: QgsVectorLayer
        '''

        # Set legend name from path
        layerName = path.split('/')[-1:][0].split('.shp')[0]
        layer = QgsVectorLayer(path, layerName, 'ogr')
        QgsMapLayerRegistry.instance().addMapLayer(layer)
        # Useful to let QGIS processing
        QgsApplication.processEvents()
        return iface.mapCanvas().layers()[0]

    def findButtonByAction(self, action):
        '''Find button corresponding to the given action.
        
            :param buttonActionName: Text value of the action.
            :type buttonActionName: QString
            
            :return: Widget if found, None else.
            :rtype: QWidget or None
        '''
        for widget in action.associatedWidgets():
            if type(widget) == QtGui.QToolButton:
                return widget
        return None

    def findButtonByActionName(self, buttonActionName):
        '''Find button corresponding to the given action.
        
            :param buttonActionName: Text value of the action.
            :type buttonActionName: QString
            
            :return: Widget if found, None else.
            :rtype: QWidget or None
        '''
        for tbar in iface.mainWindow().findChildren(QtGui.QToolBar):
            for action in tbar.actions():
                if action.text() == buttonActionName:
                    for widget in action.associatedWidgets():
                        if type(widget) == QtGui.QToolButton:
                            return widget
        return None

    def mapToScreenPoint(self, mapCoordPoint):
        '''Convert point on the map canvas from map to screen coordinates.
        
            :param mapCoordPoint: Point in map coordinates.
            :type mapCoordPoint: QgsPoint
            
            :return: Point in screen coordinates
            :rtype: QPoint
        '''
        # Get point in px into map canvas referential
        sourceScreen = iface.mapCanvas().getCoordinateTransform().transform(mapCoordPoint)
        sourceRelScreen = sourceScreen.toQPointF().toPoint()

        # find top left canvas pos
        canvas = iface.mapCanvas()
        canvasPos = canvas.mapToGlobal(QtCore.QPoint( canvas.rect().x(), canvas.rect().y()))

        # Get point in px into screen referential
        sourceAbsScreen = QtCore.QPoint(sourceRelScreen.x() + canvasPos.x(), sourceRelScreen.y() + canvasPos.y())
        
        return sourceAbsScreen

    def moveTo(self, dest, rate=MOVE_SPEED):
        '''Move smoothly mouse cursor to a destination point.
        
            :param dest: Mouse cursor destination point.
            :type dest: QPoint

            :param rate: Speed of mouse movement : 1 = 100px/sec.
            :type rate: float >= 0.01
            :default rate: TestSaisieCarhab constant MOVE_SPEED
        '''
        source = self.mouse.position()
        npoints = int(np.sqrt((dest.x()-source[0])**2 + (dest.y()-source[1])**2 ) / (rate*100))
        for i in range(npoints):
            x = int(source[0] + ((dest.x()-source[0])/npoints)*i)
            y = int(source[1] + ((dest.y()-source[1])/npoints)*i)
            self.mouse.move(x,y)
            time.sleep(0.01)

            # Useful to let QGIS processing
            QgsApplication.processEvents()

        self.mouse.move(dest.x(), dest.y())
        
        # Useful to let QGIS processing
        QgsApplication.processEvents()

    def clickOnMapPoint(self, point):
        '''Click on the map at the given point (map coordinates).
        
            :param point: Widget to click on.
            :type point: QgsPoint
        '''

        sceenCoordPoint = self.mapToScreenPoint(point)
       
        self.moveTo(sceenCoordPoint)
        self.mouse.click(sceenCoordPoint.x(), sceenCoordPoint.y(), 1)
        
        # Useful to let QGIS processing
        QgsApplication.processEvents()
        
    def clickOnWidget(self, widget):
        '''Click on the given widget.
        
            :param widget: Widget to click on.
            :type widget: QWidget
        '''

        widgetX = widget.rect().center().x()
        widgetY = widget.rect().center().y()
        widgetPos = widget.mapToGlobal(QtCore.QPoint(widgetX, widgetY))
       
        self.moveTo(widgetPos)
        self.mouse.click(widgetPos.x(), widgetPos.y(), 1)
        
        # Useful to let QGIS processing
        QgsApplication.processEvents()
        
    def clickOnWidgetByActionName(self, actionName):
        '''Click on action by its text value.
        
            :param actionName: Text value of the action to click on.
            :type actionName: QString
        '''
        
        button = self.findButtonByActionName(actionName)
        self.clickOnWidget(button)

    def dragAndDropScreen(self, source, dest):
        '''Press mouse at source point, move to destination point and release.
        
            :param source: Drag start position.
            :type source: QPoint
            
            :param dest: Mouse cursor destination point.
            :type dest: QPoint
        '''

        self.moveTo(source)
        self.mouse.press(source.x(), source.y(), 1)
        
        # Useful to let QGIS processing
        QgsApplication.processEvents()

        self.moveTo(dest)
        self.mouse.release(dest.x(), dest.y(), 1)
        
        # Useful to let QGIS processing
        QgsApplication.processEvents()

    def dragAndDropMap(self, source, dest):
        '''DragAndDropScreen in map coordinates.
        
            :param source: Drag start position.
            :type source: QPoint in map coordinates
            
            :param dest: Mouse cursor destination point.
            :type dest: QPoint in map coordinates
        '''
        screenCoordSource = self.mapToScreenPoint(source)
        screenCoordDest = self.mapToScreenPoint(dest)

        self.dragAndDropScreen(screenCoordSource, screenCoordDest)
    
    def ctrlZ(self):
        '''Simulates 'CTRL + z' type on the keyboard.'''
        
        self.keyboard.press_key(self.keyboard.control_l_key)
        time.sleep(0.25)
        self.keyboard.tap_key(self.keyboard.type_string('z'), 0, 0)
        time.sleep(0.25)
        self.keyboard.release_key(self.keyboard.control_l_key)
        # Useful to let QGIS processing
        QgsApplication.processEvents()
    
    def quitLayerEditMode(self, layer):
        '''Rollback changes on the layer and quit edit mode.
        
            :param layer: Layer in edit mode.
            :type layer: QVectorLayer
        '''
        
        layer.rollBack()
        # Useful to let QGIS processing
        QgsApplication.processEvents()
        # Refresh layer layout
        layer.triggerRepaint()
    
    def featuresCount(self, layer):
        '''Count the features of a vector layer.
        
            :param layer: Vector layer.
            :type layer: QgsVectorLayer
            
            :return: Layer features count.
            :rtype: int
        '''

        count = 0
        for feature in layer.getFeatures():
            count += 1
        return count
    
    def printTest(self, expected, result, testName, valueToTestName):
        '''Print test result.
        
            :param expected: expected value to test.
            :type expected: unknown
			
            :param result: result value to test.
            :type result: unknown
			
            :param testName: Name of test (into header printed).
            :type testName: QString
            
			:param valueToTestName: Tested variable name (printed if test fails).
            :type valueToTestName: QString
            
            :return: True if test pass, False else.
            :rtype: Bool
        '''

        test = False
        msg = testName+''' : Fails
                '''+valueToTestName+''' expected : '''+str(expected)+'''
                '''+valueToTestName+''' : '''+str(result)
        if expected == result:
            test = True
            msg = testName+''' : OK'''
        print msg
        return test
    
    def testFusion(self):
        '''Simulate user moves and check results.'''

        # Open python console
        iface.actionShowPythonDialog().trigger()
        # Get map tool action that is activated
        previousActivatedMapToolActionName = iface.mapCanvas().mapTool().action().text()
        
        # Simulate click on edit mode button
#        self.clickOnWidgetByActionName(iface.actionToggleEditing().text())

        # Simulate click on plugin fusion button
        JobManager().create_carhab_lyr('test')
        
#        # Simulate 2nd click and check if fusion button stay checked
#        self.clickOnWidgetByActionName('Fusion')
#        # Fusion button must be enabled
#        if not self.printTest(True, self.findButtonByActionName('Fusion').isChecked(), 'Fusion button stays checked', 'Fusion button status'):
#            return
#        
#        # Press (left click) and move mouse on the map canvas to carry out the fusion
#        self.dragAndDropMap(QgsPoint(783902,6528458),  QgsPoint(785223,6528279))
#
#        # Features must have been merged (test with features count)
#        if not self.printTest(35, self.featuresCount(layer), 'Fusion', 'Features count'):
#            return
#
#        # End of test ?
#        if self.TESTS_TO_CHECK == 'up_to_fusion':
#            return
#
#        # Test undo action
#        self.ctrlZ() 
#        if not self.printTest(37, self.featuresCount(layer), 'Undo fusion(Ctrl + z)', 'Features count'):
#            return
#
#        # End of test ?
#        if self.TESTS_TO_CHECK == 'up_to_undo_fusion':
#            return
#
#        # Quit edit mode
#        self.clickOnWidgetByActionName(iface.actionToggleEditing().text())
#
#        # Fusion button must be enabled
#        if not self.printTest(False, self.findButtonByActionName('Fusion').isEnabled(), 'Disable fusion button', 'Fusion button status'):
#            return
#
#        # Fusion button must be enabled
#        if not self.printTest(False, self.findButtonByActionName('Fusion').isChecked(), 'Uncheck fusion button', 'Fusion button status'):
#            return
#
#        # Previous map tool button must be re-checked
#        if not self.printTest(True, self.findButtonByActionName(previousActivatedMapToolActionName).isChecked(), 'Check previous map tool button ('+previousActivatedMapToolActionName+')', 'Previous map tool button ('+previousActivatedMapToolActionName+') status'):
#            return
#
#        # End of test ?
#        if self.TESTS_TO_CHECK == 'up_to_quit_edit_fusion':
#            return
#
#        # Clear layers
#        QgsMapLayerRegistry.instance().removeAllMapLayers()
#        iface.mapCanvas().refresh()
#        # Close python console
#        iface.actionShowPythonDialog().trigger()
#
#        self.testImportFeature()

    def testImportFeature(self):
        '''Simulate user moves and check results.'''

        # Open python console
        iface.actionShowPythonDialog().trigger()
        # Get map tool action that is activated
        previousActivatedMapToolActionName = iface.mapCanvas().mapTool().action().text()
        #Add test layers to map registry
        layerDest = self.addTestVectorLayer()
        layerSource = self.addTestVectorLayer('test/data/segment2.shp')
        
        iface.setActiveLayer(layerDest)
        
        # Simulate click on edit mode button
        self.clickOnWidgetByActionName(iface.actionToggleEditing().text())

        # Simulate click on plugin fusion button
        self.clickOnWidgetByActionName('Import Feature')

        # Move and click on the feature to import
        self.clickOnMapPoint(QgsPoint(785519,6528705))

        # Feature must have been added
        if not self.printTest(38, self.featuresCount(layerDest), 'Import feature', 'Features count'):
            return

        # End of test ?
        if self.TESTS_TO_CHECK == 'up_to_import_feature':
            return
        
        # Test undo action
        self.ctrlZ() 
        if not self.printTest(37, self.featuresCount(layerDest), 'Undo Import feature(Ctrl + z)', 'Features count'):
            return

        # End of test ?
        if self.TESTS_TO_CHECK == 'up_to_undo_import_feat':
            return

        # Quit edit mode
        self.clickOnWidgetByActionName(iface.actionToggleEditing().text())

        # Fusion button must be enabled
        if not self.printTest(False, self.findButtonByActionName('Import Feature').isEnabled(), 'Disable fusion button', 'Fusion button status'):
            return

        # Previous map tool button must be re-checked
        if not self.printTest(True, self.findButtonByActionName(previousActivatedMapToolActionName).isChecked(), 'Check previous map tool button ('+previousActivatedMapToolActionName+')', 'Previous map tool button ('+previousActivatedMapToolActionName+') status'):
            return

        # End of test ?
        if self.TESTS_TO_CHECK == 'up_to_quit_edit_import_feat':
            return

        # Clear layers
        #QgsMapLayerRegistry.instance().removeAllMapLayers()
        iface.mapCanvas().refresh()
Exemple #55
0
 def release(self, x, y, button=1):
     PyMouse.release(self, x, y, button)
     if button == 1:
         self.leftmousepressed = False
     elif button == 2:
         self.rightmousepressed = False
Exemple #56
0
 time.sleep(random.randint(7, 15))
 driver.find_element_by_xpath(
     '/html/body/div[1]/div/div/div/div[2]/form/div[2]/span[1]/input'
 ).click()
 time.sleep(random.randint(2, 5))
 #driver.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/form/div[2]/span[1]/input').send_keys('Nosecret521')
 driver.find_element_by_xpath(
     '/html/body/div[1]/div/div/div/div[2]/form/div[2]/span[1]/input'
 ).send_keys('Nosecret521')
 time.sleep(random.randint(2, 5))
 #place of huakuaier
 m.move(494, 296)
 time.sleep(random.randint(2, 5))
 m.drag(710, 296)
 time.sleep(random.randint(7, 15))
 m.release(710, 296, 1)
 time.sleep(random.randint(7, 15))
 driver.find_element_by_xpath(
     '/html/body/div[1]/div/div/div/div[2]/form/div[2]/span[1]/input'
 ).send_keys(Keys.RETURN)
 time.sleep(random.randint(7, 15))
 cookies = [
     item["name"] + "=" + item["value"] for item in driver.get_cookies()
 ]
 cookiestr = ';'.join(item for item in cookies)
 #f=file("/root/weixin.11tn.com/mp/almmck.txt","w+")
 f = file("/data/tbk/requests/almmck.txt", "w+")
 f.writelines(cookiestr)
 #print currurl
 f.close()
 #driver.save_screenshot("3.png")
Exemple #57
0
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,
        "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,
            "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, 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 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