def m_c(): from pynput.mouse import Button, Controller mouse = Controller() for i in range(0, 1365): for j in range(0, 767): mouse.move(i, j) return None
def runLoop0(): from pynput.mouse import Button, Controller mouse = Controller() mousex = 0.00 mousey = 0.00 print('runloop0 started') while True: line = s.readline() #print(line) line2 = str(line) line3 = line2[:-5] line4 = line3.replace("b' ", "") #print(line4) if ('mouseclick' in line4): mouse.click(Button.left, 1) #!! if ('mousepress' in line4): mouse.press(Button.left) if ('mouserelease' in line4): mouse.release(Button.left) if ('rollmap' in line4): line5 = line4.replace("rollmap", "") line6 = line5.replace(" ", "") print(line6) if (float(line6) > 10): mousex = float(line6) / 4 elif (float(line6) < -10): mousex = float(line6) / 4 else: mousex = 0 if ('pitchmap' in line4): line5 = line4.replace("pitchmap", "") line6 = line5.replace(" ", "") print(line6) if (float(line6) > 10): mousey = float(line6) / 4 elif (float(line6) < -10): mousey = float(line6) / 4 else: mousey = 0 if ('lightvaldown' in line4): mouse.click(Button.right, 1) mouse.move(mousex, mousey)
class MouseController(): def __init__(self): pass self.mouse = Controller() def mouse_position(self): # Read pointer position print('The current pointer position is {0}'.format( self.mouse.position)) return self.mouse.position def mouse_postiion_set(self, x, y): # Set pointer position self.mouse.position = (x, y) print('Now we have moved it to {0}'.format(self.mouse.position)) def mouse_move(self, x, y): # Move pointer relative to current position self.mouse.move(x, y) def mouse_press(self, right=False): if right == True: self.mouse.press(Button.right) else: self.mouse.press(Button.left) def mouse_release(self, right=False): if right == True: self.mouse.release(Button.right) else: self.mouse.release(Button.left) def mouse_double_click(self, right=False): # Double click; this is different from pressing and releasing # twice on Mac OSX if right == 'right' or right == True: self.mouse.click(Button.right, 2) else: self.mouse.click(Button.right, 2) def mouse_scroll(self, x, y): # Scroll two steps down self.mouse.scroll(x, y)
def handTracking(self): mC = Controller() # mB = Button() mouseX = -1 mouseY = -1 cv2.namedWindow("preview") vc = cv2.VideoCapture(0) if vc.isOpened(): rval, frame = vc.read() else: rval = False while rval: rval, frame = vc.read() # img = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) # frm = frame[120:400,80:400] img = cv2.resize(frame, (180, 120)) data = [] data.append(img) data = np.array(data) data = data.reshape(1, 120, 180, 3) data = data / 255 x = self.model.predict(data) xl = self.fromScale(640, x[0][0]) yl = self.fromScale(480, x[0][1]) xr = self.fromScale(640, x[0][2]) yr = self.fromScale(480, x[0][3]) # print(xl,yl,xr,yr) frame = cv2.rectangle(frame, (xl - 50, yl - 20), (xr + 20, yr + 20), (255, 0, 0), 2) # frame = cv2.rectangle(frame,(0,0),(40,40),(255,114,36),2) if xl > 0 and yl > 0 and xr > xl and yr > yl: yl -= 20 yr += 20 xl -= 50 xr += 20 if yl < 0: yl = 0 if xl < 0: xl = 0 if yr >= 480: yr = 479 if xr >= 640: xr = 639 dtimg = frame[yl:yr, xl:xr] dt = [] dtimg = cv2.resize(dtimg, (180, 120)) dt.append(dtimg) dt = np.asarray(dt) dt = dt.reshape(1, 120, 180, 3) dt = dt / 255 value = np.argmax(self.model_Cat.predict(dt)[0]) print(value) # if self.gesture != value: # self.gesture = value # self.counters = [0,0,0,0,0] if value == 5: self.counter = [0, 0, 0, 0, 0] if value < 5: self.counters[value] += 1 if value != 5: i = np.argmax(self.counters) if mouseX != -1 and mouseY != -1: xC = int((xl + xr) / 2) yC = int((yl + yr) / 2) xC = int((xC / 640) * 1920) yC = int((yC / 480) * 1080) difX = 0 difY = 0 if xC > mouseX: difX = 200 if xC < mouseX: difX = -200 if yC > mouseY: difY = 200 if yC < mouseY: difY = -200 mouseX += difX mouseY += difY mC.move(difX, difY) else: xC = int((xl + xr) / 2) yC = int((yl + yr) / 2) pxC = xC / 640 pyC = yC / 480 mC.position = (500, 500) mouseX = 500 mouseY = 500 # print([np.argmax(self.model_Cat.predict(dt)[0]),np.argmax(self.model_Cat.predict(dt)[0]),np.argmax(self.model_Cat.predict(dt)[0])]) cv2.imshow("preview", frame) key = cv2.waitKey(5) if key == 27: break cv2.destroyWindow("preview")
def shubiao(jian): from pynput.mouse import Controller, Button mouse = Controller() #设置鼠标键 global kaiguan if kaiguan == 1: if jian in '7': print(jian) mouse.move(-20, -25) elif jian in '8': mouse.move(0, -20) elif jian in '9': mouse.move(20, -25) elif jian in '4': mouse.move(-20, 0) elif jian in '5': mouse.click(Button.left, 1) #左键在单击 elif jian in '6': mouse.move(30, 0) elif jian in '1': mouse.move(-30, 35) elif jian in '2': mouse.move(0, 15) elif jian in '3': mouse.move(30, 20) elif jian in '-': mouse.click(Button.right, 1) #左键双击 elif jian in '+': mouse.click(Button.left, 1) #右键单击 elif jian in '/': mouse.scroll(0, 20) #滚轮向下移动一个单位 elif jian in '*': mouse.scroll(0, -30) #轮向上移动一个单位 elif jian in '0' or jian in '.': mouse.click(Button.left) #左键在按住 else: kaiguan = 0
from pynput.keyboard import Key, Controller from pynput.mouse import Controller import time keyboard = Controller() mouse = Controller() # Press and release space time.sleep(2) while True: # print('press') # keyboard.press(Key.space) # time.sleep(0.5) # keyboard.release(Key.space) print('move') mouse.move(-10, 10) time.sleep(0.1) # cannot work in ME