def on_End_analog(): logging.info(30*'_' + " on_End_analog") k = PyKeyboard() #抬起功能按键Ctrl,否则End效果会变为Ctrl+End效果 k.release_key(k.control_key) k.tap_key(k.end_key) return False
def key_combo(key0, key1): k = PyKeyboard() if key0 == 'ctrl': key0 = k.control_key k.press_key(key0) k.tap_key(key1) k.release_key(key0)
def xdomenu(): """interacts with a simple menu.""" char_to_bin = {'s': 'srmenu', 'j': 'jmenu', 'c': 'clipmenu', 't': 'terminal', 'u': 'urxvt', 'p': 'pomodoro', ' ': 'moveempty'} keybrd = PyKeyboard() k_menu = keybrd.menu_key persistent = False print_menu(persistent) while True: sleep(0.1) stdout.flush() char = getchar() try: cmd = char_to_bin[char] print_flush(cmd) if persistent: sleep(0.2) keybrd.tap_key(k_menu) except KeyError: if char == '\t': persistent = not persistent print_menu(persistent) else: keybrd.tap_key(k_menu)
def previous_focus(): print('*_* next_focus') k = PyKeyboard() k.press_key(k.shift_key) k.tap_key(k.tab_key) k.release_key(k.shift_key)
def change_window_start(): print('*_* change_window_start') k = PyKeyboard() k.release_key(k.alt_key) k.press_key(k.alt_key) k.tap_key(k.tab_key)
def switch_to_highlayer(outName, frameNumber, segNumber): #totalIdx = len(inputList) ''' timeInterval defines the print frequency of the frame number in the terminal. ''' timeInterval = 0.4 frameStep = timeInterval*25 k = PyKeyboard() logName = outName + ".log" tmpIdx = 1 while True: text = subprocess.check_output(["tail", "-1", logName]) #tmpIdx = totalIdx - len(inputList)+1 if "PAUSE" in text and "===== PAUSE =====\rV:" not in text: sleep(timeInterval) continue elif "Exiting" in text: break else: print text sleep(timeInterval) frameIdx = parse_frame_idx(text) if frameIdx >= frameNumber*tmpIdx and frameIdx < frameNumber*tmpIdx + frameStep: print "======================================" print "currentFrame is: "+str(frameIdx)+".\n" if bool(stepList): tmpIdx = tmpIdx +1 value = stepList.pop(0) if value >0: for t in range(value): k.tap_key('b') print "switch to higher layer" sleep(0.1) else: break
def faceTracker(): print "Face Track running" k = PyKeyboard() faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') video_capture = cv2.VideoCapture(0) centerX = 0; while True: # Capture frame-by-frame ret, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(150, 150), ) #Draw a rectangle around the face if len(faces) >= 1: (x,y,w,h) = faces[0] cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) centerNew = x + w/2 if centerNew > centerX + 10: print('left') k.tap_key('Left') if centerNew < centerX - 10: print('right') k.tap_key('Right') centerX = centerNew if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything is done, release the capture video_capture.release() cv2.destroyAllWindows()
class KLKeyboard(object): def __init__(self): self.keyboard = PyKeyboard() pass def hitKey(self, key): self.keyboard.tap_key(key)
def close_tab(): print('*_* new_tab') k = PyKeyboard() k.release_key(k.control_l_key) k.press_key(k.control_l_key) k.tap_key(k.function_keys[4])F k.release_key(k.control_l_key)
def new_tab(): print('*_* new_tab') k = PyKeyboard() k.release_key(k.control_l_key) k.press_key(k.control_l_key) k.tap_key('t') k.release_key(k.control_l_key)
def next_tab(): print('*_* next_tab') k = PyKeyboard() k.press_key(k.control_l_key) # k.press_key(k.shift_key) k.tap_key(k.tab_key) k.release_key(k.control_l_key)
def previous_tab(): print('*_* previous_tab') k = PyKeyboard() k.press_key(k.control_l_key) k.press_key(k.shift_key) k.tap_key(k.tab_key) k.release_key(k.control_l_key) k.release_key(k.shift_key)
def runExp(self): k = PyKeyboard() # Resetting both gammas k.type_string('s') k.type_string('d') for i in xrange(self.SWEEP_STEP_SIZE): if (self.running): # Resetting gammaSta k.type_string('s') for j in xrange(self.SWEEP_STEP_SIZE): if (self.running): self.currTrial = i*self.SWEEP_STEP_SIZE + j + 1 # Reset and cool-down for nagging in xrange(5): k.type_string('0') sleep(0.3) self.strong_damper.damping = 0.0 # Perturbation k.type_string('j') sleep(3) # Increasing gammaSta k.type_string('w') sleep(1.0) # Add a strong damper to stop any motion self.strong_damper.damping = 100.0 # attmept to remove the residual torque. bitVal = convertType(0.0, fromType = 'f', toType = 'I') xem_spindle_bic.SendPara(bitVal = bitVal, trigEvent = 1) # Ia gain xem_spindle_tri.SendPara(bitVal = bitVal, trigEvent = 1) xem_spindle_bic.SendPara(bitVal = bitVal, trigEvent = 10) # II gain xem_spindle_tri.SendPara(bitVal = bitVal, trigEvent = 10) sleep(2.0) bitVal = convertType(1.2, fromType = 'f', toType = 'I') xem_spindle_bic.SendPara(bitVal = bitVal, trigEvent = 1) # Ia gain xem_spindle_tri.SendPara(bitVal = bitVal, trigEvent = 1) bitVal_II = convertType(2.0, fromType = 'f', toType = 'I') xem_spindle_bic.SendPara(bitVal = bitVal_II, trigEvent = 10) # II gain xem_spindle_tri.SendPara(bitVal = bitVal_II, trigEvent = 10) # Increasing gammaDyn k.type_string('e') k.tap_key(k.escape_key)
def keypress(key): """ Simulates a key press Args: key (str) : the key [a-zA-Z0-9] to enter. Use 'enter' for the return key """ k = PyKeyboard() if key == 'enter': key = k.return_key k.tap_key(key)
def fill_board(start_board, solution_board, x,y,width,height): cell_width = width / 9 cell_height = height / 9 mouse = PyMouse() keyboard = PyKeyboard() for i in range(0,9): for j in range(0,9): # insert only empty cell if start_board.values[j][i] == 0: sleep(0.1) mouse.click(x + cell_width*(j+0.5), y+cell_height*(i+0.5)) sleep(0.01) keyboard.tap_key(str(solution_board.values[j][i]))
def type_unicode(symbol): k = PyKeyboard() hexval = symbol.encode("unicode_escape") print hexval if hexval[:2] == '\u': # if the value is a hex number k.press_key('Control_L') # press the control k.press_key('Shift_L') # the left shift k.tap_key('u') # and the u key k.release_key('Control_L') # release the control k.release_key('Shift_L') # and shift keys hexval = hexval[2:] # remove the unicode escape character k.type_string(hexval) # type the unicode string k.tap_key('Return') # tap the return key else: # if the given string isnt a unicode character k.type_string(hexval) # just type the string
def key_pressed(action): print('*_* key_pressed %s' % action) k = PyKeyboard() if action == 'enterKey': k.tap_key(k.enterkey) elif action == 'backSpace': k.tap_key(k.backspace_key) elif action == 'spaceKey': k.tap_key(k.space_key) else: k.tap_key('%s' % action)
def sendMessage(message): if Var.locked: return if not (type(message) is str): try: message = str(message) except Exception: pass try: message = message.encode('ascii', 'ignore') except Exception: message = message.encode('utf-8', 'ignore') Var.locked = True k = PyKeyboard() try: k.type_string(message) except Exception: k.press_key(k.control_l_key) k.tap_key('A') k.release_key(k.control_l_key) k.tap_key(k.backspace_key) k.type_string("Message Contains Unknown Characters") k.tap_key(k.enter_key) Var.locked = False
class SwitchManager(object): def __init__(self, toggle_interval, strategy): self._toggle_interval = toggle_interval self._keyboard = PyKeyboard() self._strategy = strategy(self._keyboard) self._active = False self._last_toggle = time.time() def _activate(self): print 'activate' self._active = True self._strategy.open() def _deactivate(self): print 'deactivate' self._active = False self._strategy.close() def toggle(self): if self._last_toggle + self._toggle_interval > time.time(): print 'toggle interval' return print 'toggle' self._last_toggle = time.time() if self._active: self._deactivate() else: self._activate() def _move(self, key): print 'move', key if self._active: self._last_toggle = time.time() self._keyboard.tap_key(key) left = lambda self: self._move(self._keyboard.left_key) right = lambda self: self._move(self._keyboard.right_key) up = lambda self: self._move(self._keyboard.up_key) down = lambda self: self._move(self._keyboard.down_key)
def runScript(delay, SCRIPT_NAME): #screen = wnck.screen_get_default() screen = wnck.screen_get(0) while gtk.events_pending(): gtk.main_iteration() windowTitle = re.compile('.*Board.*') for window in screen.get_windows(): if windowTitle.match(window.get_name()): window.activate(int(time.time())) window.maximize() #MOUSE CLICK TO ACTUALLY FOCUS EAGLE BOARD m = PyMouse() x, y = m.screen_size() #m.click(x/4, y/4, 1) #KEYBOARD INPUT k = PyKeyboard() #BRING UP ALT-FILE MENU k.press_key(k.alt_key) k.type_string('f') k.release_key(k.alt_key) #PRESS T TO RUN SCRIPT FROM ALT-FILE MENU k.type_string('t') time.sleep(delay) #TYPE IN SCRIPT NAME TO DIALOG BOX k.type_string('/home/anton/Documents/st/st-py/' + SCRIPT_NAME) time.sleep(delay) time.sleep(delay) k.tap_key(k.enter_key)
def play_solution(solution): k = PyKeyboard() for row in solution: for c in row: if c == 'X': k.tap_key("space") elif c == '.': pass else: raise Exception k.tap_key("rightarrow") k.tap_key("downarrow") for _ in range(0, len(row)): k.tap_key("leftarrow")
def runScript(delay, SCRIPT_PATH, SCRIPT_NAME): screen = wnck.screen_get(0) #screen = wnck.screen_get_default() while gtk.events_pending(): gtk.main_iteration() windowTitle = re.compile('.*Board.*') for window in screen.get_windows(): if windowTitle.match(window.get_name()): window.activate(int(time.time())) window.maximize() #MOUSE CLICK TO ACTUALLY FOCUS EAGLE BOARD m = PyMouse() x, y = m.screen_size() #m.click(x/4, y/4, 1) #KEYBOARD INPUT k = PyKeyboard() caps_flag = 0 if int(commands.getoutput('xset q | grep LED')[65]) % 2: k.tap_key(k.caps_lock_key) caps_flag = 1 #BRING UP ALT-FILE MENU k.press_key(k.alt_key) k.type_string('f') k.release_key(k.alt_key) #PRESS T TO RUN SCRIPT FROM ALT-FILE MENUs k.type_string('t') time.sleep(delay) #TYPE IN SCRIPT NAME TO DIALOG BOX k.type_string(SCRIPT_PATH + SCRIPT_NAME) time.sleep(delay) time.sleep(delay) k.tap_key(k.enter_key) if caps_flag: k.tap_key(k.caps_lock_key)
def Qlearn(episode, steps, prob, sess, sz): gamma = 0.99 learnrate = .618 k = PyKeyboard() #runs the cnn model to create initial values of all my values states, input_layer = cnn() act = tf.placeholder("float", [None, numact]) nextQ = tf.placeholder("float", [None]) #for performing gradient decent and minimizing loss stateact = tf.reduce_sum(tf.multiply(states, act), reduction_indices=1) loss = tf.reduce_mean(tf.square(nextQ - stateact)) trainer = tf.train.AdamOptimizer(1e-6) updateModel = trainer.minimize(loss) #saves training data saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) # if i==0: # st=tf.train.get_checkpoint_state('./') # if st and st.model_checkpoint_path : # saver.restore(sess,st.model_checkpoint_path) # print ("loaded") # else: # print ("nothing to restore") #initializesall my variables so i can now print them after they have been evaluated count = 0 #LOOP FOR THE NUMBER OF EPISODES I WANT TO USE for j in range(episode): th = 0 c = 0 xprev = 0 x2prev = 0 x3prev = 0 x4prev = 0 d = 0 e = 0 ystanding = 0 ycurrent = 0 xstanding = 0 y = 0 x = 0 totreward = 0 oldscore = 0 end = 0 gnd = 0 score = 0 xold = 0 epsilon = prob # epsilon= 0.1 es = 0.1 ef = 0.0001 tot = 50000 #make first action [right] index = 0 oldinp = 1 k.release_key('x') k.release_key('z') k.release_key(k.right_key) k.release_key(k.up_key) k.release_key(k.down_key) k.release_key(k.left_key) #will call the next state and return new state info, rewards,and misc img, reward, atend, th, ystanding, oldscore, xold, oldinp = keypress( index, th, ystanding, oldscore, xold, oldinp) img = cv2.cvtColor(cv2.resize(img, (88, 88)), cv2.COLOR_BGR2GRAY) s_t = np.stack((img, img, img, img), axis=2) #iterates through number of steps in each episode for i in range(steps): # print(loss) # out=tf.Print(loss,[loss],'hi') #will put all my Q values into an array[][] readout_t = states.eval(feed_dict={input_layer: [s_t]}) print("readout") print(readout_t) #<- will print out the cnn state values print("episode ") print(j) print("count ") print(count) print("random ") print(epsilon) # will either ge max q or a random q based on episilon e if random.random() <= epsilon: print("random") #chooses a random action (takes the index) a1 = random.choice(list(enumerate(readout_t[0])))[0] else: print("max") #gets the max Q value a1 = np.argmax(readout_t[0]) print(a1) if epsilon > ef and count > 100 and sz != True: epsilon -= (es - ef) / tot # #makes an action and returnst state 2, reward img2, reward, atend, th, ystanding, oldscore, xold, oldinp = keypress( a1, th, ystanding, oldscore, xold, oldinp) # cv2.imwrite('st2.png',s_t2) #gets total reward img2 = cv2.cvtColor(cv2.resize(img2, (88, 88)), cv2.COLOR_BGR2GRAY) # ret, img2 = cv2.threshold(img2, 1, 255, cv2.THRESH_BINARY) img2 = np.reshape(img2, (88, 88, 1)) totreward = totreward + reward s_t2 = np.append(img2, s_t[:, :, :3], axis=2) print("total reward") print(totreward) z = np.zeros([numact]) a_t = np.zeros([numact]) z[a1] = 1 #store transition from S1->s2 saved in replay memory actionreplay(s_t, z, reward, s_t2, atend) if len(action_replay) == replaysize: action_replay.popleft() #after count # of frames will begin training on a batch of previous states if count >= 100 and sz != True: print("acton replay ") #sample from minibatch D = random.sample(action_replay, batchsize) #puts values from the minibatch into arrays sta1 = [k[0] for k in D] abat = [k[1] for k in D] rew = [k[2] for k in D] sta2 = [k[3] for k in D] term = [k[4] for k in D] Q = [] #gets q value for s_t2 (second state) for the minibatch readout_t2 = states.eval(feed_dict={input_layer: sta2}) for i in range(len(D)): #if we have died or reached the end Q=r #otherwuse q= reward+gamma*state2 if term[i] == False: #update the Q value (train it) Q.append(rew[i] + gamma * np.max(readout_t2[i])) else: Q.append(rew[i]) #updates the model with the new Q values tt = updateModel.run(feed_dict={ nextQ: Q, act: abat, input_layer: sta2 }) # tt=updateModel.run(feed_dict={act:abat, nextQ: Q, input_layer : sta1}) print("updated") print(totreward) count += 1 #saves the model after 1k steps if i % 1000 == 0 and sz != True: saver.save(sess, './mario.ckpt', global_step=i, write_state=True) #if we have died or reached the end terminate episode else continue if atend == True: print("restart") break else: #set the new state s_t = s_t2 #end of for #end of for k = PyKeyboard() print("restart") #resets the stage k.tap_key('q')
#!/bin/env python3 from pykeyboard import PyKeyboard as PyK k = PyK() k.tap_key(k.return_key)
m = PyMouse() k = PyKeyboard() sleep(2) GPIO.setmode(GPIO.BCM) GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Doorbell Button GPIO.setup(9, GPIO.OUT) # Indicator LED while True: if (GPIO.input(22) == 1): print("Doorbell Pressed") GPIO.output(9, True) m.click(276, 372) # Click in the Duo text box sleep(0.5) k.tap_key('Delete', n=6, interval=0.05) # Delete existing characters k.type_string("Father") # Type name of Duo contact sleep(0.2) k.tap_key('Return') # Hit Return to select contact sleep(1.5) m.click(683, 445) # Click on Video Call button GPIO.output(9, False) for count in range(0, 30): GPIO.output(9, True) sleep(0.8) GPIO.output(9, False) sleep(0.8) else: sleep(0.05)
def command(self, com): """Function that serves as the entry point for each one of the user commands. This function goes through these steps for each one of user's commands, respectively: - Search across the built-in commands via a simple if-else control flow. - Try to get a response from :func:`dragonfire.arithmetic.arithmetic_parse` function. - Try to get a response from :func:`dragonfire.learn.Learner.respond` method. - Try to get a answer from :func:`dragonfire.odqa.ODQA.respond` method. - Try to get a response from :func:`dragonfire.deepconv.DeepConversation.respond` method. Args: com (str): User's command. Returns: str: Response. """ if not self.args["server"]: global config_file global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) args = self.args userin = self.userin user_full_name = self.user_full_name user_prefix = self.user_prefix if self.testing: config_file = self.config_file if isinstance(com, str) and com: com = com.strip() else: return False print("You: " + com.upper()) doc = nlp(com) h = Helper(doc) self.h = h if args["verbose"]: userin.pretty_print_nlp_parsing_results(doc) # Input: DRAGONFIRE | WAKE UP | HEY if self.inactive and not self.check_wake_up_intent: return "" # User is answering to Dragonfire if user_answering['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): user_answering['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if user_answering['for'] == 'wikipedia': with nostderr(): search_query = user_answering['options'][selection] try: return self.wikisearch(search_query) except requests.exceptions.ConnectionError: return self.wikipedia_connection_error() except WikipediaNoResultsFoundError: return self.wikipedia_no_results_found_error( search_query) except Exception: return False # Input: DRAGONFIRE | WAKE UP | HEY if self.check_wake_up_intent(): self.inactive = False return userin.say( choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix.capitalize() + ", tell me your wish." ])) # Input: GO TO SLEEP if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or ( h.check_verb_lemma("stop") and h.check_verb_lemma("listen")): self.inactive = True userin.execute([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) return userin.say("I'm going to sleep") # Input: ENOUGH | SHUT UP if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut") and h.check_nth_lemma(-1, "up")): tts_kill() msg = "Dragonfire quiets." print(msg) return msg # Input: WHAT IS YOUR NAME if h.check_wh_lemma("what") and h.check_deps_contains("your name"): return userin.execute([" "], "My name is Dragonfire.", True) # Input: WHAT IS YOUR GENDER if h.check_wh_lemma("what") and h.check_deps_contains("your gender"): return userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") # Input: WHO AM I | SAY MY NAME if (h.check_wh_lemma("who") and h.check_text("I")) or (h.check_verb_lemma("say") and h.check_text("my") and h.check_lemma("name")): userin.execute([" "], user_full_name) return userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") # Input: OPEN [CAMERA, CALENDAR, CALCULATOR, STEAM, BLENDER, WRITER, MATH, IMPRESS, DRAW, TERMINAL] if h.check_verb_lemma("open") or h.check_adj_lemma( "open") or h.check_verb_lemma("run") or h.check_verb_lemma( "start") or h.check_verb_lemma("show"): if h.check_text("blender"): userin.execute(["blender"], "Blender") return userin.say("Blender 3D computer graphics software") if h.check_text("draw"): userin.execute(["libreoffice", "--draw"], "LibreOffice Draw") return userin.say("Opening LibreOffice Draw") if h.check_text("impress"): userin.execute(["libreoffice", "--impress"], "LibreOffice Impress") return userin.say("Opening LibreOffice Impress") if h.check_text("math"): userin.execute(["libreoffice", "--math"], "LibreOffice Math") return userin.say("Opening LibreOffice Math") if h.check_text("writer"): userin.execute(["libreoffice", "--writer"], "LibreOffice Writer") return userin.say("Opening LibreOffice Writer") if h.check_noun_lemma("browser") or h.check_text( "chrome") or h.check_text("firefox"): userin.execute(["sensible-browser"], "Web Browser") return userin.say("Web browser") if h.check_text("steam"): userin.execute(["steam"], "Steam") return userin.say("Opening Steam Game Store") if h.check_text("files"): return self.start_file_manager() if h.check_noun_lemma("camera"): userin.execute(["kamoso"], "Camera") # KDE neon userin.execute(["snap-photobooth"], "Camera") # elementary OS userin.execute(["cheese"], "Camera") # Ubuntu return userin.say("Camera") if h.check_noun_lemma("calendar"): userin.execute(["korganizer"], "Calendar") # KDE neon userin.execute(["maya-calendar"], "Calendar") # elementary OS userin.execute(["orage"], "Calendar") # Ubuntu return userin.say("Calendar") if h.check_noun_lemma("calculator"): userin.execute(["kcalc"], "Calculator") # KDE neon userin.execute(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(["gnome-calculator"], "Calculator") # Ubuntu return userin.say("Calculator") if h.check_noun_lemma("console") or h.check_noun_lemma("terminal"): userin.execute(["konsole"], "Terminal") # KDE neon userin.execute(["gnome-terminal"], "Terminal") # elementary OS & Ubuntu userin.execute(["guake"], "Terminal") # Guake return userin.say("Terminal") # Input FILE MANAGER | FILE EXPLORER if h.check_noun_lemma("file") and (h.check_noun_lemma("manager") or h.check_noun_lemma("explorer")): return self.start_file_manager() # Input: SOFTWARE CENTER if h.check_noun_lemma("software") and h.check_text("center"): userin.execute(["plasma-discover"], "Software Center") # KDE neon userin.execute(["software-center"], "Software Center") # elementary OS & Ubuntu return userin.say("Software Center") # Input: OFFICE SUITE if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") return userin.say("Opening LibreOffice") # Input: GIMP | PHOTOSHOP | PHOTO EDITOR if h.check_text("gimp") or (h.check_noun_lemma("photo") and (h.check_noun_lemma("editor") or h.check_noun_lemma("shop"))): userin.execute(["gimp"], "GIMP") return userin.say("Opening the photo editor software.") # Input: INKSCAPE | VECTOR GRAPHICS if h.check_text("inkscape") or (h.check_noun_lemma("vector") and h.check_noun_lemma("graphic")) or ( h.check_text("vectorial") and h.check_text("drawing")): userin.execute(["inkscape"], "Inkscape") return userin.say("Opening the vectorial drawing software.") # Input: Kdenlive | VIDEO EDITOR if h.check_text("kdenlive") or (h.check_noun_lemma("video") and h.check_noun_lemma("editor")): userin.execute(["kdenlive"], "Kdenlive") return userin.say("Opening the video editor software.") # Input: MY TITLE IS LADY | I'M A LADY | I'M A WOMAN | I'M A GIRL if h.check_lemma("be") and h.check_lemma( "-PRON-") and h.check_gender_lemmas('female'): return self.gender_update('female') # Input: MY TITLE IS SIR | I'M A MAN | I'M A BOY if h.check_lemma("be") and h.check_lemma( "-PRON-") and h.check_gender_lemmas('male'): return self.gender_update('male') # Input: CALL ME * if h.check_lemma("call") and h.check_lemma("-PRON-"): title = "" for token in doc: if token.pos_ == "NOUN": title += ' ' + token.text title = title.strip() if not args["server"]: callme_config = config_file.search( Query().datatype == 'callme') if callme_config: config_file.update({'title': title}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': title}) self.user_prefix = title user_prefix = self.user_prefix return userin.say("OK, " + user_prefix + ".") # Input: WHAT'S THE TEMPERATURE IN * if h.is_wh_question() and h.check_lemma("temperature"): city = "" for ent in doc.ents: if ent.label_ == "GPE": city += ' ' + ent.text city = city.strip() if city: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() try: weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format( city, weather.get_temperature('celsius')['temp']) userin.execute([" "], msg) return userin.say(msg) except IndexError: msg = "Sorry, " + user_prefix + " but I couldn't find a city named " + city + " on the internet." userin.execute([" "], msg) return userin.say(msg) # Input: WHAT TIME IS IT if h.check_wh_lemma("what") and h.check_noun_lemma( "time") and h.check_verb_lemma("be") and h.check_text("it"): return userin.say("It's " + datetime.datetime.now().strftime("%I:%M %p") + choice([", " + user_prefix + ".", "."])) # Input: KEYBOARD * if (h.check_nth_lemma(0, "keyboard") or h.check_nth_lemma(0, "type")) and not args["server"]: n = len(doc[0].text) + 1 with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: for character in com[n:]: k.tap_key(character) k.tap_key(" ") return "keyboard" # Input: ENTER | NEW TAB | SWITCH TAB | CLOSE | GO BACK | GO FORWARD if (h.directly_equal(["enter"]) or (h.check_adj_lemma("new") and h.check_noun_lemma("line"))) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(k.enter_key) return "enter" if h.check_adj_lemma("new") and h.check_noun_lemma( "tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 't']) return "new tab" if h.check_verb_lemma("switch") and h.check_noun_lemma( "tab") and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, k.tab_key]) return "switch tab" if h.directly_equal(["CLOSE", "ESCAPE"]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) return "close" if self.check_browser_history_nav_intent("back"): return self.press_browser_history_nav("back") if self.check_browser_history_nav_intent("forward"): return self.press_browser_history_nav("forward") # Input: SCROLL LEFT | SCROLL RIGHT | SCROLL UP | SCROLL DOWN if (h.check_text("swipe") or h.check_text("scroll")) and not args["server"]: if h.check_text("left"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, -5) return "swipe left" if h.check_text("right"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(0, 5) return "swipe right" if h.check_text("up"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(5, 0) return "swipe up" if h.check_text("down"): with nostdout(): with nostderr(): m = PyMouse() if not self.testing: m.scroll(-5, 0) return "swipe down" # Input: PLAY | PAUSE | SPACEBAR if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR" ]) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.tap_key(" ") return "play" # Input: SHUT DOWN THE COMPUTER if ((h.check_text("shut") and h.check_text("down")) or (h.check_text("power") and h.check_text("off")) ) and h.check_text("computer") and not args["server"]: return userin.execute(["sudo", "poweroff"], "Shutting down", True, 3) # Input: GOODBYE | BYE BYE | SEE YOU LATER if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma( 0, "bye") or (h.check_verb_lemma("see") and h.check_text("you") and h.check_adv_lemma("later")): response = userin.say("Goodbye, " + user_prefix) if not args["server"] and not self.testing: # raise KeyboardInterrupt thread.interrupt_main() return response # Input: (SEARCH|FIND) * (IN|ON|AT|USING) WIKIPEDIA if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("Wikipedia"): with nostderr(): search_query = self.strip_the_search_query_by_intent( doc, "Wikipedia") if search_query: try: return self.wikisearch(search_query) except requests.exceptions.ConnectionError: return self.wikipedia_connection_error() except wikipedia.exceptions.DisambiguationError as disambiguation: user_answering['status'] = True user_answering['for'] = 'wikipedia' user_answering['reason'] = 'disambiguation' user_answering['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[ 0] msg = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[ 0] for option in disambiguation.options[1:3]: msg += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.execute([" "], notify) return userin.say(msg) except WikipediaNoResultsFoundError: return self.wikipedia_no_results_found_error( search_query) except Exception: pass # Input: (SEARCH|FIND) * (IN|ON|AT|USING) YOUTUBE if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("YouTube"): with nostdout(): with nostderr(): search_query = self.strip_the_search_query_by_intent( doc, "YouTube") if search_query: info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.execute(["sensible-browser", youtube_url], youtube_title) youtube_title = TextToAction.fix_the_encoding_in_text_for_tts( youtube_title) response = userin.say( youtube_title, ["sensible-browser", youtube_url]) else: youtube_title = "No video found, " + user_prefix + "." response = userin.say(youtube_title) k = PyKeyboard() if not args["server"] and not self.testing: time.sleep(5) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') return response # Input: (SEARCH|FIND) * (IN|ON|AT|USING) (GOOGLE|WEB) if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("Google") or h.check_lemma("web") or h.check_lemma("internet")) and not h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "Google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query return userin.execute(["sensible-browser", tab_url], search_query, True) # Input: (SEARCH IMAGES OF|FIND IMAGES OF|SEARCH|FIND) * (IN|ON|AT|USING) (GOOGLE|WEB|GOOGLE IMAGES|WEB IMAGES) if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("Google") or h.check_lemma("web") or h.check_lemma("internet")) and h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "Google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.lemma_ == "image" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" return userin.execute(["sensible-browser", tab_url], search_query, True) original_com = com com = coref.resolve(com) if args["verbose"]: print("After Coref Resolution: " + com) arithmetic_response = arithmetic_parse(com) if arithmetic_response: return userin.say(arithmetic_response) else: learner_response = learner.respond(com) if learner_response: return userin.say(learner_response) else: odqa_response = odqa.respond(com, not args["silent"], userin, user_prefix, args["server"]) if odqa_response: return userin.say(odqa_response) else: dc_response = dc.respond(original_com, user_prefix) if dc_response: return userin.say(dc_response)
eyes = cascade2.detectMultiScale(sub_gray, minSize=(sub_face.shape[0] / 6, sub_face.shape[0] / 6)) ael_x = ael_y = [] if len(eyes) != 0: ex1 = x + (w / 2) ey1 = y + (h / 2) # ex1 = eyes[0][0] # ey1 = eyes[0][1] # ew1 = eyes[0][2] # eh1 = eyes[0][3] if ex1 - 240 > 0: k.tap_key('Right') elif ex1 - 240 < 0: k.tap_key('Left') if ey1 - 320 > 0: k.tap_key('Down') elif ey1 - 320 < 0: k.tap_key('Up') # if len(eyes) != 0: # if flag == 1: # ex1 = eyes[0][0] # ey1 = eyes[0][1] # ew1 = eyes[0][2] # eh1 = eyes[0][3] # ex = ex1
def comboSequence(*arg): k = PyKeyboard() for key in arg[:-1]: k.tap_key(key)
from selenium import webdriver import time from pykeyboard import PyKeyboard '''driver = webdriver.Chrome() driver.get('https://www.imooc.com/user/newlogin') driver.find_element_by_name('email').send_keys('*****@*****.**') driver.find_element_by_name('password').send_keys('zhouting414') driver.find_element_by_class_name('moco-btn').click() time.sleep(3) driver.get('https://www.imooc.com/user/setprofile') time.sleep(5) # 上传图片 element = driver.find_element_by_id("upload") element.send_keys('/Users/edz/Downloads/Picture.jpg') time.sleep(6)''' driver = webdriver.Chrome() driver.get('chrome://settings/importData') time.sleep(10) pykey = PyKeyboard() pykey.press_key(pykey.return_key) pykey.tap_key('return') time.sleep(3) driver.close()
from pymouse import PyMouse from pykeyboard import PyKeyboard m = PyMouse() k = PyKeyboard() # pressing a key k.press_key('H') # which you then follow with a release of the key k.release_key('H') # or you can 'tap' a key which does both k.tap_key('e') # note that that tap_key does support a way of repeating keystrokes with a interval time between each k.tap_key('l', n=2, interval=1) # and you can send a string if needed too k.type_string('o World!') # move the mouse to int x and int y (these are absolute positions) m.move(200, 200) # click works about the same, except for int button possible values are 1: left, 2: right, 3: middle m.click(600, 900, 1) # get the screen size m.screen_size() # (1024, 768) # get the mouse positionHello World!Hello World! m.position() # (500, 300)lo World!
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) if abs(dist_change) > 0 or abs(angle_change) > 30: msg = str(dist_change) gesFound = 10 if dist_change > 200: k1.tap_key('-', n=2) #msg+=' Z out' else: k1.tap_key(k1.numpad_keys['Add'], n=2) #msg+=' Z in' event_que.append((ut.getDist(l[0], l[1]), ut.getSlope(l[0], l[1]))) cv2.putText( img, 'que-> ' + str(len(event_que)) + ' ' + str(len(l)) + ' ' + str(press_count), (300, 50), font, 1, (255, 255, 255), 2, cv2.LINE_AA) if gesFound > 0: cv2.putText(img, msg, (100, 100), font, 2, (255, 255, 255), 10, cv2.LINE_AA) gesFound -= 1
def typer(): k = PyKeyboard() text = getText() k.tap_key(k.backspace_key) temp = 0 for i in text: if i == '\n': k.tap_key(k.return_key) if temp != '\n': k.tap_key(k.home_key) elif i == '\t': k.tap_key(k.tab_key) else: k.type_string(i) temp = i k.tap_key(k.escape_key) k.press_key(k.shift_key) k.press_key(k.control_key) k.press_key(k.end_key) k.release_key(k.shift_key) k.release_key(k.control_key) k.release_key(k.end_key) k.tap_key(k.backspace_key)
print i browser = webdriver.Firefox() # Opening Firefox with its Homepage browser.maximize_window() url='http://www.fakemailgenerator.com/#/superrito.com/monkeydluffy' + str(i)+ '/' browser.get(url) # Open the fake mail generator in TAB1 time.sleep(2) k.press_key(k.control_key) k.tap_key('t') k.release_key(k.control_key) # Getting an element where Keys can be passed # Opening a NEW TAB browser.get('http://hck.re/qcKgO4') time.sleep(11) m.click(598L,798L,1,1) mail='monkeydluffy'+str(i)+'@superrito.com' k.type_string(mail) k.tap_key(k.return_key)
def upload_pic(self, image_path, caption, c_flag=1): self.dummy_clicks( 'Upload', '//*[@id="react-root"]/section/nav[2]/div/div/div[2]/div/div/div[3]', 2, [1, 1]) ###### for windows ###### # handle file select # rand_Sleep(1, 2) # autokey.win_active("Open") # rand_Sleep(1, 2) # autokey.control_send("Open", "Edit1", image_path) # rand_Sleep(1, 2) # autokey.control_send("Open", "Edit1","{ENTER}") # rand_Sleep(1, 2) ###### for windows ends ###### ###### for linex ###### k = PyKeyboard() m = PyMouse() x_dim, y_dim = m.screen_size() for i in image_path: sleep(0.01) k.tap_key(i) rand_Sleep(1, 2) # to click open button m.click(x_dim - 60, 50, 1) # to click browser button ###### for linex ends ###### m.click(20, y_dim - 20, 1) rand_Sleep(2, 3) # image fit button try: img_fit_btn = self.driver.find_element_by_xpath( '//*[@id="react-root"]/section/div[2]/div[2]/div/div/div/button[1]' ) except Exception as e: pass else: img_fit_class = str( img_fit_btn.find_element_by_tag_name('span').get_attribute( 'class')) print(img_fit_class) if "Expand" in img_fit_class: self.dummy_clicks( 'Next', '//*[@id="react-root"]/section/div[2]/div[2]/div/div/div/button[1]' ) # next button self.dummy_clicks( 'Next', '//*[@id="react-root"]/section/div[1]/header/div/div[2]/button') if c_flag == 1: # add fake location self.dummy_clicks( 'Add Location', '//*[@id="react-root"]/section/div[2]/section[2]/button') options = 0 tr = 0 while options == 0 and tr < 5: tr += 1 self.dummy_writes( 'Location', '//*[@id="react-root"]/section/div[2]/input', self.faker.country()) options = len( self.driver.find_elements_by_xpath( '//*[@id="react-root"]/section/div[3]/div/div/button')) if options == 0: self.dummy_clicks( 'Go Back', '//*[@id="react-root"]/section/div[1]/header/div/div[1]/button' ) else: pick = random.randint(0, options) print('No. of Location picked is {}'.format(pick)) # pick a location rand_Sleep(3, 5) self.dummy_clicks( 'Pick Location', '//*[@id="react-root"]/section/div[3]/div/div/button[{}]'. format(pick)) # write caption rand_Sleep(1, 2) for line in caption: self.dummy_writes( 'Caption', '//*[@id="react-root"]/section/div[2]/section[1]/div[1]/textarea', line, 2, [0, 0]) rand_Sleep(1, 1) # share self.dummy_clicks( 'Share', '//*[@id="react-root"]/section/div[1]/header/div/div[2]/button') rand_Sleep(5, 10) self.close_extras()
# # k.tap_key(k.enter_key) # time.sleep(.5) # screen = m.screen_size() # print(screen) # # #select test name # m.click(600,700, 1, 2) # #replace with david # for l in 'David': # k.tap_key(l) #MAKE SURE BROTHER IS DEFAULT PRINTER!!! #open the print dialog k.press_key(k.control_r_key) k.tap_key('p') k.release_key(k.control_r_key) #pause time.sleep(1) #move to Printer Properties k.tap_key(k.tab_key, 3) #open Printer Properties k.tap_key(k.enter_key) #pause time.sleep(1) #move to select ink k.tap_key(k.tab_key, 3) if __ink_type == 'cmykw': #move to Highlight slider
""" 键盘修饰符的使用: # 打印按键属性 print(dir(k)) """ # 导入模块 from pykeyboard import PyKeyboard import time k = PyKeyboard() # # 按 , 号键 # k.press_key(",") # k.release_key(",") # # # 按 shit+ / 键 # k .press_keys([k.shift_key, '/']) # 联合按键-按ctrl + s k.press_key(k.control_key) k.tap_key("c") k.release_key(k.control_key) # 打印按键属性 print(dir(k))
def insertKey(self): k = PyKeyboard() k.tap_key('e') k.tap_key('a')
def convert_md_html(pathAndFile_of_md): #获取md、html文件的文件名以及路径 [md_fileName, md_path, _, _] = get_html_pathAndFile(pathAndFile_of_md) #用typora打开文件,并等待4s for file in os.listdir(md_path): if '.html' in file: os.system('cd %s; rm *.html &' % md_path) time.sleep(0.1) #保证md文件夹中没有html文件,然后打开typora,并输入html文件 os.system('cd %s; typora %s &' % (md_path, md_fileName)) time.sleep(4) #模拟键盘按键,输出html文件 k = PyKeyboard() k.tap_key(k.alt_key) time.sleep(0.2) k.tap_key(k.enter_key) time.sleep(0.2) k.tap_key('e') time.sleep(0.2) k.tap_key(k.enter_key) time.sleep(0.2) k.tap_key('h') time.sleep(0.2) k.tap_key(k.enter_key) time.sleep(0.3) k.tap_key(k.enter_key) time.sleep(1) #ctrl+w 关闭typora k.press_key(k.control_key) time.sleep(0.1) k.tap_key('w') time.sleep(0.1) k.release_key(k.control_key)
def command(com, args): if (type(com) is not str) or com == "": return False original_com = com global inactive global user_full_name global user_prefix global config_file global e com = com.upper() print("You: " + com) if inactive == 1 and com not in ("DRAGONFIRE", "DRAGON FIRE", "WAKE UP", "HEY"): return True if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False choice = None if com.startswith("FIRST") or com.startswith("THE FIRST"): choice = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): choice = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): choice = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][choice] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.define(["sensible-browser", wikipage.url], search_query) userin.execute(0) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.define([" "], "Wikipedia connection error.") userin.execute(0) userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) return True except: return True if com in ("DRAGONFIRE", "DRAGON FIRE", "WAKE UP", "HEY"): tts_kill() inactive = 0 userin.define([" "], " ") words_dragonfire = { 0: "Yes, " + user_prefix + ".", 1: "Yes. I'm waiting.", 2: "What is your orders?" } userin.say(words_dragonfire[randint(0, 2)]) elif "GO TO SLEEP" == com: tts_kill() inactive = 1 userin.define([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.execute(0) userin.say("I'm going to sleep") elif com in ("ENOUGH", "SHUT UP"): print("Dragonfire quiets.") tts_kill() elif com in ("WHO AM I", "SAY MY NAME"): tts_kill() userin.define([" "], user_full_name) userin.execute(0) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") elif com in ("MY TITLE IS LADY", "I'M A LADY", "I'M A WOMAN", "I'M A GIRL"): tts_kill() config_file.update({'gender': 'female'}, Query().datatype == 'gender') user_prefix = "My Lady" userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com in ("MY TITLE IS SIR", "I'M A MAN", "I'M A BOY"): tts_kill() config_file.update({'gender': 'male'}, Query().datatype == 'gender') user_prefix = "Sir" userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com.startswith("CALL ME "): tts_kill() callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({'title': original_com[8:].lower()}, Query().datatype == 'callme') else: config_file.insert({ 'datatype': 'callme', 'title': original_com[8:].lower() }) user_prefix = original_com[8:].lower().encode("utf8") userin.define([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "WHAT IS YOUR NAME" == com: tts_kill() userin.define([" "], "My name is Dragonfire.") userin.execute(0) userin.say("My name is Dragon Fire.") elif "WHAT" in com and "TEMPERATURE" in com: # only for The United States today but prepared for all countries. Also only for celsius degrees today. --> by Radan Liska :-) tts_kill() capture = re.search( "(?:WHAT IS|WHAT'S) THE TEMPERATURE (?:IN|ON|AT|OF)? (?P<city>.*)", com) if capture: city = capture.group('city') owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() userin.define([" "], "The temperature in " + city + " is " + str(weather.get_temperature('celsius')['temp']) + " degrees celsius") userin.execute(0) userin.say("The temperature in " + city + " is " + str(weather.get_temperature('celsius')['temp']) + " degrees celsius") elif "WHAT IS YOUR GENDER" == com: tts_kill() userin.define([" "], " ") userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") elif "FILE MANAGER" in com or "OPEN FILES" == com: tts_kill() userin.define(["dolphin"], "File Manager") # KDE neon userin.execute(0) userin.define(["pantheon-files"], "File Manager") # elementary OS userin.execute(0) userin.define(["nautilus", "--browser"], "File Manager") # Ubuntu userin.execute(0) userin.say("File Manager") elif "WEB BROWSER" in com: tts_kill() userin.define(["sensible-browser"], "Web Browser") userin.execute(0) userin.say("Web Browser") elif "OPEN BLENDER" == com: tts_kill() userin.define(["blender"], "Blender") userin.execute(0) userin.say("Blender 3D computer graphics software") elif "PHOTO SHOP" in com or "PHOTO EDITOR" in com or "GIMP" in com: tts_kill() userin.define(["gimp"], "GIMP") userin.execute(0) userin.say("Photo editor") elif "INKSCAPE" in com or "VECTOR GRAPHICS" in com or "VECTORIAL DRAWING" in com: tts_kill() userin.define(["inkscape"], "Inkscape") userin.execute(0) userin.say("Inkscape") elif "VIDEO EDITOR" in com: tts_kill() #userin.define(["openshot"],"Openshot") #userin.execute(0) #userin.define(["lightworks"],"Lightworks") #userin.execute(0) userin.define(["kdenlive"], "Kdenlive") userin.execute(0) userin.say("Video editor") elif "OPEN CAMERA" == com: tts_kill() userin.define(["kamoso"], "Camera") # KDE neon userin.execute(0) userin.define(["snap-photobooth"], "Camera") # elementary OS userin.execute(0) userin.define(["cheese"], "Camera") # Ubuntu userin.execute(0) userin.say("Camera") elif "OPEN CALENDAR" == com: tts_kill() userin.define(["korganizer"], "Calendar") # KDE neon userin.execute(0) userin.define(["maya-calendar"], "Calendar") # elementary OS userin.execute(0) userin.define(["orage"], "Calendar") # Ubuntu userin.execute(0) userin.say("Calendar") elif "OPEN CALCULATOR" == com: tts_kill() userin.define(["kcalc"], "Calculator") # KDE neon userin.execute(0) userin.define(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(0) userin.define(["gnome-calculator"], "Calculator") # Ubuntu userin.execute(0) userin.say("Calculator") elif "OPEN STEAM" == com: tts_kill() userin.define(["steam"], "Steam") userin.execute(0) userin.say("Steam Game Store") elif "SOFTWARE CENTER" in com: tts_kill() userin.define(["plasma-discover"], "Software Center") # KDE neon userin.execute(0) userin.define(["software-center"], "Software Center") # elementary OS & Ubuntu userin.execute(0) userin.say("Software Center") elif "OFFICE SUITE" in com: tts_kill() userin.define(["libreoffice"], "LibreOffice") userin.execute(0) userin.say("Office Suite") elif "OPEN WRITER" == com: tts_kill() userin.define(["libreoffice", "--writer"], "LibreOffice Writer") userin.execute(0) userin.say("Writer") elif "OPEN MATH" == com: tts_kill() userin.define(["libreoffice", "--math"], "LibreOffice Math") userin.execute(0) userin.say("Math") elif "OPEN IMPRESS" == com: tts_kill() userin.define(["libreoffice", "--impress"], "LibreOffice Impress") userin.execute(0) userin.say("Impress") elif "OPEN DRAW" == com: tts_kill() userin.define(["libreoffice", "--draw"], "LibreOffice Draw") userin.execute(0) userin.say("Draw") elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif com == "NEW TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) elif com == "SWITCH TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) elif com in ("CLOSE", "ESCAPE"): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) elif com == "GO BACK": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) elif com == "GO FORWARD": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) elif com == "SCROLL LEFT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) elif com == "SCROLL RIGHT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) elif com == "SCROLL UP": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) elif com == "SCROLL DOWN": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) elif com in ("PLAY", "PAUSE", "SPACEBAR"): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") elif "SHUTDOWN THE COMPUTER" == com: tts_kill() userin.define(["sudo", "poweroff"], "Shutting down") userin.say("Shutting down") userin.execute(3) elif com in ("GOODBYE", "BYE BYE", "SEE YOU LATER"): tts_kill() userin.define([" "], " ") userin.say("Goodbye, " + user_prefix) #raise KeyboardInterrupt thread.interrupt_main() elif "WIKIPEDIA" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? WIKIPEDIA", com) if capture: search_query = capture.group('query') try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.define(["sensible-browser", wikipage.url], search_query) userin.execute(0) userin.say(wikicontent) except requests.exceptions.ConnectionError: userin.define([" "], "Wikipedia connection error.") userin.execute(0) userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[ 0] message = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[ 0] for option in disambiguation.options[1:3]: message += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.define([" "], notify) userin.execute(0) userin.say(message) except: pass elif "YOUTUBE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? YOUTUBE", com) if capture: search_query = capture.group('query') info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.define(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin.define(" ", " ") userin.execute(0) userin.say(youtube_title) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') elif ("GOOGLE" in com or "WEB" in com) and "IMAGE" not in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? (?:GOOGLE|WEB)?", com) if capture: search_query = capture.group('query') tab_url = "http://google.com/?#q=" + search_query userin.define(["sensible-browser", tab_url], search_query) userin.execute(0) userin.say(search_query) elif ("GOOGLE" in com or "WEB" in com) and "IMAGE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH IMAGES OF|FIND IMAGES OF|SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? (?:GOOGLE|WEB|GOOGLE IMAGES|WEB IMAGES)?", com) if capture: search_query = capture.group('query') tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" userin.define(["sensible-browser", tab_url], search_query) userin.execute(0) userin.say(search_query) else: tts_kill() learn_response = learn_.respond(com) if learn_response: userin.define([" "], " ") userin.say(learn_response) else: omniscient_.respond(original_com, user_prefix, not args["silent"], userin)
k=PyKeyboard() m=PyMouse() browser = webdriver.Firefox() # Opening Firefox with its Homepage browser.maximize_window() browser.get('http://www.fakemailgenerator.com/#/superrito.com/monkeydluffy13/') # Open the fake mail generator in TAB1 time.sleep(2) k.press_key(k.control_key) k.tap_key('t') k.release_key(k.control_key) # Getting an element where Keys can be passed # Opening a NEW TAB browser.get('http://hck.re/qcKgO4') m.click(598L,798L,1,1) k.type_string('*****@*****.**') k.tap_key(k.return_key)
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_()
def command(com, args, tw_user=None): global e if (e.is_set()): # System Tray Icon exit must trigger this exit(0) if not com.strip() or not isinstance(com, str): return False global inactive global user_full_name global user_prefix global config_file userin.twitter_user = tw_user print("You: " + com.upper()) doc = nlp(com) h = Helper(doc) if args["verbose"]: if len(doc) > 0: print("") print("{:12} {:12} {:12} {:12} {:12} {:12} {:12} {:12}". format("TEXT", "LEMMA", "POS", "TAG", "DEP", "SHAPE", "ALPHA", "STOP")) for token in doc: print( "{:12} {:12} {:12} {:12} {:12} {:12} {:12} {:12}" .format(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.shape_, str(token.is_alpha), str(token.is_stop))) print("") if len(list(doc.noun_chunks)) > 0: print("{:12} {:12} {:12} {:12}".format( "TEXT", "ROOT.TEXT", "ROOT.DEP_", "ROOT.HEAD.TEXT")) for chunk in doc.noun_chunks: print("{:12} {:12} {:12} {:12}".format( chunk.text, chunk.root.text, chunk.root.dep_, chunk.root.head.text)) print("") if inactive and not ( h.directly_equal(["dragonfire", "hey"]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or (h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2))): return True if USER_ANSWERING['status']: if com.startswith("FIRST") or com.startswith( "THE FIRST") or com.startswith("SECOND") or com.startswith( "THE SECOND") or com.startswith( "THIRD") or com.startswith("THE THIRD"): USER_ANSWERING['status'] = False selection = None if com.startswith("FIRST") or com.startswith("THE FIRST"): selection = 0 elif com.startswith("SECOND") or com.startswith("THE SECOND"): selection = 1 elif com.startswith("THIRD") or com.startswith("THE THIRD"): selection = 2 if USER_ANSWERING['for'] == 'wikipedia': with nostderr(): search_query = USER_ANSWERING['options'][selection] try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) return True except Exception: return True if h.directly_equal([ "dragonfire", "hey" ]) or (h.check_verb_lemma("wake") and h.check_nth_lemma(-1, "up")) or ( h.check_nth_lemma(0, "dragon") and h.check_nth_lemma(1, "fire") and h.max_word_count(2)): inactive = False userin.say( choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix + ", tell me your wish." ])) return True if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or ( h.check_verb_lemma("stop") and h.check_verb_lemma("listen")): inactive = True userin.execute([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.say("I'm going to sleep") return True if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut") and h.check_nth_lemma(-1, "up")): tts_kill() print("Dragonfire quiets.") return True if h.check_wh_lemma("what") and h.check_deps_contains("your name"): userin.execute([" "], "My name is Dragonfire.", True) return True if h.check_wh_lemma("what") and h.check_deps_contains("your gender"): userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") return True if (h.check_wh_lemma("who") and h.check_text("I")) or (h.check_verb_lemma("say") and h.check_text("my") and check_lemma("name")): userin.execute([" "], user_full_name) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") return True if h.check_verb_lemma("open") or h.check_adj_lemma( "open") or h.check_verb_lemma("run") or h.check_verb_lemma( "start") or h.check_verb_lemma("show"): if h.check_text("blender"): userin.execute(["blender"], "Blender") userin.say("Blender 3D computer graphics software") return True if h.check_text("draw"): userin.execute(["libreoffice", "--draw"], "LibreOffice Draw") userin.say("Opening LibreOffice Draw") return True if h.check_text("impress"): userin.execute(["libreoffice", "--impress"], "LibreOffice Impress") userin.say("Opening LibreOffice Impress") return True if h.check_text("math"): userin.execute(["libreoffice", "--math"], "LibreOffice Math") userin.say("Opening LibreOffice Math") return True if h.check_text("writer"): userin.execute(["libreoffice", "--writer"], "LibreOffice Writer") userin.say("Opening LibreOffice Writer") return True if h.check_text("gimp") or (h.check_noun_lemma("photo") and (h.check_noun_lemma("editor") or h.check_noun_lemma("shop"))): userin.execute(["gimp"], "GIMP") userin.say("Opening the photo editor software.") return True if h.check_text("inkscape") or (h.check_noun_lemma("vector") and h.check_noun_lemma("graphic") ) or (h.check_text("vectorial") and h.check_text("drawing")): userin.execute(["inkscape"], "Inkscape") userin.say("Opening the vectorial drawing software.") return True if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") userin.say("Opening LibreOffice") return True if h.check_text("kdenlive") or (h.check_noun_lemma("video") and h.check_noun_lemma("editor")): userin.execute(["kdenlive"], "Kdenlive") userin.say("Opening the video editor software.") return True if h.check_noun_lemma("browser") or h.check_noun_lemma( "chrome") or h.check_text("firefox"): userin.execute(["sensible-browser"], "Web Browser") userin.say("Web browser") return True if h.check_text("steam"): userin.execute(["steam"], "Steam") userin.say("Opening Steam Game Store") return True if h.check_text("files") or (h.check_noun_lemma("file") and h.check_noun_lemma("manager")): userin.execute(["dolphin"], "File Manager") # KDE neon userin.execute(["pantheon-files"], "File Manager") # elementary OS userin.execute(["nautilus", "--browser"], "File Manager") # Ubuntu userin.say("File Manager") return True if h.check_noun_lemma("camera"): userin.execute(["kamoso"], "Camera") # KDE neon userin.execute(["snap-photobooth"], "Camera") # elementary OS userin.execute(["cheese"], "Camera") # Ubuntu userin.say("Camera") return True if h.check_noun_lemma("calendar"): userin.execute(["korganizer"], "Calendar") # KDE neon userin.execute(["maya-calendar"], "Calendar") # elementary OS userin.execute(["orage"], "Calendar") # Ubuntu userin.say("Calendar") return True if h.check_noun_lemma("calculator"): userin.execute(["kcalc"], "Calculator") # KDE neon userin.execute(["pantheon-calculator"], "Calculator") # elementary OS userin.execute(["gnome-calculator"], "Calculator") # Ubuntu userin.say("Calculator") return True if h.check_noun_lemma("software") and h.check_text("center"): userin.execute(["plasma-discover"], "Software Center") # KDE neon userin.execute(["software-center"], "Software Center") # elementary OS & Ubuntu userin.say("Software Center") return True if h.check_lemma("be") and h.check_lemma("-PRON-") and ( h.check_lemma("lady") or h.check_lemma("woman") or h.check_lemma("girl")): config_file.update({'gender': 'female'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "My Lady" userin.say("Pardon, " + user_prefix + ".") return True if h.check_lemma("be") and h.check_lemma("-PRON-") and ( h.check_lemma("sir") or h.check_lemma("man") or h.check_lemma("boy")): config_file.update({'gender': 'male'}, Query().datatype == 'gender') config_file.remove(Query().datatype == 'callme') user_prefix = "Sir" userin.say("Pardon, " + user_prefix + ".") return True if h.check_lemma("call") and h.check_lemma("-PRON-"): title = "" for token in doc: if token.pos_ == "NOUN": title += ' ' + token.text title = title.strip() callme_config = config_file.search(Query().datatype == 'callme') if callme_config: config_file.update({'title': title}, Query().datatype == 'callme') else: config_file.insert({'datatype': 'callme', 'title': title}) user_prefix = title userin.say("OK, " + user_prefix + ".") return True # only for The United States today but prepared for all countries. Also # only for celsius degrees today. --> by Radan Liska :-) if h.is_wh_question() and h.check_lemma("temperature"): city = "" for ent in doc.ents: if ent.label_ == "GPE": city += ' ' + ent.text city = city.strip() if city: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") reg = owm.city_id_registry() weather = owm.weather_at_id( reg.ids_for(city)[0][0]).get_weather() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format(city, weather.get_temperature('celsius')['temp']) userin.execute([" "], msg) userin.say(msg) return True if h.check_nth_lemma(0, "keyboard") or h.check_nth_lemma(0, "type"): n = len(doc[0].text) + 1 with nostdout(): with nostderr(): k = PyKeyboard() for character in com[n:]: k.tap_key(character) k.tap_key(" ") return True if h.directly_equal(["enter"]) or (h.check_adj_lemma("new") or h.check_noun_lemma("line")): with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) return True if h.check_adj_lemma("new") and h.check_noun_lemma("tab"): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) return True if h.check_verb_lemma("switch") and h.check_noun_lemma("tab"): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) return True if h.directly_equal(["CLOSE", "ESCAPE"]): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) return True if h.check_lemma("back") and h.max_word_count(4): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) return True if h.check_lemma("forward") and h.max_word_count(4): with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) return True if h.check_text("swipe") or h.check_text("scroll"): if h.check_text("left"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) return True if h.check_text("right"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) return True if h.check_text("up"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) return True if h.check_text("down"): with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) return True if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR"]): with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") return True if ((h.check_text("shut") and h.check_text("down")) or (h.check_text("power") and h.check_text("off"))) and h.check_text("computer"): userin.execute(["sudo", "poweroff"], "Shutting down", True, 3) return True if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma( 0, "bye") or (h.check_verb_lemma("see") and h.check_noun_lemma("you") and h.check_noun_lemma("later")): userin.say("Goodbye, " + user_prefix) # raise KeyboardInterrupt thread.interrupt_main() return True if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("wikipedia"): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "wikipedia" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: try: wikiresult = wikipedia.search(search_query) if len(wikiresult) == 0: userin.say( "Sorry, " + user_prefix + ". But I couldn't find anything about " + search_query + " in Wikipedia.") return True wikipage = wikipedia.page(wikiresult[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin.execute(["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) return True except wikipedia.exceptions.DisambiguationError as disambiguation: USER_ANSWERING['status'] = True USER_ANSWERING['for'] = 'wikipedia' USER_ANSWERING['reason'] = 'disambiguation' USER_ANSWERING['options'] = disambiguation.options[:3] notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[ 0] message = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[ 0] for option in disambiguation.options[1:3]: message += ", or " + option notify += "\n - " + option notify += '\nSay, for example: "THE FIRST ONE" to choose.' userin.execute([" "], notify) userin.say(message) return True except BaseException: pass if (h.check_lemma("search") or h.check_lemma("find")) and h.check_lemma("youtube"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "youtube" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin.execute(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin.say(youtube_title) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') return True if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and not h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query userin.execute(["sensible-browser", tab_url], search_query, True) return True if (h.check_lemma("search") or h.check_lemma("find")) and ( h.check_lemma("google") or h.check_lemma("web") or h.check_lemma("internet")) and h.check_lemma("image"): with nostdout(): with nostderr(): search_query = "" for token in doc: if not (token.lemma_ == "search" or token.lemma_ == "find" or token.lemma_ == "google" or token.lemma_ == "web" or token.lemma_ == "internet" or token.lemma_ == "image" or token.is_stop): search_query += ' ' + token.text search_query = search_query.strip() if search_query: tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch" userin.execute(["sensible-browser", tab_url], search_query, True) return True arithmetic_response = arithmetic_parse(com) if arithmetic_response: userin.say(arithmetic_response) else: learnerresponse = learner.respond(com) if learnerresponse: userin.say(learnerresponse) else: if not omniscient.respond(com, not args["silent"], userin, user_prefix, args["server"]): dc_response = dc.respond(com, user_prefix) if dc_response: userin.say(dc_response)
exit(0) ### while True: if time.time() - cur_end_time >= 24*60*60 + 600: semi_automatic_machine() down = False while not down: time.sleep(300) cksum = get_crop_sum(538, 171, 73, 19) down = cksum == 941148 cksum = get_crop_sum(5, 107, 80, 53) # crash... if cksum == 2946800: m.click(293, 492, 1) time.sleep(1) k.tap_key(k.function_keys[5]) k.press_key(k.control_key) k.tap_key(k.tab_key) k.release_key(k.control_key) time.sleep(3) k.tap_key(k.function_keys[5]) time.sleep(3) k.press_key(k.control_key) k.tap_key(k.tab_key) k.release_key(k.control_key) # cur_end_time = time.time() close_and_swap() last_end_time, cur_end_time = cur_end_time, last_end_time
def command(speech): #here = os.path.dirname(os.path.realpath(__file__)) #Popen(["./gradlew","web","-q"], stdout=FNULL, stderr=FNULL) #os.chdir(here) ''' kernel = aiml.Kernel() with nostdout(): with nostderr(): kernel.learn(DRAGONFIRE_PATH + "/aiml/learn.aiml") ''' previous_command = "" global inactive global user_full_name global user_prefix global config_file while (True): line = speech.readline() if line.startswith("sentence1: ") or line.startswith( "<search failed>"): com = google_speech_api() if com == "\n" or com == " ": com = "Enter" original_com = com if (com == 0): #speech_error() continue com = com.upper() print "You: " + com if inactive == 1 and "DRAGONFIRE" != com and "DRAGON FIRE" != com and "WAKE UP" != com and com != "HEY": continue if "DRAGONFIRE" == com or "DRAGON FIRE" == com or "WAKE UP" == com or com == "HEY": tts_kill() inactive = 0 userin = Data([" "], " ") words_dragonfire = { 0: "Yes, " + user_prefix + ".", 1: "Yes. I'm waiting.", 2: "What is your orders?" } userin.say(words_dragonfire[randint(0, 2)]) elif "GO TO SLEEP" == com: tts_kill() inactive = 1 userin = Data([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.say("I'm going to sleep") userin.interact(0) previous_command = com elif "ENOUGH" == com or "SHUT UP" == com: print "Dragonfire quiets." tts_kill() elif "WHO AM I" == com or "SAY MY NAME" == com: tts_kill() userin = Data([" "], user_full_name) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") userin.interact(0) previous_command = com elif "MY TITLE IS LADY" == com or "I'M A LADY" == com or "I'M A WOMAN" == com or "I'M A GIRL" == com: tts_kill() config_file.update({'gender': 'female'}, Query().datatype == 'gender') user_prefix = "My Lady" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "MY TITLE IS SIR" == com or "I'M A MAN" == com or "I'M A BOY" == com: tts_kill() config_file.update({'gender': 'male'}, Query().datatype == 'gender') user_prefix = "Sir" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com.startswith("CALL ME "): tts_kill() callme_config = config_file.search( Query().datatype == 'callme') if callme_config: config_file.update({'title': original_com[8:].lower()}, Query().datatype == 'callme') else: config_file.insert({ 'datatype': 'callme', 'title': original_com[8:].lower() }) user_prefix = original_com[8:].lower().encode("utf8") userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "WHAT IS YOUR NAME" == com: tts_kill() userin = Data([" "], "My name is Dragonfire.") userin.say("My name is Dragon Fire.") userin.interact(0) previous_command = com elif "WHAT IS YOUR GENDER" == com: tts_kill() userin = Data([" "], " ") userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") previous_command = com elif "FILE MANAGER" in com or "OPEN FILES" == com: tts_kill() userin = Data(["dolphin"], "File Manager") # KDE neon userin.interact(0) userin = Data(["pantheon-files"], "File Manager") # elementary OS userin.interact(0) userin = Data(["nautilus", "--browser"], "File Manager") # Ubuntu userin.say("File Manager") userin.interact(0) previous_command = com elif "WEB BROWSER" in com: tts_kill() userin = Data(["sensible-browser"], "Web Browser") userin.say("Web Browser") userin.interact(0) previous_command = com elif "OPEN BLENDER" == com: tts_kill() userin = Data(["blender"], "Blender") userin.say("Blender 3D computer graphics software") userin.interact(0) previous_command = com elif "PHOTO SHOP" in com or "PHOTO EDITOR" in com or "GIMP" in com: tts_kill() userin = Data(["gimp"], "GIMP") userin.say("Photo editor") userin.interact(0) previous_command = com elif "INKSCAPE" in com or "VECTOR GRAPHICS" in com or "VECTORIAL DRAWING" in com: tts_kill() userin = Data(["inkscape"], "Inkscape") userin.say("Inkscape") userin.interact(0) previous_command = com elif "VIDEO EDITOR" in com: tts_kill() #userin = Data(["openshot"],"Openshot") #userin.interact(0) #userin = Data(["lightworks"],"Lightworks") #userin.interact(0) userin = Data(["kdenlive"], "Kdenlive") userin.say("Video editor") userin.interact(0) previous_command = com elif "OPEN CAMERA" == com: tts_kill() userin = Data(["kamoso"], "Camera") # KDE neon userin.interact(0) userin = Data(["snap-photobooth"], "Camera") # elementary OS userin.interact(0) userin = Data(["cheese"], "Camera") # Ubuntu userin.say("Camera") userin.interact(0) previous_command = com elif "OPEN CALENDAR" == com: tts_kill() userin = Data(["korganizer"], "Calendar") # KDE neon userin.interact(0) userin = Data(["maya-calendar"], "Calendar") # elementary OS userin.interact(0) userin = Data(["orage"], "Calendar") # Ubuntu userin.say("Calendar") userin.interact(0) previous_command = com elif "OPEN CALCULATOR" == com: tts_kill() userin = Data(["kcalc"], "Calculator") # KDE neon userin.interact(0) userin = Data(["pantheon-calculator"], "Calculator") # elementary OS userin.interact(0) userin = Data(["gnome-calculator"], "Calculator") # Ubuntu userin.say("Calculator") userin.interact(0) previous_command = com elif "OPEN STEAM" == com: tts_kill() userin = Data(["steam"], "Steam") userin.say("Steam Game Store") userin.interact(0) previous_command = com elif "SOFTWARE CENTER" in com: tts_kill() userin = Data(["plasma-discover"], "Software Center") # KDE neon userin.interact(0) userin = Data(["software-center"], "Software Center") # elementary OS & Ubuntu userin.say("Software Center") userin.interact(0) previous_command = com elif "OFFICE SUITE" in com: tts_kill() userin = Data(["libreoffice"], "LibreOffice") userin.say("Office Suite") userin.interact(0) previous_command = com elif "OPEN WRITER" == com: tts_kill() userin = Data(["libreoffice", "--writer"], "LibreOffice Writer") userin.say("Writer") userin.interact(0) previous_command = com elif "OPEN MATH" == com: tts_kill() userin = Data(["libreoffice", "--math"], "LibreOffice Math") userin.say("Math") userin.interact(0) previous_command = com elif "OPEN IMPRESS" == com: tts_kill() userin = Data(["libreoffice", "--impress"], "LibreOffice Impress") userin.say("Impress") userin.interact(0) previous_command = com elif "OPEN DRAW" == com: tts_kill() userin = Data(["libreoffice", "--draw"], "LibreOffice Draw") userin.say("Draw") userin.interact(0) previous_command = com elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif com == "NEW TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) elif com == "SWITCH TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) elif com == "CLOSE" or com == "ESCAPE": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) elif com == "GO BACK": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) elif com == "GO FORWARD": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) elif com == "SCROLL LEFT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) elif com == "SCROLL RIGHT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) elif com == "SCROLL UP": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) elif com == "SCROLL DOWN": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) elif com == "PLAY" or com == "PAUSE" or com == "SPACEBAR": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") elif "SHUTDOWN THE COMPUTER" == com: tts_kill() userin = Data(["sudo", "poweroff"], "Shutting down") userin.say("Shutting down") userin.interact(3) previous_command = com elif com == "GOODBYE" or com == "BYE BYE" or com == "SEE YOU LATER": tts_kill() userin = Data([" "], " ") userin.say("Goodbye, " + user_prefix) previous_command = com julius_proc.terminate() with nostdout(): with nostderr(): try: os.system('rm -f /tmp/' + str(datetime.date.today().year) + '*.[Ww][Aa][Vv]') except: pass sys.exit(1) elif "WIKIPEDIA" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? WIKIPEDIA", com) if capture: search_query = capture.group('query') try: wikipage = wikipedia.page( wikipedia.search(search_query)[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin = Data( ["sensible-browser", wikipage.url], search_query) userin.interact(0) userin.say(wikicontent) previous_command = com except: pass elif "YOUTUBE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? YOUTUBE", com) if capture: search_query = capture.group('query') info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin = Data( ["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin = Data(" ", " ") userin.say(youtube_title) userin.interact(0) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') else: tts_kill() #dragonfire_respond = kernel.respond(com) aiml_respond = learn_.respond(com) if aiml_respond: userin = Data([" "], " ") userin.say(aiml_respond) else: omniscient_.respond(original_com) previous_command = com
//分别定义一个实例 m = PyMouse() k = PyKeyboard() 鼠标操作: m.click(x,y,button,n) –鼠标点击 x,y –是坐标位置 buttong –1表示左键,2表示点击右键 n –点击次数,默认是1次,2表示双击 m.move(x,y) –鼠标移动到坐标(x,y) x_dim, y_dim = m.screen_size() –获得屏幕尺寸 键盘操作: k.type_string(‘Hello, World!’) –模拟键盘输入字符串 k.press_key(‘H’) –模拟键盘按H键 k.release_key(‘H’) –模拟键盘松开H键 k.tap_key(“H”) –模拟点击H键 k.tap_key(‘H’,n=2,interval=5) –模拟点击H键,2次,每次间隔5秒 k.tap_key(k.function_keys[5]) –点击功能键F5 k.tap_key(k.numpad_keys[5],3) –点击小键盘5,3次 联合按键模拟 例如同时按alt+tab键盘 k.press_key(k.alt_key) –按住alt键 k.tap_key(k.tab_key) –点击tab键 k.release_key(k.alt_key) –松开alt键
socket.SOCK_DGRAM) # UDP sock.bind((UDP_IP, UDP_PORT)) m = PyMouse() k = PyKeyboard() while True: data, addr = sock.recvfrom(128) # buffer size is 100 bytes splitdata = data.split(",") cm = splitdata[0] if cm == "1": #move print "moving mouse:" x = splitdata[1] y = splitdata[2] pos = m.position() m.move(pos[0] + int(x), pos[1] + int(y)) if cm == "2": #tap pos = m.position() m.click(pos[0], pos[1], 1, 1) # x,y,button number, times to be clicked if cm == "3": #double tap pos = m.position() m.click(pos[0], pos[1], 1, 2) if cm == "4": #double tap key = splitdata[1] if key == "backspace_key": k.tap_key(k.backspace_key) else: k.tap_key(key) print "received message:", data
from pykeyboard import PyKeyboard from subprocess import Popen k = PyKeyboard() p = Popen("/usr/bin/stella") output, error = p.communicate() print("here") k.tap_key(k.enter_key)
associate_flag = int(sys.argv[1]) RUNNING_TIME = int(sys.argv[2]) AI_BOTS_DIR = sys.argv[3] RESULT_DIR = sys.argv[4] BIND_CPU = int(sys.argv[5]) HUMAN_RUN = int(sys.argv[6]) Reso_Width = int(sys.argv[7]) Reso_Hight = int(sys.argv[8]) MultipleMode = int(sys.argv[9]) #terminalFocus=[200,200,200,200] terminalFocus = [980, 980, 540, 540] keyboard_action.mouse_click(terminalFocus) time.sleep(1) key_board = PyKeyboard() key_board.type_string('cd $CGR_BENCHMARK_PATH/') key_board.tap_key(key_board.enter_key) key_board.type_string('./collectData.sh supertuxkart-1 ' + str(associate_flag) + ' ' + str(RUNNING_TIME) + ' ' + str(BIND_CPU) + ' ' + str(HUMAN_RUN) + ' ' + str(MultipleMode) + ' &') key_board.tap_key(key_board.enter_key) time.sleep(5) key_board.tap_key(key_board.enter_key) time.sleep(3) RUNNING_TIME -= 30 #frequently modified parameters logs_path = AI_BOTS_DIR + '/training_scripts/AI-models/supertuxkart-1/lstm-logs/' n_input = 1 # sequential input vector numbers for lstm tensor_size = 6 # length of each input vector n_classes = 3 # number of classes for the output
def upload(self, file_path): self.click() time.sleep(5) kk = PyKeyboard() kk.type_string(file_path) kk.tap_key(kk.enter_key)
def command(speech): #here = os.path.dirname(os.path.realpath(__file__)) #os.chdir(os.path.expanduser("~/yodaqa/")) #Popen(["./gradlew","web","-q"], stdout=FNULL, stderr=FNULL) #os.chdir(here) ''' kernel = aiml.Kernel() with nostdout(): with nostderr(): kernel.learn(DRAGONFIRE_PATH + "/aiml/learn.aiml") ''' previous_command = "" global inactive global user_full_name global user_prefix while (True): line = speech.readline() if line.startswith("sentence1: ") or line.startswith( "<search failed>"): com = google_speech_api() if com == "\n" or com == " ": com = "Enter" original_com = com if (com == 0): #speech_error() continue com = com.upper() print "You: " + com if inactive == 1 and "DRAGON FIRE" not in com and "WAKE UP" not in com and com != "HEY": continue if "DRAGON FIRE" in com or "WAKE UP" in com or com == "HEY": tts_kill() inactive = 0 userin = Data([" "], " ") words_dragonfire = { 0: "Yes, " + user_prefix + ".", 1: "Yes. I'm waiting.", 2: "What is your orders?" } userin.say(words_dragonfire[randint(0, 2)]) elif "GO TO SLEEP" in com: tts_kill() inactive = 1 userin = Data([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.say("I'm going to sleep") userin.interact(0) previous_command = com elif "ENOUGH" in com: print "Dragonfire quiets." tts_kill() elif "WHO AM I" in com or "SAY MY NAME" in com: tts_kill() userin = Data(["echo"], user_full_name) userin.say("Your name is " + user_full_name + "," + user_prefix + ".") userin.interact(0) previous_command = com elif "MY TITLE IS LADY" in com or "I'M A LADY" in com or "I'M A WOMAN" in com or "I'M A GIRL" in com: tts_kill() user_prefix = "My Lady" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "MY TITLE IS SIR" in com or "I'M A MAN" in com or "I'M A BOY" in com: tts_kill() user_prefix = "Sir" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "WHAT IS YOUR NAME" in com: tts_kill() userin = Data([" "], " ") userin.say("My name is Dragon Fire.") previous_command = com elif "WHAT IS YOUR GENDER" in com: tts_kill() userin = Data([" "], " ") userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") previous_command = com elif "FILE MANAGER" in com or "OPEN FILES" in com: tts_kill() userin = Data(["pantheon-files"], "File Manager") userin.interact(0) userin = Data(["nautilus", "--browser"], "File Manager") userin.say("File Manager") userin.interact(0) previous_command = com elif "WEB BROWSER" in com: tts_kill() userin = Data(["sensible-browser"], "Web Browser") userin.say("Web Browser") userin.interact(0) previous_command = com elif "OPEN BLENDER" in com: tts_kill() userin = Data(["blender"], "Blender") userin.say("Blender 3D computer graphics software") userin.interact(0) previous_command = com elif "PHOTO SHOP" in com or "PHOTO EDITOR" in com: tts_kill() userin = Data(["gimp"], "GIMP") userin.say("Photo editor") userin.interact(0) previous_command = com elif "INKSCAPE" in com: tts_kill() userin = Data(["inkscape"], "Inkscape") userin.say("Inkscape") userin.interact(0) previous_command = com elif "VIDEO EDITOR" in com: tts_kill() userin = Data(["kdenlive"], "Kdenlive") userin.say("Video editor") userin.interact(0) previous_command = com elif "OPEN CAMERA" in com: tts_kill() userin = Data(["snap-photobooth"], "Camera") userin.interact(0) userin = Data(["cheese"], "Camera") userin.say("Camera") userin.interact(0) previous_command = com elif "OPEN CALENDAR" in com: tts_kill() userin = Data(["maya-calendar"], "Calendar") userin.interact(0) userin = Data(["orage"], "Calendar") userin.say("Calendar") userin.interact(0) previous_command = com elif "OPEN CALCULATOR" in com: tts_kill() userin = Data(["pantheon-calculator"], "Calculator") userin.interact(0) userin = Data(["gnome-calculator"], "Calculator") userin.say("Calculator") userin.interact(0) previous_command = com elif "OPEN STEAM" in com: tts_kill() userin = Data(["steam"], "Steam") userin.say("Steam Game Store") userin.interact(0) previous_command = com elif "SOFTWARE CENTER" in com: tts_kill() userin = Data(["software-center"], "Software Center") userin.say("Software Center") userin.interact(0) previous_command = com elif "OFFICE SUITE" in com: tts_kill() userin = Data(["libreoffice"], "LibreOffice") userin.say("Office Suite") userin.interact(0) previous_command = com elif "OPEN WRITER" in com: tts_kill() userin = Data(["libreoffice", "--writer"], "LibreOffice Writer") userin.say("Writer") userin.interact(0) previous_command = com elif "OPEN MATH" in com: tts_kill() userin = Data(["libreoffice", "--math"], "LibreOffice Math") userin.say("Math") userin.interact(0) previous_command = com elif "OPEN IMPRESS" in com: tts_kill() userin = Data(["libreoffice", "--impress"], "LibreOffice Impress") userin.say("Impress") userin.interact(0) previous_command = com elif "OPEN DRAW" in com: tts_kill() userin = Data(["libreoffice", "--draw"], "LibreOffice Draw") userin.say("Draw") userin.interact(0) previous_command = com elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif "SHUTDOWN THE COMPUTER" in com: tts_kill() userin = Data(["sudo", "poweroff"], "Shutting down") userin.say("Shutting down") userin.interact(3) previous_command = com elif com == "GOODBYE" or com == "BYE BYE" or com == "SEE YOU LATER": tts_kill() userin = Data([" "], " ") userin.say("Goodbye, " + user_prefix) previous_command = com julius_proc.terminate() try: os.system('rm /tmp/' + str(datetime.date.today().year) + '*.[Ww][Aa][Vv]') except: pass sys.exit(1) elif "WIKIPEDIA" in com and "SEARCH" in com: tts_kill() with nostdout(): with nostderr(): search_query = com.replace("SEARCH ", "").replace( " SEARCH", "").replace(" IN WIKIPEDIA", "").replace( "IN WIKIPEDIA ", "").replace(" ON WIKIPEDIA", "").replace( "ON WIKIPEDIA ", "").replace( " USING WIKIPEDIA", "").replace( "USING WIKIPEDIA ", "").replace( " WIKIPEDIA", "").replace("WIKIPEDIA ", "") userin = Data([ "sensible-browser", "http://en.wikipedia.org/wiki/" + search_query.lower() ], search_query) userin.interact(0) wikicontent = "" try: wikipage = wikipedia.page(search_query) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) except: pass userin.say(wikicontent) previous_command = com elif "YOUTUBE" in com and "SEARCH" in com: tts_kill() with nostdout(): with nostderr(): search_query = com.replace("SEARCH ", "").replace( " SEARCH", "").replace(" IN YOUTUBE", "").replace( "IN YOUTUBE ", "").replace(" ON YOUTUBE", "").replace( "ON YOUTUBE ", "").replace(" USING YOUTUBE", "").replace( "USING YOUTUBE ", "").replace(" YOUTUBE", "").replace( "YOUTUBE ", "") DEVELOPER_KEY = "AIzaSyAcwHj2qzI7KWDUN4RkBTX8Y4lrU78lncA" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.search().list( q=search_query, part="id,snippet", maxResults=1).execute() videos = [] channels = [] playlists = [] # Add each result to the appropriate list, and then display the lists of # matching videos, channels, and playlists. for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": #videos.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["videoId"])) videos.append( search_result["snippet"]["title"]) videos.append(search_result["id"]["videoId"]) elif search_result["id"][ "kind"] == "youtube#channel": channels.append( "%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["channelId"])) elif search_result["id"][ "kind"] == "youtube#playlist": playlists.append( "%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["playlistId"])) if len(videos) > 1: youtube_title = videos[0] youtube_url = "https://www.youtube.com/watch?v=%s" % ( videos[1]) userin = Data(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin = Data("", youtube_title) k = PyKeyboard() k.tap_key('space') userin.say(youtube_title) userin.interact(0) time.sleep(3) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') else: tts_kill() #dragonfire_respond = kernel.respond(com) teachable_respond = Teachable.respond( "http://teach.dragon.computer/", CONVO_ID, original_com) userin = Data([" "], " ") if teachable_respond and "WHAT" not in teachable_respond and "WHERE" not in teachable_respond and "WHO" not in teachable_respond and "WHEN" not in teachable_respond and "WHICH" not in teachable_respond and "HOW" not in teachable_respond: userin.say(teachable_respond) else: userin.say( "I need to do a brief research on the internet. It may take up to 3 minutes, so please be patient." ) userin.say( YodaQA.answer("http://qa.ailao.eu", original_com, user_prefix)) previous_command = com
import pymouse, pykeyboard, os, sys from pymouse import * from pykeyboard import PyKeyboard import time m = PyMouse() k = PyKeyboard() # m.click(int(297),int(53),int(-1),int(5)) #–鼠标点击 x_dim, y_dim = m.screen_size() # –获得屏幕尺寸 k.tap_key('G') # –模拟点击H键 print("x_dim, y_dim", x_dim, y_dim) m.move(1444, 400) time.sleep(3) m.click(1444, 327, -1, 2) #–鼠标点击 k.type_string('Hello, World!')
from pykeyboard import PyKeyboard import time m = PyMouse() k = PyKeyboard() with open('names.txt') as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line names = [x.strip().lower() for x in content] for name in names: email = name + '@gmail.com' webbrowser.open_new("http://insider.finimize.com/?kid=FKBTC") time.sleep(5) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.type_string(email) k.tap_key(k.tab_key) k.type_string(name) k.tap_key(k.tab_key) k.type_string("smith") k.tap_key(k.enter_key) time.sleep(5) k.press_key(k.control_key) k.tap_key('w') k.release_key(k.control_key)
from selenium import webdriver import time from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from pykeyboard import PyKeyboard driver = webdriver.Chrome() pykey = PyKeyboard() driver.get("chrome://settings/importData") # driver.get("https://www.imooc.com/user/newlogin") # email = driver.find_element_by_name("email") # email.send_keys("18018845546") # password = driver.find_element_by_name("password") # password.send_keys("z54821348123") # driver.find_element_by_class_name("moco-btn").click() # time.sleep(2) # driver.get("https://www.imooc.com/user/setprofile") # time.sleep(2) # driver.find_element_by_class_name("update-avator").click() # driver.find_elements_by_id("uypload").send_keys("输入图片地址") pykey.tap_key(pykey.shift_key) time.sleep(3) pykey.type_string("asddasd") pykey.type_key(pykey.enter_key) time.sleep(3) driver.close()
m = PyMouse() k = PyKeyboard() x_dim , y_dim = m.screen_size() m.click(x_dim/2, y_dim/2) k.type_string('Hello World!') PyKeyboard还有很多种方式来发送键盘键入: # pressing a key k.press_key('H') # which you then follow with a release of the key k.release_key('H') # or you can 'tap' a key which does both k.tap_key('e') # note that that tap_key does support a way of repeating keystrokes with a interval time between each k.tap_key('l',n=2,interval=5) # and you can send a string if needed too k.type_string('o World!') 并且它还支持很多特殊按键: #Create an Alt+Tab combo k.press_key(k.alt_key) k.tap_key(k.tab_key) k.release_key(k.alt_key) k.tap_key(k.function_keys[5]) # Tap F5 k.tap_key(k.numpad_keys['Home']) # Tap 'Home' on the numpad k.tap_key(k.numpad_keys[5], n=3) # Tap 5 on the numpad, thrice 注意,你也可以使用press_keys方法将多个键一起发送(例如,使用某些组合键):
#This python3 program was created for creating automated income in PSP Socom #Simply Run this program at Mission start Page import time from pykeyboard import PyKeyboard k = PyKeyboard() k.tap_key('x') k.press_key('x') time.sleep(0.7) k.release_key('x') for x in range(1, 4): time.sleep(0.2) for i in range(1, 4): k.tap_key('Left') k.press_key('Left') time.sleep(0.3) k.release_key('Left') time.sleep(0.3), i # k.tap_key('Left') # k.press_key('Left') # time.sleep(0.7) # k.release_key('Left') time.sleep(0.1) k.tap_key('x') k.press_key('x') time.sleep(0.7) k.release_key('x') time.sleep(0.2) k.tap_key('Right') k.press_key('Right') time.sleep(0.4) k.release_key('Right')
# now_image.show() for j in range(4): child_image = now_image.crop((6 + 15 * j, 1, 6 + 15 * (j + 1), 28)) image_save.append(child_image) new_vector.append(Identification.identify(child_image)) yanzhenma += new_vector[-1][0] print(yanzhenma) os.remove(name) browser.find_element_by_xpath( "/html/body/form/div/div[3]/dl[1]/dd/input").send_keys(' ') k = PyKeyboard() k.press_key(k.control_key) k.press_key('a') k.tap_key(k.delete_key) k.release_key('a') k.release_key(k.control_key) for i in yonghumin: k.tap_key(i) k.tap_key(k.tab_key) k.tap_key(k.tab_key) for i in mima: k.tap_key(i) k.tap_key(k.tab_key) for i in yanzhenma: k.tap_key(i) k.tap_key(k.enter_key) time.sleep(1) try: if len(browser.page_source) > 5:
from pykeyboard import PyKeyboard from pymouse import PyMouseEvent import os keyboard = PyKeyboard() keyboard.press_key(keyboard.alt_key) keyboard.tap_key(keyboard.tab_key) class Tabwin(PyMouseEvent): def __init__(self): PyMouseEvent.__init__(self) def click(self, x, y, button, press): if button == 1: keyboard.release_key(keyboard.alt_key) self.stop() Tabwin().run()
print " .0@@@@0o.. ..o0@@@@0." time.sleep(0.02) print " .o00@@@@0oo.. ..oo0@@@@00o." time.sleep(0.02) print " .o00@@@@@00oooo.........oooo000@@@@@00o." time.sleep(0.02) print " .oo000@@@@@@@@@@@@@@@@@@@@000oo." time.sleep(0.02) print " ...oooooooooooooo..." for i in range(rows): time.sleep(0.03) print "" k.type_string('htop -d 50') time.sleep(0.1) k.tap_key('Return') time.sleep(0.01) k.press_keys(['Alt_L', '\\']) time.sleep(0.1) k.press_keys(['Alt_L', 'h']) time.sleep(0.01) k.press_keys(['Alt_L', '-']) time.sleep(0.1) k.type_string('mutt') time.sleep(0.01) k.tap_key('Return') time.sleep(0.01) k.press_key('Alt_L') k.tap_key(character='Up', n=8) time.sleep(0.01) k.tap_key('-')
# x, y, radius, move.get_trigger()) m.move(x*3,y*3) pressed, released = move.get_button_events() buttons = move.get_buttons() scrollDirection = 1 if buttons & psmove.Btn_TRIANGLE else -1 m.scroll(scrollDirection * move.get_trigger() / 20) if pressed & psmove.Btn_MOVE: m.click(x*3,y*3) # elif buttons & psmove.Btn_MOVE: # m.drag(x*3,y*3) if pressed & psmove.Btn_SELECT: k.tap_key(k.windows_l_key) if pressed & psmove.Btn_CIRCLE: m.click(x*3,y*3, 2) if pressed & psmove.Btn_CROSS: k.tap_key(k.enter_key) if pressed & psmove.Btn_SQUARE: k.tap_key(k.tab_key) elif status == psmove.Tracker_CALIBRATED: print 'Not currently tracking.' elif status == psmove.Tracker_CALIBRATION_ERROR: print 'Calibration error.'
import pdb from pykeyboard import PyKeyboard fceux = "/usr/bin/fceux" rom = "/home/jreinhart/projects/rbi/roms/RBI-Unlicensed.zip" if __name__ == '__main__': k = PyKeyboard() subprocess.Popen([fceux, rom]) #subprocess.Popen(["pluma"]) time.sleep(1) print("sleep 1") k.tap_key('g') time.sleep(1) print("sleep 1") k.tap_key('g') time.sleep(1) print("sleep 1") k.press_key('g') time.sleep(1) print("sleep 1") k.release_key('g') time.sleep(1) print("sleep 1")