class Handler(object): def __init__(self): self.d = Controller() def keycode(self, name, is_dead): """Resolves a key description to a value that can be passed to :meth:`pynput.keyboard.Controller.press` and :meth:`~pynput.keyboard.Controller.release`. :param str name: The name of the key. This should typically be the actual character requested. If it starts with ``'<'`` and ends with ``'>'``, the key value is looked up in :class:`pynput.keyboard.Key`, otherwise it is passed straight to :meth:`pynput.keyboard.Controller.press`. :return: a key value """ if is_dead: return KeyCode.from_dead(name) elif name[0] == '<' and name[-1] == '>': return Key[name[1:-1]] else: return name def down(self, name, is_dead=False): """Triggers a key down event. :param str name: The name of the key. This is passed to :meth:`keycode`. :param bool is_dead: Whether a dead key press is requested. In that case, ``name`` must be a unicode character with a corresponding combining codepoint, and the key will be combined with the next character typed. """ self.d.press(self.keycode(name, is_dead)) def up(self, name, is_dead=False): """Triggers a key up event. :param str name: The name of the key. This is passed to :meth:`keycode`. :param bool is_dead: Whether a dead key press is requested. In that case, ``name`` must be a unicode character with a corresponding combining codepoint, and the key will be combined with the next character typed. """ self.d.release(self.keycode(name, is_dead))
class DustRacingRL: def __init__(self): self.state_queue, self.pid, self.win_ids = start_game() self.keyboard = Controller() self.action_space = [Action.UP, Action.LEFT, Action.RIGHT, Action.UP_LEFT, Action.UP_RIGHT] self.last_action = None self.car_mon = {'top': 114+57-42-70, 'left': 322+58-31-70, 'width': 540, 'height': 540} #self.resize_dim = (256,256) self.resize_dim = (128,128) #self.resize_dim = (84,84) self.input_shape = self.resize_dim self.sct = mss() def reset(self): self.focus_window() self.last_action = None self.clean_up() self.keyboard.press(Key.esc) self.keyboard.release(Key.esc) self.keyboard.press(Key.enter) state = None while state != 'START': try: state = self.state_queue.get_nowait() state = state.strip("\n") except: pass self.keyboard.release(Key.enter) return self.__observe() def clean_up(self): self.keyboard.release(Key.up) self.keyboard.release(Key.left) self.keyboard.release(Key.right) self.keyboard.release(Key.down) def __observe(self): frame = None car_box = self.sct.grab(self.car_mon) car_box = np.array(Image.frombytes('RGB', (car_box.width, car_box.height), car_box.bgra, "raw", "BGRX")) frame = cv2.cvtColor(cv2.resize(car_box, self.resize_dim), cv2.COLOR_BGR2GRAY) #normalization return frame/255.0 #zero mean #return frame-frame.mean() #normalization + zero mean """frame = frame/255.0 return frame-frame.mean()""" #standardization #return (frame-frame.mean())/frame.std() #edge detection """frame = cv2.Canny(frame, threshold1 = 200, threshold2 = 300) frame = frame/255.0 return frame""" def step(self, action): action = self.action_space[action] next_state = None reward = -0.03 #get reward when go forward if Key.up in action: #print('up') reward = 0.5 done = False info = None if self.last_action and self.last_action != action: for key in (self.last_action - action): self.keyboard.release(key) self.last_action = action for key in action: self.keyboard.press(key) next_state = self.__observe() try: msg = self.state_queue.get_nowait() # or q.get(timeout=.1) msg = msg.strip("\n") if msg == 'LOSE': reward = -1 done = True elif msg == 'FINISH_LAP': reward = +1 info = msg elif msg == 'FINISH_GAME': reward = +1 done = True info = msg except: pass if done: self.clean_up() return next_state, reward, done, info def focus_window(self): for win_id in self.win_ids: subprocess.run(["xdotool", "windowfocus", str(win_id)])
time.sleep(2) # Set pointer position mouse.position = (-30050, 200000) print('Now we have moved it to {0}'.format(mouse.position)) time.sleep(2) mouse.press(Button.left) mouse.release(Button.left) time.sleep(2) keyboard.press('c') keyboard.release('c') time.sleep(0.5) keyboard.press('o') keyboard.release('o') time.sleep(0.5) keyboard.press('m') keyboard.release('m') time.sleep(0.5) keyboard.press('m') keyboard.release('m') time.sleep(0.5) keyboard.press('a') keyboard.release('a') time.sleep(0.5) keyboard.press('n') keyboard.release('n')
class winControl: def __init__(self): self.mouseControl = ControllerMouse() self.keyboardControl = Controllerkeyboard() self.mouse = mouse self.Key = keyboard.Key self.threading = threading self.Listener = Listener self.oldCpText = "" self.wc = wc def move(self, x, y): self.mouseControl.position = (x, y) pass def updateFile(self, imgSrc='test.jpg', winTitle='打开'): imgSrc = parse.unquote(imgSrc) windowBox = win32gui.FindWindow(None, winTitle) if (windowBox > 0): btn = win32gui.FindWindowEx(windowBox, None, 'Button', None) childWin = win32gui.FindWindowEx(windowBox, None, 'ComboBoxEx32', None) #逐级寻找 childWin = win32gui.FindWindowEx(childWin, None, 'ComboBox', None) childWin = win32gui.FindWindowEx(childWin, None, 'Edit', None) if (childWin > 0): win32gui.SendMessage(childWin, win32con.WM_SETTEXT, None, imgSrc) win32gui.PostMessage(btn, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32gui.PostMessage(btn, win32con.WM_KEYUP, win32con.VK_RETURN, 0) self.keyboardControl.press(Key.enter) self.keyboardControl.release(Key.enter) return "ok" else: return 'found eroor! please contact author...' else: return 'file upload not open!' def scrollbar(self, y=0): self.mouseControl.scroll(0, y) # 为正向上滚 pass def click(self, x, y): self.move(x, y) self.mouseControl.press(Button.left) self.mouseControl.release(Button.left) def rightClick(self, x, y): self.move(x, y) self.mouseControl.press(Button.right) self.mouseControl.release(Button.right) def setValue(self, x, y, value): value = parse.unquote(value) self.click(x, y) self.allSelect() self.writePaste(value) self.paste() def allSelect(self): # 全选 self.keyboardControl.press(self.Key.ctrl) self.keyboardControl.press('a') self.keyboardControl.release(self.Key.ctrl) self.keyboardControl.release('a') def paste(self): self.keyboardControl.press(self.Key.ctrl_l) self.keyboardControl.press('v') self.keyboardControl.release(self.Key.ctrl_l) self.keyboardControl.release('v') def writePaste(self, value): self.wc.OpenClipboard() self.wc.EmptyClipboard() self.wc.SetClipboardData(win32con.CF_UNICODETEXT, value) self.wc.CloseClipboard() def closeAPP(self, x, y): self.click(x, y) self.keyboardControl.press(self.Key.ctrl) self.keyboardControl.press(self.Key.f4) self.keyboardControl.release(self.Key.ctrl) self.keyboardControl.release(self.Key.f4) def getPosition(self): return self.mouseControl.position
def shift_home_select(): keyboard = Controller() with keyboard.pressed(Key.shift): keyboard.press(Key.home) keyboard.release(Key.shift)
class GUI: def __init__(self): # 实例化键盘1对象 self.keyboard = Controller() # 实例化鼠标1对象 self.mose = Controller() # 程序运行位置 self.path = 'C:\Program Files (x86)\Tencent\QQ\Bin\QQScLauncher.exe' # 实例化键盘2对象 self.k = PyKeyboard() # self.m=PyMouse() def loginQQ(self, qq, pwd): # 打开QQ os.popen(self.path) time.sleep(1) # 获取QQ窗口 self.window = uiautomation.WindowControl(Name='QQ') time.sleep(1) # QQ输入框坐标 self.window.Click(187, 200) # 输入qq号 self.keyboard.type(qq) time.sleep(1) # 密码输入框坐标 # window.Click(187,240) # self.k.press_key(('BackSpace')) self.keyboard.press(Key.tab) # self.keyboard.release(Key.tab) time.sleep(1) # self.keyboard.type('19961214xiahuai') # 输入qq密码 self.keyboard.type(pwd) self.window.Click(200, 300) self.keyboard.release(Key.tab) def findqq(self, resuitqq): time.sleep(10) # 获取登录成功后的界面 window1 = uiautomation.WindowControl(name="QQ") # 定位搜素输入框,并点击 window1.Click(123, 110) # 粘贴输入框qq号码 self.keyboard.type(resuitqq) # self.keyboard.type('1461351451') time.sleep(0.5) # 进入聊天界面 self.keyboard.press(Key.enter) self.keyboard.release(Key.enter) def sendmesages(self, li): for i in range(1, 10): self.keyboard.type(random.choice(li)) self.keyboard.press(Key.enter) time.sleep(0.5) self.keyboard.release(Key.enter) def cellphone(self): pass def __del__(self): pass
from pynput.keyboard import Key, Controller from time import sleep k = Controller() k.type('set FLASK_ENV=development') k.press(Key.enter) k.release(Key.enter) k.type('set FLASK_APP=flask_app.py') k.press(Key.enter) k.release(Key.enter) k.type('flask run') k.press(Key.enter) k.release(Key.enter)
def stopSession(): # OK time.sleep(1) keyboard = Controller() keyboard.press(Key.space) keyboard.release(Key.space) os.remove("session.png")
# now extract the data from the image distance = ExtractTrainingData.screenshot.getDistance() gameData = ExtractTrainingData.screenshot.extractData() # now arrange the training data and make a prediction gameData = gameData.reshape((1, 130)) pedal = model.predict(gameData) # 0 is brake 1 is gas pedal = np.argmax(pedal) gameData = gameData.reshape((130)) # execute the prediction if (pedal == 0): # press brake (left arrow) print("brake") keys.press(Key.left) sleep(n) keys.release(Key.left) else: # press gas (right arrow) print("gas") keys.press(Key.right) sleep(n) keys.release(Key.right) # now wait n seconds to see what the car does ExtractTrainingData.screenshot.takeScreenshot() # check to see if the car has died or not moved forward dead = ExtractTrainingData.screenshot.death() newDistance = ExtractTrainingData.screenshot.getDistance() try: start = int(distance) stop = int(newDistance)
#this was created due to an agency that decided to give me a 5 minute typing speed test #I couldn't imagine it being possible to type for 5 minutes straight and maintaining my mental stability #Sooo.... i made a script to do it for me from pynput.keyboard import Controller import time #pynput is a library that gives python access you your keyboard inputs keyboard = Controller() #this assigns the Controller() to a variable called keyboard time.sleep(2) #this sleep variable would allow me ttime to put the cursor in the desired text field msg1="Man I hate typing test. Tis is the reason this fella was created. " #the designated text to be typed for char in msg1: keyboard.press(char) keyboard.release(char) #press and release the designated char on the keyboard time.sleep(0.08) #this sleep variable designates the typing speed
def send_key_event(keyval, primary=0, alt=0, shift=0, control=0, window=None, process_events="linux" in sys.platform, bypass_keymanager="linux" not in sys.platform): """Emit a key event on GPS, simulating the given key. This event is sent asynchronously. Unless process_events is true, this function will return when the event has not yet been processed by gtk+. keyval is generally the result of calling ord("x"). Sending letters to an editor doesn't seem to work at the moment, except for special characters like GDK_RETURN. If bypass_keymanager is True, do not use the Ada function which passes the event to the key manager, but synthesize the event in Python directly. """ if not bypass_keymanager: keycode = 0 # Try to retrieve the hardware keycode with the appropriate # Gtk.Keymap function. keymap = Gdk.Keymap.get_default() success, keys = keymap.get_entries_for_keyval(keyval) if success: keycode = keys[0].keycode if hasattr(GPS, "send_key_event"): GPS.send_key_event(keyval, window=window, primary=primary, control=control, alt=alt, shift=shift, hardware_keycode=int(keycode)) return else: keyboard = Controller() key = chr(keyval) if isinstance(keyval, int) else keyval if control: with keyboard.pressed(Key.ctrl): keyboard.press(key) keyboard.release(key) elif shift: with keyboard.pressed(Key.shift): keyboard.press(key) keyboard.release(key) elif alt: with keyboard.pressed(Key.alt): keyboard.press(key) keyboard.release(key) else: keyboard.press(key) keyboard.release(key) if process_events: process_all_events()
# timings = timings.split(",") timings = [ '0.68', '0.29', '0.39', '0.34', '0.27', '0.43', '0.79', '0.49', '0.49', '0.51', '0.95', '0.96', '0.54', '0.25', '0.92', '0.83', '0.34', '0.93', '0.93', '0.32', '0.49', '0.37', '0.16', '0.80', '0.79', '0.16', '0.92', '0.29', '0.35', '0.85', '0.34', '0.56', '0.23', '0.58', '0.85', '0.58', '0.64', '0.22', '0.94', '0.47', '0.22', '0.95', '0.35', '0.10', '0.23', '0.85', '0.11', '0.80', '0.14' ] timings = [float(a) for a in timings] keypress = timings[:len(timings) / 2 + 1] keyintervals = timings[len(timings) / 2 + 1:] # print len(password) # print len(keypress) # print(len(keyintervals)) sleep(5) for i in range(len(password)): keyboard.press(password[i]) sleep(keypress[i]) keyboard.release(password[i]) if (i != (len(password) - 1)): # print("ABBB",i) sleep(keyintervals[i]) # print("AAAA",i) keyboard.press(Key.enter) keyboard.release(Key.enter) tcflush(stdin, TCIFLUSH) # print(i) print("DONE")
from sys import argv from pynput.keyboard import Key, Controller from time import sleep keyboard = Controller() try: version = argv[1] except: version = "" sleep(3) keyboard.type("python" + version + " -c 'import pty; pty.spawn(\"/bin/bash\")'") keyboard.press(Key.enter) keyboard.release(Key.enter) sleep(0.5) keyboard.press(Key.ctrl) keyboard.press('z') keyboard.release(Key.ctrl) keyboard.release('z') sleep(0.5) keyboard.type("stty raw -echo") keyboard.press(Key.enter) keyboard.release(Key.enter) sleep(0.5) keyboard.type("fg") keyboard.press(Key.enter) keyboard.release(Key.enter) keyboard.press(Key.enter) keyboard.release(Key.enter) sleep(0.5) keyboard.type("export TERM=xterm-256color") keyboard.press(Key.enter)
class RSInput: def __init__(self): self._keyboard = KeyboardController() self._mouse = MouseController() def write(self, msg): self._keyboard.press(Key.enter) sleep(uniform(0.02, 0.08)) self._keyboard.release(Key.enter) for word in msg: for char in word: self._keyboard.press(char) sleep(uniform(0.02, 0.025)) self._keyboard.release(char) sleep(0.05) self._keyboard.press(Key.space) self._keyboard.release(Key.space) self._keyboard.press(Key.enter) self._keyboard.release(Key.enter) def click(self, x, y): self._mouse.position = (x, y) sleep(uniform(0.08, 0.152)) self._mouse.press(Button.left) sleep(uniform(0.04, 0.118)) self._mouse.release(Button.left) def tap_key(self, key): self._keyboard.press(key) sleep(uniform(0.08, 0.152)) self._keyboard.release(key) def tap_space(self): self._keyboard.press(Key.space) sleep(uniform(0.08, 0.152)) self._keyboard.release(Key.space)
class Applet: def __init__(self, login, password, fOUT): self.log = hashlib.sha256(login.encode()).hexdigest() self.paswd = hashlib.sha256(password.encode()).hexdigest() self.decr = ad(password, 100000) self.host = SOCK_SERV_ADDRES self.port = SOCK_SERV_PORT self.buf = 4096 self.addr = (self.host, self.port) self.kb = Controller() self.sock = socket() self.OUT = fOUT def on_click(x, y, button, pressed): print('BOI') if button == mouse.Button.left: return False def start(self): while True: try: self.sock.connect(self.addr) break except ConnectionRefusedError: self.OUT("ERROR", "Connection refused. Will try again in 5 seconds...") time.sleep(5) self.sock.send( pickle.dumps({ "login": self.log, "password": self.paswd, "hostname": gethostname() })) while self.sock: try: query = pickle.loads(self.sock.recv(self.buf)) except ConnectionResetError: self.sock.close() self.OUT("ERROR", 'Server shutdown. Closing app in 5 seconds...') time.sleep(5) os._exit(-4) except EOFError: self.sock.close() self.OUT( "ERROR", 'Wrong login or password. Closing app in 5 seconds...') time.sleep(5) os._exit(-8) self.OUT( "Notification", "Authentication data {} has arrived\nPress LMB for login autotype, and then LMB again for password autotype " .format(query['descr'])) Thread(self.autotype(query['log'], query['pas'])).start() def close(self): self.sock.close() os._exit(-7) def autotype(self, l, p): l, p = self.decr.decrypt(l), self.decr.decrypt(p) with mouse.Listener(on_click=self.on_click) as listener: try: listener.join() except: pass for x in l: self.kb.press(x) self.kb.release(x) time.sleep(0.5) with mouse.Listener(on_click=self.on_click) as listener: try: listener.join() except: pass for x in p: self.kb.press(x) self.kb.release(x)
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 from pynput.keyboard import Key, Controller keyboard = Controller() import time def check(): file = open("ripcord.txt", "r") file.close() keyboard.type("[") keyboard.type("]") keyboard.press(Key.left) keyboard.release(Key.left) time.sleep(.1) check()
SERIAL_PORT = PORT.upper() SERIAL_RATE = 9600 Byte_list = [] ser = serial.Serial(SERIAL_PORT, SERIAL_RATE) print("Keep The Console Minimised Do Not Close") if __name__ == "__main__": while True: while True: rx_data = ser.read() if (rx_data == b'\r'): break else: Byte_list.append(str(rx_data)) byteAppenedString=''.join(Byte_list) dataString = byteAppenedString.replace('b','').replace("'","") print("Barcode Scanned: ", dataString) key = Controller() #time.sleep(5) for char in dataString: key.press(char) key.release(char) Byte_list = []
if liste[0] == "Der": print(liste) toggleSlowchat() if liste[0][0] == "[" and liste[ 3] == "->" and "STOPGRIEFERBOT" in liste: # 2 für Verletzer der Lizenz killed = int("ert") if liste[0][0] == "[" and liste[ 3] == "->" and "APFELKUCHEN" in liste: print("getötet") message = "/pay LocutusV0nB0rg 10000" keyboard.press('t') time.sleep(0.1) keyboard.release('t') keyboard.type(message) time.sleep(0.2) keyboard.press(KeyCode.from_vk(VK.RETURN)) time.sleep(0.1) keyboard.release(KeyCode.from_vk(VK.RETURN)) if liste[0] == "Kontostand:": print(liste) geldstring = liste[1] KONTOSTAND = int(geldstring[1:-1]) if zahlvorgang: if SOLLKONTO == KONTOSTAND: templiste = vorgangsliste
def enter_input(): if PYINPUT: keyboard = Controller() keyboard.press(Key.enter) keyboard.release(Key.enter)
sleep(TIME) keyboard.type(name) sleep(TIME) keyboard.tap(Key.enter) # wait a bit and anchor it as a character sleep(TIME) press_multiple([Key.ctrl, "w"]) # wait a bit and advance to the next line sleep(TIME) for i in [Key.esc, Key.left, Key.enter]: # maybe Key.down keyboard.tap(i) sleep(0.15) keyboard.press(Key.page_down) sleep(TIME) keyboard.release(Key.page_down) #export as pdf sleep(TIME) keyboard.tap(Key.f4) sleep(0.1) keyboard.tap(Key.enter) sleep(1) keyboard.tap(Key.backspace) sleep(TIME) keyboard.type(f"\"{directory}\{exported}.pdf\"") keyboard.tap(Key.enter) keyboard.tap(Key.enter)
class Tastatur(Thread): def __init__(self): super().__init__(daemon=True) # Speichert zu jeder Taste die Zeit bis zu der sie gedrueckt bleiben soll self._gedrueckte_tasten = {} self.keyboard = Controller() self.ist_ausgabe_aktiv = False def run(self): while True: with tastaturLock: self._aktualisiere_tasten() time.sleep(SLEEP_TIME) def _aktualisiere_tasten(self): if not self.ist_ausgabe_aktiv: return tasten = list(self._gedrueckte_tasten.keys()) for taste in tasten: betroffene_taste = self._gedrueckte_tasten[taste] if betroffene_taste.soll_geloescht_werden: if betroffene_taste.ist_gedrueckt: self.keyboard.release(key=betroffene_taste.taste) del self._gedrueckte_tasten[taste] elif betroffene_taste.soll_gedrueckt_werden: betroffene_taste.ist_gedrueckt = True self.keyboard.press(key=betroffene_taste.taste) elif betroffene_taste.soll_losgelassen_werden: betroffene_taste.ist_gedrueckt = False self.keyboard.release(key=betroffene_taste.taste) def druecke_taste(self, taste: Taste, dauer_in_sekunden: float = float('inf'), prozent_aktiv: float = 1.0) -> None: neue_taste = GedrueckteTaste(taste=taste, dauer_in_sekunden=dauer_in_sekunden, prozent_aktiv=prozent_aktiv) with tastaturLock: if taste in self._gedrueckte_tasten: self._gedrueckte_tasten[taste].kombiniere_mit(neue_taste) else: self._gedrueckte_tasten[taste] = neue_taste def lasse_tasten_los(self, tasten: List[Taste]) -> None: for taste in tasten: self.lasse_taste_los(taste) def lasse_taste_los(self, taste: Taste) -> None: with tastaturLock: if taste in self._gedrueckte_tasten: betroffene_taste = self._gedrueckte_tasten[taste] if betroffene_taste.ist_gedrueckt: self.keyboard.release(key=betroffene_taste.taste) del self._gedrueckte_tasten[taste] def deaktiviere_ausgabe(self): with tastaturLock: self.ist_ausgabe_aktiv = False for taste in self._gedrueckte_tasten: betroffene_taste = self._gedrueckte_tasten[taste] if betroffene_taste.ist_gedrueckt: betroffene_taste.ist_gedrueckt = False self.keyboard.release(key=betroffene_taste.taste) def aktiviere_ausgabe(self): with tastaturLock: self.ist_ausgabe_aktiv = True
class Interface(Tk): def __init__(self, config_file): # READ CONFIG FILE self.config_file = config_file self.config = configparser.ConfigParser() self.config.read(self.config_file) # WINDOW PARAMS self.window_name = "Feedback" self.img_h = self.config.getint("window", "img_h") self.img_v = self.config.getint("window", "img_v") self.refresh_rate = self.config.getint("window", "refresh_rate") self.win_posX = self.config.getint("window", "posX") self.win_posY = self.config.getint("window", "posY") # OPTIONS PARAMS self.hand2use = self.config.get("options", "hand2use") self.labels_buttons = ast.literal_eval( self.config.get("options", "labels_buttons")) self.use_setup = self.config.getboolean( "options", "use_setup") # False only for checking script self.close_cmd = self.config.getboolean("options", "close_cmd") self.use_sound = self.config.getboolean("options", "use_sound") self.show_visualizer = self.config.getboolean("options", "show_visualizer") self.record_data = self.config.getboolean("options", "record_data") self.path_leap_folder = ast.literal_eval( self.config.get("options", "path_leap_folder")) # DRAWING PARAMS self.circle_radius = self.config.getint("drawing", "circle_radius") self.circle_color = ast.literal_eval( self.config.get("drawing", "circle_color")) self.circle_thickness = self.config.getint("drawing", "circle_thickness") self.circle_first_coord = self.config.getint("drawing", "circle_first_coord") self.circle_space = self.config.getint("drawing", "circle_space") self.text_color = ast.literal_eval( self.config.get("drawing", "text_color")) self.text_color_selec = ast.literal_eval( self.config.get("drawing", "text_color_selec")) self.bar_color = ast.literal_eval( self.config.get("drawing", "bar_color")) self.bar_color_th = ast.literal_eval( self.config.get("drawing", "bar_color_th")) self.bar_origin_threshold = ast.literal_eval( self.config.get("drawing", "bar_origin_threshold")) # DATA RECORDING if self.record_data: self.folderData = ".\Data" if not os.path.exists(self.folderData): os.makedirs(self.folderData) timestr = time.strftime("%Y%m%d-%H%M%S") self.file_object = open( os.path.join(self.folderData, timestr + '.txt'), "w+") if self.hand2use == 'left': self.file_object.write(DEFAULT_DATA_HEADER_LEFT + "\n") else: self.file_object.write(DEFAULT_DATA_HEADER_RIGHT + "\n") # SOUND FEEDBACK mixer.init() self.sound_path = r'.\Sound' if not os.path.exists(self.sound_path) and self.use_sound: self.loading_sound_on_computer(OPTIONS) # CLOSE TERMINAL hwnd = ctypes.windll.kernel32.GetConsoleWindow() if hwnd != 0 and self.close_cmd: ctypes.windll.user32.ShowWindow(hwnd, 0) ctypes.windll.kernel32.CloseHandle(hwnd) _, pid = win32process.GetWindowThreadProcessId(hwnd) os.system('taskkill /PID ' + str(pid) + ' /f') # READING KEYBOARD self.keyboard = Controller() # HAND USE self.labels_fingers = self.define_labels_fingers_based_onhand( DEFAULT_LABELS_FINGERS) # [LEAP MOTION] INITIALIZATION if self.show_visualizer: self.move_leap_motion_visualizer() self.listener, self.controller = self.initialize_leap_motion() self.distance = self.read_values_from_devices(self.listener, self.controller) # [INTERFACE] - DRAWING PARAMETERS self.circle_y = int(self.img_h / 2) + 10 self.center_x = int(self.img_h / 2) self.bar_max = int(self.img_h / 3) self.bar_h = np.zeros((4)) self.decisionPressButton = [False, False, False, False] self.last_decisionPressButton = self.decisionPressButton[:] self.distance_rest = [None, None, None, None] self.distance_rest_default = [100.0, 100.0, 100.0, 100.0] self.distance_rest = self.distance_rest_default[:] # [INTERFACE] - PARAMS INITIALIZATION self.nButton = len(self.labels_buttons) self.calibrated = False self.slider_value = self.bar_origin_threshold[:] print(self.slider_value) self.threshold = self.bar_origin_threshold[:] self.slider_name = [x.lower() for x in self.labels_fingers] if self.hand2use == 'left': self.slider_name = self.slider_name[::-1] self.key = 0 self.solution2Use = 0 self.firstTime = False # BUTTON INTERFACE - TKINTER Tk.__init__(self) self.title('Hand2keyPressedRehab') self.geometry('650x900+10+600') # self.geometry('450x600+10+10') self.configure(background="Sky Blue") self.resizable(1, 1) path_icon = os.path.join(r'.\Icon', 'HackaHealth_Logo_tkinter.ico') self.wm_iconbitmap(path_icon) self.top_frame = Frame(self, bg='Sky Blue', width=650, height=500, padx=10, pady=10) btm_frame2 = Frame(self, bg='Sky Blue', width=650, height=100) btm_frame3 = Frame(self, bg='Sky Blue', width=650, height=200) btm_frame4 = Frame(self, bg='Sky Blue', width=650, height=100) # layout all of the main containers self.grid_rowconfigure(1, weight=1) self.grid_columnconfigure(0, weight=1) self.top_frame.grid(row=0, sticky="nsew") btm_frame2.grid(row=2, sticky="nsew") btm_frame3.grid(row=1, sticky="nsew") btm_frame4.grid(row=4, sticky="nsew") self.v1 = Label(self.top_frame, text="Leap Motion not detected", font=("Helvetica", 11), padx=100) #self.v1 = Label(top_frame) self.v1.grid(padx=10) #Label(self, text='Please select buttons to use', bg='#aaa').grid(row=0,columnspan=3) if self.hand2use == 'right': self.on_button_list = [ self.on_button3, self.on_button2, self.on_button1, self.on_button ] self.updata_value_list = [ self.updata_value, self.updata_value1, self.updata_value2, self.updata_value3 ] elif self.hand2use == 'left': self.on_button_list = [ self.on_button, self.on_button1, self.on_button2, self.on_button3 ] self.updata_value_list = [ self.updata_value3, self.updata_value2, self.updata_value1, self.updata_value ] # create the sliders for i in range(4): l2 = Label(btm_frame2, bg='Sky Blue', text=self.slider_name[i].lower(), font=("Helvetica", 11)) w2 = Scale(btm_frame2, bg='Sky Blue', from_=0, to=100, orient=HORIZONTAL, length=450, command=self.updata_value_list[i]) w2.set(self.slider_value[i]) l2.grid(row=i, column=0, padx=10) w2.grid(row=i, column=1, padx=50) # create the listmenu o_vars = [] self.o_vars = [] self.o = [None, None, None, None] for i in range(4): var = StringVar(value=self.labels_fingers[i]) o_vars.append(var) self.o[i] = OptionMenu(btm_frame3, var, *OPTIONS, command=self.on_button_list[i]) self.o[i].grid(row=0, column=i) self.var1 = BooleanVar() self.var1.set(self.use_sound) check_button = Checkbutton(btm_frame4, bg='Sky Blue', text="Sound", variable=self.var1, command=self.check_sound_use).grid(row=0, column=0, padx=10) self.var2 = BooleanVar() self.var2.set(self.use_setup) check_button = Checkbutton(btm_frame4, bg='Sky Blue', text="Press Button", variable=self.var2, command=self.check_setup_use).grid(row=0, column=1, padx=80) self.var3 = BooleanVar() self.var3.set(self.show_visualizer) check_button = Checkbutton(btm_frame4, bg='Sky Blue', text="Visualizer", variable=self.var3, command=self.check_vis_use).grid(row=0, column=2, padx=10) #var2 = IntVar() #Checkbutton(btm_frame4, text="Button", variable=var2).grid(row=0,column = 2) #Button(btm_frame4, text='Calibration',height=2,width=30).grid(row=0,padx=60) self._thread = None self._thread = threading.Thread(target=self.run_loop) self._thread.start() self.protocol("WM_DELETE_WINDOW", self.on_close) # RUN MAIN LOOP self.mainloop() def check_sound_use(self): self.use_sound = self.var1.get() def check_setup_use(self): self.use_setup = self.var2.get() def check_vis_use(self): self.show_visualizer = self.var3.get() if self.show_visualizer: self.move_leap_motion_visualizer() else: print("close visualization") subprocess.call(["taskkill", "/F", "/IM", 'Visualizer.exe']) # subprocess.Popen(['C:\\Program Files (x86)\\Leap Motion\\Core Services\\Visualizer.exe', '-new-tab']) def updata_value(self, selection): self.slider_value[0] = int(selection) def updata_value1(self, selection): self.slider_value[1] = int(selection) def updata_value2(self, selection): self.slider_value[2] = int(selection) def updata_value3(self, selection): self.slider_value[3] = int(selection) def on_button(self, selection): self.labels_buttons[0] = selection def on_button1(self, selection): self.labels_buttons[1] = selection def on_button2(self, selection): self.labels_buttons[2] = selection def on_button3(self, selection): self.labels_buttons[3] = selection # def exitui(self): def on_close(self): # stop the drawing thread. print("**** Exit interface ****") if self.keyboard.shift_pressed: self.keyboard.release(Key.shift_l) print("release shift") if self.keyboard.alt_pressed: self.keyboard.release(Key.alt_l) print("release alt") if self.keyboard.ctrl_pressed: self.keyboard.release(Key.ctrl_l) print("release ctrl") self.controller.remove_listener(self.listener) subprocess.call(["taskkill", "/F", "/IM", 'Visualizer.exe']) self.file_object.close() self._thread = None self.update_config_file() mixer.quit() sys.exit() self.destroy() def loading_sound_on_computer(self, OPTIONS): engine = pyttsx3.init(driverName='sapi5') print("*** Creating Sound folder ****") folderData = ".\Sound" if not os.path.exists(folderData): os.makedirs(folderData) for i, opt in enumerate(OPTIONS): print(opt) theText = opt tts = gTTS(text=theText, lang='en') tts.save(os.path.join(folderData, theText + ".mp3")) print("File saved!") def write_in_text_file(self, filehandle, distance, threshold): filehandle.write(",".join( str(item) for item in np.concatenate((distance, threshold))) + "\n") def define_labels_fingers_based_onhand(self, DEFAULT_LABELS_FINGERS): if self.hand2use == 'right': print("R") labels_fingers = DEFAULT_LABELS_FINGERS elif self.hand2use == 'left': print("L") labels_fingers = DEFAULT_LABELS_FINGERS[::-1] print(labels_fingers) return labels_fingers def press_key_on_keyboard(self, keyboard, decisionPressButton, last_decisionPressButton, labels_buttons): """ Function to Emulate keyboard input: decisionPressButton: array of pinching detection for the different fingers labels_buttons: array of labels to press when detection """ for index, (n_choice, o_choice) in enumerate( zip(decisionPressButton, last_decisionPressButton)): fn_button = DICT_PYNPUT_KEYBOARD[labels_buttons[index].lower()] if fn_button is not None: if n_choice and not o_choice: if self.use_setup: keyboard.press(fn_button) print(labels_buttons[index] + ' pressed') if self.use_sound: mixer.music.load( os.path.join(self.sound_path, labels_buttons[index] + '.mp3')) mixer.music.play() elif not n_choice and o_choice: if self.use_setup: keyboard.release(fn_button) print(labels_buttons[index] + ' released') def move_leap_motion_visualizer(self): subprocess.Popen([self.path_leap_folder, '-new-tab']) time.sleep(0.1) # # [LEAP MOTION VISUALIZER] - moving window user32 = ctypes.windll.user32 # get screen resolution of primary monitor res = (user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)) # res is (2293, 960) for 3440x1440 display at 150% scaling user32.SetProcessDPIAware() res = (user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)) # res is now (3440, 1440) for 3440x1440 display at 150% scaling handle = user32.FindWindowW(None, u'Leap Motion Diagnostic Visualizer') user32.ShowWindow(handle, 6) user32.ShowWindow(handle, 9) user32.ShowWindow(handle, 1) user32.MoveWindow(handle, 5, 0, 705, 620, True) print("Moving Leap Motion Visualizer on predefined position") for i in range(3): self.keyboard.press('v') self.keyboard.release('v') for i in range(5): self.keyboard.press(KeyCode.from_char('=')) self.keyboard.release(KeyCode.from_char('=')) self.keyboard.press('c') self.keyboard.release('c') self.keyboard.press('g') self.keyboard.release('g') # self.keyboard.press(KeyCode.from_char('=')) # self.keyboard.release(KeyCode.from_char('=')) # self.keyboard.press(KeyCode.from_char('=')) # self.keyboard.release(KeyCode.from_char('=')) # # [LEAP MOTION] - initiation def initialize_leap_motion(self): try: listener = LeapMotionListener() controller = Leap.Controller() controller.add_listener(listener) except: print("[ERROR] Leap Motion Devices is not connected") pass return listener, controller def read_values_from_devices(self, listener, controller): distance = listener.on_frame(controller) return distance def loading_pic_from_folder(self, pathFolderPicture, dim_pic): """NOT USED in this version""" pic_img = [] for file in os.listdir(pathFolderPicture): if file.endswith(".png"): file_path = os.path.join(pathFolderPicture, file) print(file_path) f = cv2.imread(file_path) f = cv2.resize(f, dim_pic) pic_img.append(f) return pic_img def update_config_file(self): data = "[" + str(self.slider_value[0]) + "," + str( self.slider_value[1]) + "," + str( self.slider_value[2]) + "," + str(self.slider_value[3]) + "]" self.config.set("drawing", "bar_origin_threshold", data) data = "['" + str(self.labels_buttons[0]) + "','" + str( self.labels_buttons[1]) + "','" + str( self.labels_buttons[2]) + "','" + str( self.labels_buttons[3]) + "']" self.config.set("options", "labels_buttons", data) with open(self.config_file + '.bak', 'w') as configfile: self.config.write(configfile) if os.path.exists(self.config_file): os.remove( self.config_file) # else rename won't work when target exists os.rename(self.config_file + '.bak', self.config_file) print("[CONFIG_FILE] Ini file has been updated") def run_loop(self): print("**** Start Running interface ****") img = np.zeros((self.img_h, self.img_v, 3), np.uint8) counter_no_data = 0 while self._thread is not None: self.key = cv2.waitKeyEx(1) if self.hand2use == 'right': self.distance_hand = self.distance[:] elif self.hand2use == 'left': self.distance_hand = self.distance[::-1] if None in self.distance_hand: self.calibrated = False counter_no_data += 1 if counter_no_data == 1: print("Waiting for values from LeapMotion") elif counter_no_data == 4: img *= 0 cv2.putText(img, "Waiting for LeapMotion ...", (self.circle_first_coord + 20, self.circle_y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255)) img_ = Image.fromarray(img) nimg = ImageTk.PhotoImage(image=img_) self.v1.n_img = nimg self.v1.configure(image=nimg) continue else: counter_no_data = 0 # if (not self.calibrated and None not in self.distance_hand): # # if s2 == 1: # # self.distance_rest = self.distance_hand[:] # # else: # self.distance_rest = self.distance_rest_default[:] # print(self.distance_rest) # self.calibrated = True # USE PHASE for i in range(self.nButton): # Labelling coord = self.circle_first_coord + i * self.circle_space cv2.putText(img, self.labels_fingers[i], (coord - 30, self.circle_y - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, self.text_color) #Bar Plot Visualization - continuous self.bar_h[ i] = 1 - self.distance_hand[i] / self.distance_rest[i] self.threshold[i] = (self.slider_value[i] / 100.0) cv2.rectangle(img, (coord - 30, self.img_h), (coord + 30, self.img_h - int(self.bar_h[i] * self.bar_max)), self.bar_color, -1) cv2.line(img, (coord - 20, self.img_h - int(self.threshold[i] * self.bar_max)), (coord + 20, self.img_h - int(self.threshold[i] * self.bar_max)), self.bar_color_th, 3) # Boolean Visualization - discrete if self.bar_h[i] >= self.threshold[i]: cv2.circle(img, (coord, self.circle_y), self.circle_radius, self.text_color_selec, self.circle_thickness) self.decisionPressButton[i] = True cv2.putText(img, self.labels_buttons[i], (coord - 30, self.circle_y - 150), cv2.FONT_HERSHEY_SIMPLEX, 1, self.text_color_selec) cv2.rectangle(img, (coord - 30 - 30, self.circle_y - 150 - 50), (coord - 40 + 70, self.circle_y - 150 + 50), self.text_color_selec, 3) else: self.decisionPressButton[i] = False cv2.putText(img, self.labels_buttons[i], (coord - 30, self.circle_y - 150), cv2.FONT_HERSHEY_SIMPLEX, 1, self.text_color_selec) cv2.circle(img, (coord, self.circle_y), self.circle_radius, self.circle_color, self.circle_thickness) # Keyboard Pressing/Release Process self.press_key_on_keyboard(self.keyboard, self.decisionPressButton, self.last_decisionPressButton, self.labels_buttons) self.last_decisionPressButton = self.decisionPressButton[:] if self.record_data: # print(self.bar_h,self.threshold) self.write_in_text_file(self.file_object, self.bar_h, self.threshold) img_ = Image.fromarray(img) nimg = ImageTk.PhotoImage(image=img_) self.v1.n_img = nimg self.v1.configure(image=nimg) self.after(self.refresh_rate) img *= 0
from selenium import webdriver from pynput.keyboard import Key, Controller MY_CHROME_DRIVER_PATH = 'C:\Program Files\chromedriver.exe' driverPath = input( 'Enter your Chrome driver PATH. Leave empty if its: C:\Program Files\chromedriver.exe' ) if driverPath == '': path = MY_CHROME_DRIVER_PATH else: path = driverPath keyboard = Controller() driver = webdriver.Chrome(path) driver.get('https://monkeytype.com/') words = driver.find_elements_by_class_name("word") print('starting!') for word in words: keyboard.type(word.text) # Press space keyboard.press(Key.space) keyboard.release(Key.space) print('done!')
from pynput.keyboard import Key, Controller from time import sleep keyboard = Controller() keyboard.press(Key.alt) keyboard.press(Key.tab) keyboard.release(Key.tab) keyboard.release(Key.alt) sleep(0.50) for i in range(7): keyboard.press(Key.tab) keyboard.release(Key.tab) sleep(0.050) for i in range(2): keyboard.press(Key.tab) keyboard.release(Key.tab) sleep(0.50) keyboard.press(Key.right) keyboard.release(Key.right) keyboard.press(Key.left) keyboard.release(Key.left) for i in range(1): keyboard.press(Key.tab) keyboard.release(Key.tab) sleep(0.050) keyboard.type('a')
class AutoClickerThread(Thread): def __init__(self, parent_ui=None, config=Config()): super(AutoClickerThread, self).__init__() self.ui = parent_ui self.config = config self.thread = None self._stop_event = Event() self.mouse_button = None self.current_keys = set() self.accepted_keys = set() self.reload_config() self.activated = False self.mouse_controller = MouseController() self.keyboard_controller = KeyboardController() self.last_state = False self.sequence_index = 0 self.sequence_length = len(self.config.output_sequence) def stop(self): self._stop_event.set() def stopped(self): return self._stop_event.is_set() def run(self): self.thread.start() while not self.stopped(): if self.activated: if self.config.output_type == 0: # Mouse output to_press = Button.right if self.config.mouse_output == 1 else Button.left self.mouse_controller.press(to_press) self.mouse_controller.release(to_press) elif self.config.output_type == 1 and self.config.output_sequence: # Keyboard output current_key = self.config.output_sequence[self.sequence_index] to_press = KeyCode.from_char(current_key) self.keyboard_controller.press(to_press) time.sleep(self.config.hold_time) self.keyboard_controller.release(to_press) self.sequence_index = (self.sequence_index + 1) % self.sequence_length if self.config.distribution_type == 0: time.sleep(self.config.wait_time + random.uniform(0, self.config.deviation_time)) else: time.sleep(abs(np.random.normal(loc=self.config.wait_time, scale=self.config.deviation_time))) self.last_state = self.activated self.thread.stop() def set_activated(self, value): if self.ui: self.ui.update_clicking(value) self.activated = value def on_press(self, key): if key in self.accepted_keys: self.current_keys.add(key) if self.accepted_keys.issubset(self.current_keys): if self.config.toggle: self.set_activated(not self.activated) else: self.set_activated(True) def on_click(self, x, y, button, pressed): if button == self.mouse_button: if self.config.toggle and pressed: self.set_activated(not self.activated) elif not self.config.toggle and pressed: self.set_activated(True) elif not self.config.toggle and not pressed: self.set_activated(False) def on_release(self, key): try: if key in self.accepted_keys: if not self.config.toggle: self.set_activated(False) self.current_keys.remove(key) except KeyError: pass def set_config(self, config): self.config = config self.reload_config() def reload_config(self): self.accepted_keys = set() if self.config.alt_modifier: self.accepted_keys.add(Key.alt_l) for key in self.config.key_combination: self.accepted_keys.add(KeyCode.from_char(key)) self.thread = {0: KeyboardListener(on_press=self.on_press, on_release=self.on_release), 1: MouseListener(on_click=self.on_click)}.get(self.config.input_mode) self.mouse_button = {0: Button.x1, 1: Button.x2}.get(self.config.special_mouse_press) self.sequence_length = len(self.config.output_sequence)
class Menu(tk.Menu): def __init__(self, parent, controller): """this function creates the menu for the text editor""" #create a main menu tk.Menu.__init__(self, parent) #store the text widget in a variable to access the frame attributs for future modifications self.textWidget = controller #initialise a keyboard controller to handle (undo, redo, cut, paste, copy ...) self.keyboard = Controller() #add the file sub-menu self.filemenu = tk.Menu(self, tearoff=0) self.filemenu.add_command(label="New File", command=lambda: self.newFile()) self.filemenu.add_command(label="Open...", command=lambda: self.openFile()) self.filemenu.add_separator() self.filemenu.add_command(label="Save", command=lambda: self.saveFile()) self.filemenu.add_separator() self.filemenu.add_command(label="Exit", command=lambda: parent.destroy()) self.add_cascade(label="File", menu=self.filemenu) #add the Edit sub-menu self.editmenu = tk.Menu(self, tearoff=0) self.editmenu.add_command(label="Undo", command=lambda: self.undo()) self.editmenu.add_command(label="Redo", command=lambda: self.redo()) self.editmenu.add_separator() self.editmenu.add_command(label="Cut", command=lambda: self.cut()) self.editmenu.add_command(label="Copy", command=lambda: self.copy()) self.editmenu.add_command(label="Paste", command=lambda: self.paste()) self.editmenu.add_separator() self.editmenu.add_command(label="Find", command=lambda: self.find()) self.add_cascade(label="Edit", menu=self.editmenu) #add the configuration sub-menu self.configmenu = tk.Menu(self, tearoff=0) self.autoGuess = tk.StringVar() self.configmenu.add_checkbutton( label="Enable Guessing Mode", variable=self.autoGuess, onvalue="Enabled", offvalue="Disabled", command=lambda: self.config_autoGuess()) self.programming = tk.StringVar() self.configmenu.add_checkbutton( label="Enable Programming Mode", variable=self.programming, onvalue="Enabled", offvalue="Disabled", command=lambda: self.config_programming()) self.add_cascade(label="Configuration", menu=self.configmenu) #add the graphics sub-menu self.graphicmenu = tk.Menu(self, tearoff=0) ###adding colors' choice (black, red, blue, green ...) colors = tk.Menu(self.graphicmenu, tearoff=0) self.color = tk.StringVar() for element in COLORS: colors.add_radiobutton(label=element.upper(), background=element.lower(), foreground="white", variable=self.color, value=element, command=lambda: self.color_choice()) self.graphicmenu.add_cascade(label="Color", menu=colors) ###adding fonts' choice (Arial, comic sans ms, verdana ...) fonts = tk.Menu(self.graphicmenu, tearoff=0) self.font = tk.StringVar() for element in FONTS: fonts.add_radiobutton(label=element, variable=self.font, value=element, command=lambda: self.font_choice()) self.graphicmenu.add_cascade(label="Font", menu=fonts) ###adding fontSizes' choice (Small, medium, Large) fontSizes = tk.Menu(self.graphicmenu, tearoff=0) self.fontSize = tk.StringVar() for element in list(FONT_SIZES.keys()): fontSizes.add_radiobutton(label=element.upper(), variable=self.fontSize, value=FONT_SIZES[element], command=lambda: self.fontSize_choice()) self.graphicmenu.add_cascade(label="Font Size", menu=fontSizes) ###adding fontStyles' choice ( Bold, Italian) fontStyles = tk.Menu(self.graphicmenu, tearoff=0) self.bold = tk.StringVar() fontStyles.add_checkbutton(label="BOLD", variable=self.bold, onvalue="Enabled", offvalue="Disabled", command=lambda: self.Bold()) self.italian = tk.StringVar() fontStyles.add_checkbutton(label="ITALIC", variable=self.italian, onvalue="Enabled", offvalue="Disabled", command=lambda: self.Italic()) self.graphicmenu.add_cascade(label="Font Styles", menu=fontStyles) self.add_cascade(label="Graphics", menu=self.graphicmenu) #add the help sub-menu self.helpmenu = tk.Menu(self, tearoff=0) message = "This is an editor made by Maher OUALI" self.helpmenu.add_command( label="About", command=lambda: mb.showinfo("About!", message)) self.add_cascade(label="Help", menu=self.helpmenu) def newFile(self): #finished """this function generates a new file""" if (not (self.textWidget.lastIsSaved)): #here before saving launch a dialog file to see if the user want to save the file or not self.saveFile() #here we have to destroy the first frame and launch a second one self.textWidget.pathOfSavedVersion = None self.textWidget.lastIsSaved = True self.textWidget.text.delete("1.0", "end") self.textWidget.initializeElements() def openFile(self): #finished """this function opens an existing file chosen by the user""" if (not (self.textWidget.lastIsSaved)): #here before saving launch a dialog file to see if the user want to save the file or not answer = mb.askokcancel("Save or not", "Do you want to save this file?") if answer: self.saveFile() #here we have to destroy the first frame and launch a second one file = fd.askopenfile() if file is None: # asksaveasfile return `None` if dialog closed with "cancel". return self.textWidget.pathOfSavedVersion = file.name self.textWidget.lastIsSaved = True text2open = file.read() file.close() self.textWidget.text.delete("1.0", "end") self.textWidget.text.insert("end", text2open) self.textWidget.text.config(font=DEFAULT_FONT, fg=DEFAULT_COLOR) self.textWidget.font = DEFAULT_FONT self.textWidget.color = DEFAULT_COLOR def saveFile(self): #finished """this function saves the file edited by the user""" if (not (self.textWidget.lastIsSaved)): if (self.textWidget.pathOfSavedVersion == None): #here launch a file dialog to choose a path and then save it there and update both self.pathOfSavedVersion and self.lastIsSaved file = fd.asksaveasfile(mode='w', defaultextension=".txt") if file is None: # asksaveasfile return `None` if dialog closed with "cancel". return self.textWidget.pathOfSavedVersion = file.name self.textWidget.lastIsSaved = True text2save = str(self.textWidget.text.get( "1.0", "end")) # starts from `1.0`, not `0.0` file.write(text2save) file.close() else: try: file = open(self.textWidget.pathOfSavedVersion, mode='w') text2save = str(self.textWidget.text.get( "1.0", "end")) # starts from `1.0`, not `0.0` file.write(text2save) file.close() except: mb.showinfo("Error!!", "Original file not found") self.textWidget.pathOfSavedVersion = None self.saveFile() else: return def undo(self): #finished """this function mimic the undo of the text widget""" self.keyboard.press(Key.ctrl) self.keyboard.press("z") self.keyboard.release(Key.ctrl) self.keyboard.release("z") def redo(self): #finished """this function mimic the redo of the text widget""" self.keyboard.press(Key.ctrl) self.keyboard.press("y") self.keyboard.release(Key.ctrl) self.keyboard.release("y") def cut(self): #finished """this function mimic the cut of the text widget""" self.keyboard.press(Key.ctrl) self.keyboard.press("x") self.keyboard.release(Key.ctrl) self.keyboard.release("x") def copy(self): #finished """this function mimic the copy of the text widget""" self.keyboard.press(Key.ctrl) self.keyboard.press("c") self.keyboard.release(Key.ctrl) self.keyboard.release("c") def paste(self): #finished """this function mimic the paste of the text widget""" self.keyboard.press(Key.ctrl) self.keyboard.press("v") self.keyboard.release(Key.ctrl) self.keyboard.release("v") def find(self): #finished """this function gives the user the posibility of finding a word in the text widget""" #here we simply launch a dialog for the user to type the word that he wants to look for and then we highlight it in all possible positions in the text widget if (not (self.textWidget.findMethodTag == None)): self.textWidget.text.tag_delete(self.textWidget.findMethodTag) word = sd.askstring("Search", "What do you want to search for?") if word is not None: #we look for the word in the text file and we highlight all occurennces self.textWidget.findMethodTag = word text = self.textWidget.text.get("1.0", "end") listOfOccurrence = [(m.start(), m.end()) for m in re.finditer(word, text)] for element in listOfOccurrence: self.textWidget.text.tag_add(word, "1.0+" + str(element[0]) + "c", "1.0+" + str(element[1]) + "c") self.textWidget.text.tag_config( word, background=TAGS_COLORS[self.textWidget.color]) else: return def config_autoGuess(self): #finished if (self.autoGuess.get() == "Enabled"): self.textWidget.autoGuess = True else: self.textWidget.autoGuess = False def config_programming(self): #finished if (self.programming.get() == "Enabled"): self.textWidget.programming = True else: self.textWidget.programming = False def color_choice(self): #finished self.textWidget.color = self.color.get() self.textWidget.text.config(fg=self.textWidget.color) def font_choice(self): #finished temp_list = list(self.textWidget.font) temp_list[0] = self.font.get() self.textWidget.font = tuple(temp_list) self.textWidget.text.config(font=self.textWidget.font) def fontSize_choice(self): #finished temp_list = list(self.textWidget.font) temp_list[1] = int(self.fontSize.get()) self.textWidget.font = tuple(temp_list) self.textWidget.text.config(font=self.textWidget.font) def Bold(self): #finished temp_list = list(self.textWidget.font) if (self.bold.get() == "Enabled"): if (len(temp_list) == 2): temp_list.append("bold") else: if (not ("bold" in temp_list[2])): temp_list[2] += " bold" else: if (len(temp_list) == 3): if ("bold" in temp_list[2]): temp_list[2] = temp_list[2].replace("bold", "") self.textWidget.font = tuple(temp_list) self.textWidget.text.config(font=self.textWidget.font) def Italic(self): #finished temp_list = list(self.textWidget.font) if (self.italian.get() == "Enabled"): if (len(temp_list) == 2): temp_list.append("italic") else: if (not ("italic" in temp_list[2])): temp_list[2] += " italic" else: if (len(temp_list) == 3): if ("italic" in temp_list[2]): temp_list[2] = temp_list[2].replace("italic", "") self.textWidget.font = tuple(temp_list) self.textWidget.text.config(font=self.textWidget.font)
class Keylogger: def __init__(self): self.current_command = "" self.commands = read_to_list("macros.json") self.commands.sort(key=lambda x: len(x.name), reverse=True) self.categories = self.get_categories() self.listener = keyboard.Listener(on_press=self.on_key_press) self.controller = Controller() self.sending_command = False def start(self, category=None): if category is not None: self.commands = self.get_commands_by_category(category) self.listener.start() self.listener.join() def on_key_press(self, key): if not self.sending_command: if key == keyboard.Key.space: for command in self.commands: if command.matches(self.current_command): params = self.current_command.split( command.name)[1].split("$") for i in range(len(self.current_command) + 1): self.controller.tap(Key.backspace) self.controller.type(command.get_response(params)) break self.current_command = "" elif key == keyboard.Key.backspace: pass elif key == keyboard.Key.shift: pass elif key == keyboard.Key.enter: pass elif key == keyboard.Key.f8: exit(0) else: try: self.current_command += key.char except: self.current_command = "" def get_categories(self): return list(set([command.category for command in self.commands])) def check_command(self): for command in self.commands: if self.current_command == command.name: self.send_command(command) def get_commands_by_category(self, category): return [ command for command in self.commands if command.category == category ] def get_commands_except_category(self, category): return [ command for command in self.commands if command.category != category ] def add_macro(self, name, parameters, response): self.commands.append(Command(name, parameters, response)) write("macros.json", self.commands) def send_command(self, command): self.current_command = "" self.sending_command = True self.controller.press(Key.backspace) self.controller.release(Key.backspace) self.controller.type(command.response) self.sending_command = False
mask = cv2.resize(mask, (128, 128)) mask = mask.reshape(-1, 128, 128, 1) ############################ result = model.predict(mask) res = np.argmax(result) #print(res) cv2.putText(wind, "{}".format(res), (50, 125), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 255, 0), 2) if flag: if res == 0: cont.press(Key.space) cont.release(Key.space) flag = False elif res == 1: cont.press(Key.up) cont.release(Key.up) flag = False elif res == 2: cont.press(Key.down) cont.release(Key.down) flag = False elif res == 3: cont.press(Key.left) cont.release(Key.left) flag = False elif res == 4: cont.press(Key.right)
def on_frame(self, controller): frame = controller.frame() keyboard1 = Controller() """print "frame ID: " + str(frame.id) \ + " Timestamp: " + str(frame.timestamp) \ + " Number of Hands " + str(len(frame.hands)) \ + " Number of Fingers " + str(len(frame.fingers)) \ + " Number of Tools " + str(len(frame.tools)) \ + " Number of Gestures " + str(len(frame.gestures()))""" for hand in frame.hands: handType = 'Left Hand' if hand.is_left else 'Right Hand' print handType + ' Hand ID:' + str( hand.id) + "Palm Position" + str(hand.palm_position) print("Number of Gestures " + str(len(frame.gestures()))) print() print str(hand.palm_position.y) normal = hand.palm_normal direction = hand.direction pitchValue = hand.palm_position.y hold = 0 time.sleep(.1) if (hold == 0): if (pitchValue < 100): altTab() playPause() altTab() hold = 1 if (hold == 1): hold = 0 if (pitchValue > 300): altTab() time.sleep(1) keyboard1.press('g') keyboard1.release('g') time.sleep(1) altTab() """ hold = 0 if hold == 0: if((pitchValue < 100)): keyboard1.press('H') keyboard1.release('H') #altTab() print('h') hold = 1 if hold == 1: if((pitchValue > 100)): # altTab() print('h') keyboard1.press('H') keyboard1.release('H') hold = 0 """ # print "Pitch:" + str(direction.pitch * Leap.RAD_TO_DEG) + ' Roll: ' +str(normal.roll * Leap.RAD_TO_DEG) + ' Yaw: '+ str(direction.yaw * Leap.RAD_TO_DEG) """
left: Key.left, ok: Key.enter, #red toggles wether the arduino sends data or not green: ';', #change audio language yellow: 'l', #next subtitle blue: Key.space, back: Key.backspace, exit: 'x', menu: 'c' }.get(button, '') keyboard = Controller() while True: try: time.sleep(0.005) #sleep 5 ms ser = serial.Serial(USBLink) # open serial port input_message = ser.readline().rstrip() print(input_message) button = SelectButton(input_message) print(button) keyboard.press(button) time.sleep(0.005) keyboard.release(button) except KeyboardInterrupt: quit() except: pass
# pip install SpeechRecognition # pip install pipwin # pipwin install pyaudio # >> this will count to 100 and write the number line for line on your keyboard<< from pynput.keyboard import Key, Controller from time import sleep keyboard = Controller() sleep(3) for i in range (100): keyboard.type(str(i)) keyboard.press(Key.enter) keyboard.release(Key.enter) sleep(0.05)
class App: def __init__(self, master): self.master = master self.CRNs = [] self.keyboard = None self.listener_initialized = False master.title("CRN Automatic Paster") # Validation vcmd = (self.master.register(self.validate), '%S') self.v = False # self.v = IntVar() label_greeting = Label(master, text="CRN Automatic Paster") self.e1 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e2 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e3 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e4 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e5 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e6 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e7 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e8 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e9 = Entry(master, width=10, validate="key", validatecommand=vcmd) self.e10 = Entry(master, width=10, validate="key", validatecommand=vcmd) C = Checkbutton(master, text="Press Submit after pasting", command=self.cb) # variable=self.v button_done = Button(master, text="Done", command=self.done_pressed) label_greeting.grid(row=0, column=0, columnspan=10, pady=10) self.e1.grid(row=1, column=0, sticky=W, padx=7) self.e2.grid(row=1, column=1, sticky=W, padx=7) self.e3.grid(row=1, column=2, sticky=W, padx=7) self.e4.grid(row=1, column=3, sticky=W, padx=7) self.e5.grid(row=1, column=4, sticky=W, padx=7) self.e6.grid(row=2, column=0, sticky=W, padx=7, pady=10) self.e7.grid(row=2, column=1, sticky=W, padx=7) self.e8.grid(row=2, column=2, sticky=W, padx=7) self.e9.grid(row=2, column=3, sticky=W, padx=7) self.e10.grid(row=2, column=4, sticky=W, padx=7) C.grid(row=3, column=0, columnspan=2) button_done.grid(row=3, column=3, columnspan=2, sticky=W + E, pady=5, padx=3) self.generate_menu_bar() def done_pressed(self): self.CRNs = [ self.e1.get(), self.e2.get(), self.e3.get(), self.e4.get(), self.e5.get(), self.e6.get(), self.e7.get(), self.e8.get(), self.e9.get(), self.e10.get() ] if not self.listener_initialized: self.keyboard = Controller() listener = Listener(on_release=self.on_release, on_press=None) listener.start() self.listener_initialized = True def generate_menu_bar(self): menu = Menu(self.master) self.master.config(menu=menu) helpmenu = Menu(menu, tearoff=0) menu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="How to use", command=self.guide) helpmenu.add_command(label="Try it out!", command=self.demo) helpmenu.add_command(label="About...", command=self.about) def guide(self): guide_window = Toplevel() v = "1. Copy-Paste or manually input your required CRNs into the program's entry boxes.\n(Keep in mind the " \ "CRN must not contain any spaces or characters, else they won't be accepted into the entry box)\n2. Press " \ "the 'Done' Button\n3. Open BSS, highlight/press the FIRST entry box in BSS\n4. Press Shift (Either the " \ "left or the right one, both work) " guide_text = Label(guide_window, text=v, justify=LEFT) guide_text.pack() def demo(self): url = "demo.html" webbrowser.open(url, new=2) def about(self): about_window = Toplevel() v = "Made by Shady Fanous\[email protected]\nSource code at " \ "https://github.com/ShadyF/CRN_Paster\nthis tab needs to be redone " about_text = Label(about_window, text=v, justify=LEFT) about_text.pack() def iterate(self): for CRN in self.CRNs: if CRN: self.keyboard.type(CRN) self.keyboard.press(Key.tab) self.keyboard.release(Key.tab) # If Press enter after pasting checkbox is marked if self.v: self.keyboard.press(Key.enter) self.keyboard.release(Key.enter) @staticmethod def validate(s): return s.isdigit() def on_release(self, key): if key == Key.shift: self.iterate() def cb(self): self.v = not self.v
class MouseKeyboard: def __init__(self): self.keyboard = KeyboardController() self.mouse = MouseController() self.controlKeys = {37:self.keyLeft, 38:self.keyUp, 39:self.keyRight, 40:self.keyDown, 60:self.leftClick, 62:self.rightClick, 8:self.keyBksp, 13:self.keyEnter} def handleButtonPress(self, key): key = key.strip("'\\") try: keyNum = int(key) except ValueError: keyNum = ord(key) try: print('Got {}.'.format(keyNum)) except OSError: pass if keyNum in self.controlKeys.keys(): self.controlKeys[keyNum]() else: try: self.keyboard.press(key) self.keyboard.release(key) except ValueError: print("Failed to press {}: ValueError.".format(key)) def handleMouseMove(self, key): key = key.strip("'\\") (xStr, yStr) = key.split() try: xVal = int(xStr) yVal = int(yStr) except ValueError: print("Got MouseMove but x, y string format unexpected: '{}', '{}'".format(xStr, yStr)) print("Mouse Moving by: {}, {}".format(xVal,yVal)) self.mouse.move(xVal*8,yVal*8) def keyUp(self): self.keyboard.press(Key.up) self.keyboard.release(Key.up) def keyLeft(self): self.keyboard.press(Key.left) self.keyboard.release(Key.left) def keyRight(self): self.keyboard.press(Key.right) self.keyboard.release(Key.right) def keyDown(self): self.keyboard.press(Key.down) self.keyboard.release(Key.down) def keyBksp(self): self.keyboard.press(Key.backspace) self.keyboard.release(Key.backspace) def keySpace(self): self.keyboard.press(Key.space) self.keyboard.release(Key.space) def keyEnter(self): self.keyboard.press(Key.enter) self.keyboard.release(Key.enter) def leftClick(self): self.mouse.click(Button.left) def rightClick(self): self.mouse.click(Button.right)