def wheel(ticks): """ Simulates a mouse wheel movement Args: ticks (int) : number of increments to scroll the wheel """ m = PyMouse() m.scroll(ticks)
def force_scroll_screen(self, vertical=-10, ele=None, zoom=1): """ :param element: 目标元素,即想要看到的元素 :param vertical: 每一步垂直滚动的距离, +向上,-向下 """ m = PyMouse() if ele is None: pass else: m.move( ele.location_once_scrolled_into_view.get('x') * zoom, ele.location_once_scrolled_into_view.get('y') * zoom) m.scroll(vertical=vertical) sleep(3)
def scroll_screen(self, element, vertical=-10, ele=None, zoom=1): """ :param element: 目标元素,即想要看到的元素 :param vertical: 每一步垂直滚动的距离, +向上,-向下 """ m = PyMouse() while not element.is_displayed(): x_dim, y_dim = m.screen_size() sleep(3) if ele is None: # m.move(x_dim // 2, y_dim // 2) pass else: m.move( ele.location_once_scrolled_into_view.get('x') * zoom, ele.location_once_scrolled_into_view.get('y') * zoom) m.scroll(vertical=vertical) sleep(3)
def mouse_control(dir_tr): MOVE_DX = 5 # 每次滚动行数 ms = PyMouse() horizontal = 0 vertical = 0 if dir_tr.find("上") != -1: # 向上移动 vertical = MOVE_DX #print("vertical={0}, 向上".format(vertical)) elif dir_tr.find("下") != -1: # 向下移动 vertical = 0 - MOVE_DX #print("vertical={0}, 向下".format(vertical)) elif dir_tr.find("左") != -1: # 向左移动 horizontal = 0 - MOVE_DX #print("horizontal={0}, 向左".format(horizontal)) elif dir_tr.find("右") != -1: # 向右移动 horizontal = MOVE_DX #print("horizontal={0}, 向右".format(horizontal)) #print("horizontal, vertical=[{0},{1}]".format(horizontal, vertical)) # 通过scroll(vertical, horizontal)函数控制页面滚动 # 另外PyMouse还支持模拟move光标,模拟鼠标click,模拟键盘击键等 ms.scroll(vertical, horizontal)
class Scroll: def __init__(self, sb, sink = "0"): self.sb = sb self.mouse = PyMouse() self.moving = False def init(self): self.sb.createParts(1) self.moving = True while self.sb.getPosition() < 0.42 or self.sb.getPosition() > 0.56: time.sleep(1. / 60.) self.moving = False # We specify that we started, so that the thread doesn't stop self.running = True # And we start the thread self.periodic_thread = threading.Thread(target = self.update) self.periodic_thread.start() def stop(self): self.running = False self.sb.removeParts() def keydown(self, event): return def keyup(self, event): return def update(self): while True: if not self.running: return if self.moving: time.sleep(1. / 60.) continue # We compute the distance position = self.sb.getPosition() if position < 0.4: distance = 0.4 - position distance = int(distance * 10.) + 1 self.mouse.scroll(vertical=distance) elif position > 0.6: distance = position - 0.6 distance = int(distance * 10.) + 1 self.mouse.scroll(vertical=-distance) # print("scrolling:", distance) # self.mouse.scroll(vertical=distance) # print(self.sb.getPosition()) # We repeat this process at a 60Hz frequency time.sleep(5. / 60.)
class ControlMainClass(): def __init__(self): self.osName = platform.system() self.pcName = os.environ['computername'] self.screenSize = pyautogui.size() print "Operation system is: ", self.osName print "PC name is: ", self.pcName self.mouseMap = ['left', 'middle', 'right'] self.controller = pyautogui self.pyMouse = PyMouse() self.pyKeyboard = PyKeyboard() self.keyboardMap = {} self.initKeyboardMap() def __del__(self): pass def initKeyboardMap(self): self.keyboardMap[3] = ' ' self.keyboardMap[10] = '\'' self.keyboardMap[15] = ',' self.keyboardMap[16] = '-' self.keyboardMap[17] = '.' self.keyboardMap[18] = '/' self.keyboardMap[19] = '0' self.keyboardMap[20] = '1' self.keyboardMap[21] = '2' self.keyboardMap[22] = '3' self.keyboardMap[23] = '4' self.keyboardMap[24] = '5' self.keyboardMap[25] = '6' self.keyboardMap[26] = '7' self.keyboardMap[27] = '8' self.keyboardMap[28] = '9' self.keyboardMap[30] = ';' self.keyboardMap[32] = '=' self.keyboardMap[36] = '[' self.keyboardMap[37] = '\\' self.keyboardMap[38] = ']' self.keyboardMap[41] = '`' self.keyboardMap[42] = 'a' self.keyboardMap[43] = 'b' self.keyboardMap[44] = 'c' self.keyboardMap[45] = 'd' self.keyboardMap[46] = 'e' self.keyboardMap[47] = 'f' self.keyboardMap[48] = 'g' self.keyboardMap[49] = 'h' self.keyboardMap[50] = 'i' self.keyboardMap[51] = 'j' self.keyboardMap[52] = 'k' self.keyboardMap[53] = 'l' self.keyboardMap[54] = 'm' self.keyboardMap[55] = 'n' self.keyboardMap[56] = 'o' self.keyboardMap[57] = 'p' self.keyboardMap[58] = 'q' self.keyboardMap[59] = 'r' self.keyboardMap[60] = 's' self.keyboardMap[61] = 't' self.keyboardMap[62] = 'u' self.keyboardMap[63] = 'v' self.keyboardMap[64] = 'w' self.keyboardMap[65] = 'x' self.keyboardMap[66] = 'y' self.keyboardMap[67] = 'z' self.keyboardMap[75] = self.pyKeyboard.alt_l_key self.keyboardMap[76] = self.pyKeyboard.alt_r_key self.keyboardMap[78] = self.pyKeyboard.backspace_key self.keyboardMap[90] = self.pyKeyboard.control_l_key self.keyboardMap[91] = self.pyKeyboard.control_r_key self.keyboardMap[93] = self.pyKeyboard.delete_key self.keyboardMap[94] = self.pyKeyboard.delete_key self.keyboardMap[96] = self.pyKeyboard.down_key self.keyboardMap[97] = self.pyKeyboard.end_key self.keyboardMap[98] = self.pyKeyboard.enter_key self.keyboardMap[99] = self.pyKeyboard.escape_key self.keyboardMap[102] = self.pyKeyboard.function_keys[1] self.keyboardMap[103] = self.pyKeyboard.function_keys[10] self.keyboardMap[104] = self.pyKeyboard.function_keys[11] self.keyboardMap[105] = self.pyKeyboard.function_keys[12] self.keyboardMap[113] = self.pyKeyboard.function_keys[2] self.keyboardMap[119] = self.pyKeyboard.function_keys[3] self.keyboardMap[120] = self.pyKeyboard.function_keys[4] self.keyboardMap[121] = self.pyKeyboard.function_keys[5] self.keyboardMap[122] = self.pyKeyboard.function_keys[6] self.keyboardMap[123] = self.pyKeyboard.function_keys[7] self.keyboardMap[124] = self.pyKeyboard.function_keys[8] self.keyboardMap[125] = self.pyKeyboard.function_keys[9] if self.osName=="Windows": self.keyboardMap[129] = self.pyKeyboard.hangul_key elif self.osName=="Linux": self.keyboardMap[129] = -1 pass self.keyboardMap[132] = self.pyKeyboard.home_key self.keyboardMap[141] = self.pyKeyboard.left_key self.keyboardMap[146] = '0' self.keyboardMap[147] = '1' self.keyboardMap[148] = '2' self.keyboardMap[149] = '3' self.keyboardMap[150] = '4' self.keyboardMap[151] = '5' self.keyboardMap[152] = '6' self.keyboardMap[153] = '7' self.keyboardMap[154] = '8' self.keyboardMap[155] = '9' self.keyboardMap[156] = self.pyKeyboard.num_lock_key self.keyboardMap[157] = self.pyKeyboard.page_down_key self.keyboardMap[158] = self.pyKeyboard.page_up_key self.keyboardMap[160] = self.pyKeyboard.page_down_key self.keyboardMap[161] = self.pyKeyboard.page_up_key self.keyboardMap[170] = self.pyKeyboard.right_key self.keyboardMap[171] = self.pyKeyboard.scroll_lock_key self.keyboardMap[175] = self.pyKeyboard.shift_l_key self.keyboardMap[176] = self.pyKeyboard.shift_r_key self.keyboardMap[180] = self.pyKeyboard.tab_key self.keyboardMap[181] = self.pyKeyboard.up_key pass def command(self,data): if data[0] == 'm': x = (ord(data[3])*100)+ord(data[4]) y = (ord(data[5])*100)+ord(data[6]) #print "x:",x," y:",y #self.controller.moveTo( x, y) # x,y self.pyMouse.move(x, y) if data[1] == 'p': #self.controller.mouseDown(x,y,self.mouseMap[ord(data[2])]) # x,y,b #print "press" self.pyMouse.press(x, y,ord(data[2])) pass elif data[1] == 'r' and ord(data[2]) != 0: #self.controller.mouseUp(x,y,self.mouseMap[ord(data[2])]) # x,y,b #print "release" self.pyMouse.release(x, y, ord(data[2])) pass if data[7] == 's': if data[8] == 'u': #self.controller.scroll(10) self.pyMouse.scroll(vertical=10) pass else: #self.controller.scroll(-10) self.pyMouse.scroll(vertical=-10) pass pass else: print 'data: ',data[0],' '+data[1],' ',ord(data[2]) keyCode=ord(data[2]) if data[1]=='p' and keyCode!=0: #self.controller.keyDown(self.controller.KEYBOARD_KEYS[ord(data[2])]) print 'press '+str(ord(data[2])) if self.keyboardMap[ord(data[2])]!=-1: self.pyKeyboard.press_key(self.keyboardMap[ord(data[2])]) pass elif data[1]=='r' and keyCode!=0: #self.controller.keyUp(self.controller.KEYBOARD_KEYS[ord(data[2])]) print 'release '+str(ord(data[2])) if self.keyboardMap[ord(data[2])]!=-1: self.pyKeyboard.release_key(self.keyboardMap[ord(data[2])]) pass pass
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)
class Mouse: m = None def __init__(self, metrics_x=1920, metrics_y=1080): self.m = PyMouse() self.metrics_x = metrics_x self.metrics_y = metrics_y def mouse_to(self, x, y): self.m.move(x, y) def click(self, x, y): self.m.click(x, y, 1, 1) def double_click(self, x, y): self.m.click(x, y, 1, 2) # 发送esc退出事件r def send_esc(self, hwnd): win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_ESCAPE, 0) win32api.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_ESCAPE, 0) #win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_ESCAPE, 0) #win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_ESCAPE, 0) def gun_lun(self, n, m): self.m.scroll(n, m) def vertical_tuo(self, x, y, n): self.m.press(x, y, 1) self.m.move(x, y - n) self.m.release(x, y, 1) def left_down(self): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) # 左键按下 def left_up(self): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) def absolute(self, x, y, dx, dy): SW = self.metrics_x SH = self.metrics_y self.mouse_to(x, y) # 鼠标移动到 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) # 左键按下 i = 1 ddx = int(dx / 10) ddy = int(dy / 10) while i < 10: time.sleep(0.02) self.mouse_to(x + ddx * i, y + ddy * i) # 鼠标移动到 i += 1 self.mouse_to(x + dx, y + dy) # 鼠标移动到 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) return time.sleep(0.5) self.mouse_to(x + 10, y + 10) # 鼠标移动到 time.sleep(0.5) mw = int((dx + x) * 65535 / SW) mh = int((dy + y) * 65535 / SH) win32api.mouse_event( win32con.MOUSEEVENTF_ABSOLUTE + win32con.MOUSEEVENTF_MOVE, mw, mh, 0, 0) time.sleep(0.2) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
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.omniscient.Omniscient.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) if args["verbose"]: userin.pretty_print_nlp_parsing_results(doc) if self.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 "" 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) return userin.say(wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return userin.say("Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers.") except Exception: return False 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)): 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." ])) 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") 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 if h.check_wh_lemma("what") and h.check_deps_contains("your name"): return userin.execute([" "], "My name is Dragonfire.", True) 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 + ".") 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 + ".") 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_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.") 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.") if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") return userin.say("Opening LibreOffice") 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.") 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") 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 return userin.say("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("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") 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" return userin.say("Pardon, " + user_prefix + ".") 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" return userin.say("Pardon, " + user_prefix + ".") 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}) user_prefix = title return userin.say("OK, " + user_prefix + ".") 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) 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" 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 h.check_lemma("back") and h.max_word_count(4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.left_key]) return "back" if h.check_lemma("forward") and h.max_word_count(4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.right_key]) return "forward" 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" 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" 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) 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 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) return userin.say(wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return 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] 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 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]) 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 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) 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: omniscient_response = omniscient.respond(com, not args["silent"], userin, user_prefix, args["server"]) if omniscient_response: return omniscient_response else: dc_response = dc.respond(original_com, user_prefix) if dc_response: return userin.say(dc_response)
# k.tap_key('l',n=2,interval=5) # # and you can send a string if needed too # k.type_string('o World!') def alt_tab(): k.press_key(k.alt_key) k.tap_key(k.tab_key) k.release_key(k.alt_key) time.sleep(1) def alt_f4(): k.press_key(k.alt_key) k.tap_key(k.function_keys[4]) k.release_key(k.alt_key) # k.tap_key('space') # k.tap_key(k.page_down_key) # time.sleep(2) # k.tap_key(k.page_up_key) x_dim, y_dim = m.screen_size() alt_tab() k.tap_key(k.page_up_key) time.sleep(0.5) k.tap_key(k.page_up_key) time.sleep(1) print x_dim, y_dim # m.click(x_dim / 2, y_dim / 2) time.sleep(0.5) m.scroll(vertical=10) time.sleep(0.5) m.scroll(vertical=-10) time.sleep(1)
def compare(self, com, args, testing): """Method to dragonfire's command structures of keyboard keys ability. Args: com (str): User's command. args: Command-line arguments. """ self.testing = testing doc = nlp(com) h = Helper(doc) 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" 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 h.check_lemma("back") and h.max_word_count( 4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.left_key]) return "back" if h.check_lemma("forward") and h.max_word_count( 4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.right_key]) return "forward" 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" 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" return None
mouse = PyMouse() time.sleep(2) # 点击 # def click(self, x, y, button=1, n=1) # x,y表示左边 # button= 1 = left, 2 = right, 3 = middle. # n表示点几次 # 默认左键单击 # mouse.click(38, 34, button=1, n=2) # 滚动 # def scroll(self, vertical=None, horizontal=None, depth=None) # vertical,horizontal # 垂直、水平,应用xy坐标轴定义正负 mouse.scroll(vertical=-1, horizontal=1) # 移动到指定坐标点 # x, y mouse.move(40, 40)
class Mzmouse(PyMouseEvent): """docstring for Mzmouse""" def __init__(self, callback=callfun): super(Mzmouse, self).__init__() self.callback = callback self.mzpymouse = PyMouse() self.mouseevent = [] tm1 = threading.Thread(target=self.clientmove) tm1.setDaemon(True) tm1.start() def click(self, x, y, button, press): # print "click x:%s,y:%s,button:%s,press:%s" % (x, y, button, press) retevent = {} retevent = { "type": "click", "x": x, "y": y, "button": button, "press": press } self.callback(retevent) def move(self, x, y): # print "move x:%s,y:%s" % (x,y) retevent = {} retevent = {"type": "move", "x": x, "y": y} self.callback(retevent) def scroll(self, x, y, vertical, horizontal): # print "scroll x:%s,y:%s,vertical:%s,horizontal:%s" % (x, y, vertical, horizontal) retevent = {} retevent = { "type": "scroll", "x": x, "y": y, "vertical": vertical, "horizontal": horizontal } self.callback(retevent) def clientmove(self, event=False): # print event # event=event.strip("\n") # revents=event.split("\n") # print len(revents) # print revents while True: try: if len(self.mouseevent) > 0: tm1 = threading.Thread(target=self.mousemoveactive, args=[ self.mouseevent.pop(0), ]) tm1.setDaemon(True) tm1.start() # for e in range(0,len(revents),6): # # print e # eventobj=json.loads(revents[e]) # # self.mousemoveactive(eventobj) # tm1=threading.Thread(target=self.mousemoveactive,args=[eventobj,]) # tm1.setDaemon(True) # tm1.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm2=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm2.setDaemon(True) # tm2.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm3=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm3.setDaemon(True) # tm3.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm4=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm4.setDaemon(True) # tm4.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm5=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm5.setDaemon(True) # tm5.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm6=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm6.setDaemon(True) # tm6.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm7=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm7.setDaemon(True) # tm7.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm8=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm8.setDaemon(True) # tm8.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm9=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm9.setDaemon(True) # tm9.start() # if e+1<len(revents): # # self.mousemoveactive(json.loads(revents[e+1])) # tm10=threading.Thread(target=self.mousemoveactive,args=[json.loads(revents[e+1]),]) # tm10.setDaemon(True) # tm10.start() # print eventobj # if eventobj['type']=='move': # # print eventobj # tm=threading.Thread(target=self.mzpymouse.move,args=[int(eventobj['x']),int(eventobj['y'])]) # tm.start() # # self.mzpymouse.move(int(eventobj['x']),int(eventobj['y'])) # elif eventobj['type']=='click': # if eventobj['press']: # self.mzpymouse.press(int(eventobj['x']),int(eventobj['y']),int(eventobj['button'])) # else: # self.mzpymouse.release(int(eventobj['x']),int(eventobj['y']),int(eventobj['button'])) # elif eventobj['type']=='scroll': # self.mzpymouse.scroll( vertical=eventobj['vertical'], horizontal=eventobj['horizontal']) except: print(event) print(traceback.format_exc()) # if eventobj['type']=='move': # self.mzpymouse.move(int(eventobj['x']),int(eventobj['y'])) def mousemoveactive(self, eventobj): if eventobj['type'] == 'move': # print eventobj tm = threading.Thread( target=self.mzpymouse.move, args=[int(eventobj['x']), int(eventobj['y'])]) tm.start() # self.mzpymouse.move(int(eventobj['x']),int(eventobj['y'])) elif eventobj['type'] == 'click': if eventobj['press']: self.mzpymouse.press(int(eventobj['x']), int(eventobj['y']), int(eventobj['button'])) else: self.mzpymouse.release(int(eventobj['x']), int(eventobj['y']), int(eventobj['button'])) elif eventobj['type'] == 'scroll': self.mzpymouse.scroll(vertical=eventobj['vertical'], horizontal=eventobj['horizontal'])
class Image_Frame1(): def __init__(self): self.mouse = PyMouse() self.keyboard = PyKeyboard() self.opencv = ImageMathOpenCV(0.8) #随机坐标 def get_position(self): #找浏览器的窗口 cal_window = uiautomation.WindowControl(searchDepth=1,ClassName = "MozillaWindowClass") if cal_window.Exists(): result = cal_window.BoundingRectangle x = random.randint(100,999) y = random.randint(100,999) return x,y def sele(self): x,y = self.zb("5.png") # print(1) self.mouse.click(x,y) self.keyboard.type_string("java") def open(self): x,y = self.zb("6.png") self.mouse.click(x,y) time.sleep(2) x1,y1 = self.zb("7.png") print(x1,y1) if x1 !=- 1 and y1 !=- 1: print("测试成功") #模拟鼠标左键单击 def left_click(self): x,y = self.get_position() self.mouse.click(x,y,button=1,n=1) print("在位置%d,%d左键点击"%(x,y)) return x,y #鼠标左键双击 #模拟鼠标左键双击 def left_double_click(self): x,y = self.get_position() self.mouse.click(x,y,button=1,n=2) print("在位置%d,%d左键双击"%(x,y)) return x,y #滚动 def top(self,num): self.mouse.scroll(vertical=num) #输入随机字符串 #模拟使用键盘产生随机输入 def input_string(self): result = [] x,y = self.get_position() self.mouse.move(x,y) string = ["123","456","789"] index = random.randint(0,len(string)-1) key = string[index] self.keyboard.type_string(key) print("模拟使用键盘产生随机输入%s"%(key)) result.append(x) result.append(y) return result,key #键盘按键(单键和组合键) #模拟使用键盘随机按键 def enter_key(self): result = [] x,y = self.get_position() self.mouse.move(x,y) keys = ["a","b","c","d","r",self.keyboard.numpad_keys[2]] index = random.randint(0,len(keys)-1) key = keys[index] self.keyboard.press_key(key) self.keyboard.release_key(key) print("模拟使用键盘按%s键"%(key)) result.append(x) result.append(y) return result,key #获取坐标 图像识别的方式 def zb(self,img): x,y = self.opencv.match_opencv(img) return x,y
class Bot(object): """Mouse and Keyboard robot class. Abbreviation table: - m: stands for mouse - k: stands for keyboard - dl: stands for delay - n: how many times you want to tap the key - i: usually for the ith function key, F1 ~ F12 Almost every method have an option keyword ``dl`` (dl stands for delay), there is ``dl`` seconds delay applied at begin. By default it is ``None``, means no delay applied. Keyboard Key Name Table (Case Insensitive):: # Main Keyboard Keys "ctrl": self.k.control_key, "l_ctrl": self.k.control_l_key, "r_ctrl": self.k.control_r_key, "alt": self.k.alt_key, "l_alt": self.k.alt_l_key, "r_alt": self.k.alt_r_key, "shift": self.k.shift_key, "l_shift": self.k.shift_l_key, "r_shift": self.k.shift_r_key, "tab": self.k.tab_key, "space": self.k.space, "enter": self.k.enter_key, "back": self.k.backspace_key, "backspace": self.k.backspace_key, # Side Keyboard Keys "home": self.k.home_key, "end": self.k.end_key, "page_up": self.k.page_up_key, "pageup": self.k.page_up_key, "page_down": self.k.page_down_key, "page_down": self.k.page_down_key, "insert": self.k.insert_key, "ins": self.k.insert_key, "delete": self.k.delete_key, "del": self.k.delete_key, "up": self.k.up_key, "down": self.k.down_key, "left": self.k.left_key, "right": self.k.right_key, # Function Keys "f1": F1 """ def __init__(self): self.m = PyMouse() self.k = PyKeyboard() self.dl = 0 self._key_mapper = { # Main Keyboard Keys "ctrl": self.k.control_key, "l_ctrl": self.k.control_l_key, "r_ctrl": self.k.control_r_key, "alt": self.k.alt_key, "l_alt": self.k.alt_l_key, "r_alt": self.k.alt_r_key, "shift": self.k.shift_key, "l_shift": self.k.shift_l_key, "r_shift": self.k.shift_r_key, "tab": self.k.tab_key, "space": self.k.space, "enter": self.k.enter_key, "back": self.k.backspace_key, "backspace": self.k.backspace_key, # Side Keyboard Keys "home": self.k.home_key, "end": self.k.end_key, "page_up": self.k.page_up_key, "pageup": self.k.page_up_key, "page_down": self.k.page_down_key, "page_down": self.k.page_down_key, "insert": self.k.insert_key, "ins": self.k.insert_key, "delete": self.k.delete_key, "del": self.k.delete_key, "up": self.k.up_key, "down": self.k.down_key, "left": self.k.left_key, "right": self.k.right_key, # f1 - f12 is the function key } for i in range(1, 1 + 12): self._key_mapper["f%s" % i] = self.k.function_keys[i] def _parse_key(self, name): name = str(name) if name in string.printable: return name elif name.lower() in self._key_mapper: return self._key_mapper[name.lower()] else: raise ValueError("%r is not a valid key name, use one of %s." % (name, list(self._key_mapper))) def delay(self, dl=0): """Delay for ``dl`` seconds. """ if dl is None: time.sleep(self.dl) elif dl < 0: sys.stderr.write( "delay cannot less than zero, this takes no effects.\n") else: time.sleep(dl) #--- Meta --- def get_screen_size(self): """Return screen's width and height in pixel. **中文文档** 返回屏幕的像素尺寸。 """ width, height = self.m.screen_size() return width, height def get_position(self): """Return the current coordinate of mouse. **中文文档** 返回鼠标所处的位置坐标。 """ x_axis, y_axis = self.m.position() return x_axis, y_axis #--- Mouse Macro --- def left_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Left click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处左键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 1, n) self.delay(post_dl) def right_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Right click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处右键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 2, n) self.delay(post_dl) def middle_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Middle click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处中键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 3, n) self.delay(post_dl) def scroll_up(self, n, pre_dl=None, post_dl=None): """Scroll up ``n`` times. **中文文档** 鼠标滚轮向上滚动n次。 """ self.delay(pre_dl) self.m.scroll(vertical=n) self.delay(post_dl) def scroll_down(self, n, pre_dl=None, post_dl=None): """Scroll down ``n`` times. **中文文档** 鼠标滚轮向下滚动n次。 """ self.delay(pre_dl) self.m.scroll(vertical=-n) self.delay(post_dl) def scroll_right(self, n, pre_dl=None, post_dl=None): """Scroll right ``n`` times. **中文文档** 鼠标滚轮向右滚动n次(如果可能的话)。 """ self.delay(pre_dl) self.m.scroll(horizontal=n) self.delay(post_dl) def scroll_left(self, n, pre_dl=None, post_dl=None): """Scroll left ``n`` times. **中文文档** 鼠标滚轮向左滚动n次(如果可能的话)。 """ self.delay(pre_dl) self.m.scroll(horizontal=-n) self.delay(post_dl) def move_to(self, x, y, pre_dl=None, post_dl=None): """Move mouse to (x, y) **中文文档** 移动鼠标到 (x, y) 的坐标处。 """ self.delay(pre_dl) self.m.move(x, y) self.delay(post_dl) def drag_and_release(self, start_x, start_y, end_x, end_y, pre_dl=None, post_dl=None): """Drag something from (start_x, start_y) to (end_x, endy) **中文文档** 从start的坐标处鼠标左键单击拖曳到end的坐标处 start, end是tuple. 格式是(x, y) """ self.delay(pre_dl) self.m.press(start_x, start_y, 1) self.m.drag(end_x, end_y) self.m.release(end_x, end_y, 1) self.delay(post_dl) #--- Keyboard Single Key --- def tap_key(self, key_name, n=1, interval=0, pre_dl=None, post_dl=None): """Tap a key on keyboard for ``n`` times, with ``interval`` seconds of interval. Key is declared by it's name Example:: bot.tap_key("a") bot.tap_key(1) bot.tap_key("up") bot.tap_key("space") bot.tap_key("enter") bot.tap_key("tab") **中文文档** 以 ``interval`` 中定义的频率按下某个按键 ``n`` 次。接受按键名作为输入。 """ key = self._parse_key(key_name) self.delay(pre_dl) self.k.tap_key(key, n, interval) self.delay(post_dl) def enter(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press enter key n times. **中文文档** 按回车键/换行键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.enter_key, n, interval) self.delay(post_dl) def backspace(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press backspace key n times. **中文文档** 按退格键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.backspace_key, n, interval) self.delay(post_dl) def space(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press white space key n times. **中文文档** 按空格键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.space, n) self.delay(post_dl) def fn(self, i, n=1, interval=0, pre_dl=None, post_dl=None): """Press Fn key n times. **中文文档** 按 Fn 功能键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.function_keys[i], n, interval) self.delay(post_dl) def tab(self, n=1, interval=0, pre_dl=None, post_dl=None): """Tap ``tab`` key for ``n`` times, with ``interval`` seconds of interval. **中文文档** 以 ``interval`` 中定义的频率按下某个tab键 ``n`` 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.tab_key, n, interval) self.delay(post_dl) def up(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press up key n times. **中文文档** 按上方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.up_key, n, interval) self.delay(post_dl) def down(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press down key n times. **中文文档** 按下方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.down_key, n, interval) self.delay(post_dl) def left(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press left key n times **中文文档** 按左方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.left_key, n, interval) self.delay(post_dl) def right(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press right key n times. **中文文档** 按右方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.right_key, n, interval) self.delay(post_dl) def delete(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres delete key n times. **中文文档** 按 delete 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.delete_key, n, interval) self.delay(post_dl) def insert(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres insert key n times. **中文文档** 按 insert 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.insert_key, n, interval) self.delay(post_dl) def home(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres home key n times. **中文文档** 按 home 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.home_key, n, interval) self.delay(post_dl) def end(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press end key n times. **中文文档** 按 end 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.end_key, n, interval) self.delay(post_dl) def page_up(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres page_up key n times. **中文文档** 按 page_up 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.page_up_key, n, interval) self.delay(post_dl) def page_down(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres page_down key n times. **中文文档** 按 page_down 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.page_down, n, interval) self.delay(post_dl) #--- Keyboard Combination --- def press_and_tap(self, press_key, tap_key, n=1, interval=0, pre_dl=None, post_dl=None): """Press combination of two keys, like Ctrl + C, Alt + F4. The second key could be tapped for multiple time. Examples:: bot.press_and_tap("ctrl", "c") bot.press_and_tap("shift", "1") **中文文档** 按下两个键的组合键。 """ press_key = self._parse_key(press_key) tap_key = self._parse_key(tap_key) self.delay(pre_dl) self.k.press_key(press_key) self.k.tap_key(tap_key, n, interval) self.k.release_key(press_key) self.delay(post_dl) def press_two_and_tap(self, press_key1, press_key2, tap_key, n=1, interval=0, pre_dl=None, post_dl=None): """Press combination of three keys, like Ctrl + Shift + C, The tap key could be tapped for multiple time. Examples:: bot.press_and_tap("ctrl", "shift", "c") **中文文档** 按下三个键的组合键。 """ press_key1 = self._parse_key(press_key1) press_key2 = self._parse_key(press_key2) tap_key = self._parse_key(tap_key) self.delay(pre_dl) self.k.press_key(press_key1) self.k.press_key(press_key2) self.k.tap_key(tap_key, n, interval) self.k.release_key(press_key1) self.k.release_key(press_key2) self.delay(post_dl) def ctrl_c(self, pre_dl=None, post_dl=None): """Press Ctrl + C, usually for copy. **中文文档** 按下 Ctrl + C 组合键, 通常用于复制。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("c") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_v(self, pre_dl=None, post_dl=None): """Press Ctrl + V, usually for paste. **中文文档** 按下 Ctrl + V 组合键, 通常用于粘贴。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("v") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_x(self, pre_dl=None, post_dl=None): """Press Ctrl + X, usually for cut. **中文文档** 按下 Ctrl + X 组合键, 通常用于剪切。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("x") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_z(self, pre_dl=None, post_dl=None): """Press Ctrl + Z, usually for undo. **中文文档** 按下 Ctrl + Z 组合键, 通常用于撤销上一次动作。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("z") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_y(self, pre_dl=None, post_dl=None): """Press Ctrl + Y, usually for redo. **中文文档** 按下 Ctrl + Y 组合键, 通常用于重复上一次动作。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("y") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_a(self, pre_dl=None, post_dl=None): """Press Ctrl + A, usually for select all. **中文文档** 按下 Ctrl + A 组合键, 通常用于选择全部。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("a") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_f(self, pre_dl=None, post_dl=None): """Press Ctrl + F, usually for search. **中文文档** 按下 Ctrl + F 组合键, 通常用于搜索。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("f") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_fn(self, i, pre_dl=None, post_dl=None): """Press Ctrl + Fn1 ~ 12 once. **中文文档** 按下 Ctrl + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.control_key) self.delay(post_dl) def alt_fn(self, i, pre_dl=None, post_dl=None): """Press Alt + Fn1 ~ 12 once. **中文文档** 按下 Alt + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.alt_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.alt_key) self.delay(post_dl) def shift_fn(self, i, pre_dl=None, post_dl=None): """Press Shift + Fn1 ~ 12 once. **中文文档** 按下 Shift + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.shift_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.shift_key) self.delay(post_dl) def alt_tab(self, n=1, pre_dl=None, post_dl=None): """Press Alt + Tab once, usually for switching between windows. Tab can be tapped for n times, default once. **中文文档** 按下 Alt + Tab 组合键, 其中Tab键按 n 次, 通常用于切换窗口。 """ self.delay(pre_dl) self.k.press_key(self.k.alt_key) self.k.tap_key(self.k.tab_key, n=n, interval=0.1) self.k.release_key(self.k.alt_key) self.delay(post_dl) #--- Other --- def type_string(self, text, interval=0, pre_dl=None, post_dl=None): """Enter strings. **中文文档** 从键盘输入字符串, interval是字符间输入时间间隔, 单位是秒。 """ self.delay(pre_dl) self.k.type_string(text, interval) self.delay(post_dl) def copy_text_to_clipboard(self, text): """Copy text to clipboard. **中文文档** 拷贝字符串到剪贴板。 """ pyperclip.copy(text)
class BaseCursorListener(Listener): """ Defines generic callback functions in order to convert frame events into mouse actions. """ # After this amount of time, a click event is generared (ms) click_timeout = 600 # After this amount of time, a press event is generared (ms) press_timeout = 600 # Number of frames to average numframes = 8 def __init__(self, *args, **kwargs): super(BaseCursorListener, self).__init__(*args, **kwargs) # The following sets are maintained to determine the actions to be performed from # the average values fetched from the last 10 frames self.frames_set = list() self.previousframes_set = list() # Fetch a given mouse object or instanciate it if necessary if 'mouse' in kwargs and isinstance(kwargs['mouse'], PyMouse): self.mouse = kwargs['mouse'] else: self.mouse = PyMouse() # Fetch a given keyboard object or instanciate it if necessary if 'keyboard' in kwargs and isinstance(kwargs['keyboard'], PyKeyboard): self.keyboard = kwargs['keyboard'] else: self.keyboard = PyKeyboard() if 'click' in kwargs: print("test") self.click_timeout = kwargs['click'] if 'press' in kwargs: self.press_timeout = kwargs['press'] # Init mouse position tracking self.previous_pos = self.mouse.position() # Init timers self.click_dtstart = 0 self.press_dtstart = 0 # Init flags self.active_fist = False #Indicates if a clenched fist is considered self.press_requested = False # Indicates if a mouse press event is requested def is_clicking(self, data): """ Determines whether the mouse is clicking or not. The default behavior is to cause a click when the mouse position remains the same during a fixed amount of time. This value is given by the click_timeout attribute. """ # Get the required data hand_state = data['leap_state']['current'] if self.previous_pos == data['pos']: current_time = time.time() elapsed_time = current_time - self.click_dtstart if (elapsed_time * 1000) >= self.click_timeout and hand_state.av_fingers >= 4: self.click_dtstart = time.time() return True else: self.click_dtstart = time.time() return False def is_pressing(self, data): """ Determines whether the mouse is pressing or not. The default behavior is to cause a press action when no fingers are available (the hand is closed). """ # Get the required data hand_state = data['leap_state']['current'] hand_state_prev = data['leap_state']['prev'] current_time = time.time() elapsed_time = current_time - self.press_dtstart if hand_state.av_fingers <= 1.2: if hand_state.av_numhands == 1 and hand_state.is_horizontal and ( (hand_state_prev.av_fingers >= 3 and hand_state.av_fingers <= 1.2) or (self.active_fist and hand_state.av_fingers < 1)): self.press_requested = True elif hand_state.av_numhands == 1 and hand_state.av_fingers <= 1.2 and self.press_requested == True and ( elapsed_time * 1000) >= self.press_timeout: self.active_fist = True return True else: self.press_dtstart = time.time() self.press_requested = False return False def is_releasing(self, data): """ Determines whether the mouse is releasing or not. The default behabior is to cause a release action when the hand is not closed. """ # Get the required data hand_state = data['leap_state']['current'] hand_state_prev = data['leap_state']['prev'] if hand_state.av_fingers >= 2.5 and hand_state.av_palm_pos[ 2] < 0 and self.active_fist: self.active_fist = False self.want_press = False return True return False def is_scrolling_up(self, data): """ Determines whether the mouse is scrolling up or not. """ # Get the required data hand_state = data['leap_state']['current'] if hand_state.av_fingers >= 4 and not self.active_fist and not self.press_requested: if hand_state.av_fingers_speed - hand_state.av_palm_vel < -150: repeats = abs(int(hand_state.av_fingers_speed / 50.)) repeats = max(repeats, 0) repeats = min(repeats, 5) return repeats return False def is_scrolling_down(self, data): """ Determines whether the mouse is scrolling down or not. """ # Get the required data hand_state = data['leap_state']['current'] if hand_state.av_fingers >= 4 and not self.active_fist and not self.press_requested: if hand_state.av_fingers_speed - hand_state.av_palm_vel > 150: repeats = abs(int(hand_state.av_fingers_speed / 50.)) repeats = max(repeats, 0) repeats = min(repeats, 5) return repeats return False def is_switching_desktop(self, latest_frame): """ Determines whether a desktop switch must be induced or not. """ for gesture in latest_frame.gestures(): if gesture.type == Gesture.TYPE_SWIPE: swipe = SwipeGesture(gesture) if swipe.state >= 3: if swipe.direction[0] >= 0.8: return 'right' elif swipe.direction[0] <= -0.8: return 'left' return False def on_init(self, controller): # Force the listener to stop if therse is no controller and no leapd daemon launched showmessage("Initializing listener", Status.RUNNING, Colors.BLUE) i_try = 0 while not controller.frame(2 * self.numframes).is_valid: i_try += 1 if i_try >= 1e6: showmessage("Initializing listener", Status.ERROR, Colors.RED, update=True) sys.exit(1) showmessage("Initializing listener", Status.SUCCESS, Colors.GREEN, update=True) # Fill the frames and previousframes lists with the first available frames for i_frame in range(self.numframes): self.frames_set.append(controller.frame(i_frame)) self.previousframes_set.append( controller.frame(self.numframes + i_frame)) def on_connect(self, controller): # Enable gestures controller.enable_gesture(Gesture.TYPE_SWIPE) showmessage("Initializing listener", Status.SUCCESS, Colors.GREEN, update=True, newline=True) def on_disconnect(self, controller): print("Disconnected") def on_exit(self, controller): print("Exit") def on_frame(self, controller): # Get the most recent frame latest_frame = controller.frame() # Update the frames sets self.previousframes_set.pop(0) self.previousframes_set.append(self.frames_set[0]) self.frames_set.pop(0) self.frames_set.append(latest_frame) data = dict() # Fetch some usefull information pos = self.mouse.position() current_state = CursorState(self.frames_set, self.numframes) previous_state = CursorState(self.previousframes_set, self.numframes) data.update({ 'pos': pos, 'leap_state': { 'current': current_state, 'prev': previous_state, } }) # Determine what is going on above the Leap click = self.is_clicking(data) press = self.is_pressing(data) release = self.is_releasing(data) scroll_up = self.is_scrolling_up(data) scroll_down = self.is_scrolling_down(data) switch_desk = self.is_switching_desktop(latest_frame) data.update({ 'actions': { 'click': click, 'press': press, 'release': release, 'scroll_up': scroll_up, 'scroll_down': scroll_down, 'switch_desk': switch_desk, } }) # Update the previous mouse position self.previous_pos = pos self.update(latest_frame, data) def update(self, frame, data): """ Translates frames dispatched by the Leap Motion to mouse events. """ raise NotImplementedError def move(self, x, y): """ Moves the mouse by using x and y pixels offsets. """ # Get the mouse position current_x, current_y = self.mouse.position() # Move! self.mouse.move(current_x + round(x), current_y + round(y)) def click(self): """ Do a click at the current mouse position. """ # Get the mouse position current_pos = self.mouse.position() # Click! self.mouse.click(*current_pos) def press(self): """ Do a press at the current mouse position. """ # Get the mouse position current_pos = self.mouse.position() # Click! self.mouse.press(*current_pos) def release(self): """ Do a release at the current mouse position. """ # Get the mouse position current_pos = self.mouse.position() # Click! self.mouse.release(*current_pos) def scroll_up(self, repeats=0): """ Do a scroll up action. """ # Scroll! for irep in range(repeats): self.mouse.scroll(vertical=4) time.sleep(.1) def scroll_down(self, repeats=0): """ Do a scroll down action. """ # Scroll! for irep in range(repeats): self.mouse.scroll(vertical=-4) time.sleep(.1) def switch_desktop(self, direction): """ Switch to the desktop to the left, right, top or bottom. """ if direction == 'right': self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.press_key(self.keyboard.control_l_key) self.keyboard.tap_key(self.keyboard.right_key) self.keyboard.release_key(self.keyboard.control_l_key) self.keyboard.release_key(self.keyboard.alt_key) elif direction == 'left': self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.press_key(self.keyboard.control_l_key) self.keyboard.tap_key(self.keyboard.left_key) self.keyboard.release_key(self.keyboard.control_l_key) self.keyboard.release_key(self.keyboard.alt_key)
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)
scroll = ord(data[5]) & 4 != 0 if leftPressed != left: leftPressed = left if left: m.press(position[0], position[1], 1) else: m.release(position[0], position[1], 1) if rightPressed != right: rightPressed = right if right: m.press(position[0], position[1], 2) else: m.release(position[0], position[1], 2) if not scroll and (movement[0] != 0 or movement[1] != 0): m.move(position[0] + movement[0], position[1] + movement[1]) elif scroll: if movement[1] >= 0: m.scroll(position[0], position[1], False, movement[1]) else: m.scroll(position[0], position[1], True, -movement[1]) except IOError: pass print "disconnected" client_sock.close() server_sock.close() print "all done"
class Bot(): def __init__(self): self.mouse = PyMouse() self.keyboard = PyKeyboard() def Delay(self, n): time.sleep(n) """ ====== Mouse Macro ====== """ def Left_click(self, x, y, n=1, dl=0): """在屏幕某点左键点击若干次 """ self.Delay(dl) self.mouse.click(x, y, 1, n) def Right_click(self, x, y, n=1, dl=0): """在屏幕某点右键点击若干次 """ self.Delay(dl) self.mouse.click(x, y, 2, n) def Double_click(self, x, y, dl=0): """在屏幕的某点双击 """ self.Delay(dl) self.mouse.click(x, y, 1, n=2) def Scroll_up(self, n, dl=0): """鼠标滚轮向上n次 """ self.Delay(dl) self.mouse.scroll(vertical=n) def Scroll_down(self, n, dl=0): """鼠标滚轮向下n次 """ self.Delay(dl) self.mouse.scroll(vertical=-n) def Move_to(self, x, y, dl=0): """鼠标移动到x, y的坐标处 """ self.Delay(dl) self.mouse.move(x, y) def Drag_and_release(self, start, end, dl=0): """从start的坐标处鼠标左键单击拖曳到end的坐标处 start, end是tuple. 格式是(x, y) """ self.Delay(dl) self.mouse.press(start[0], start[1], 1) self.mouse.drag(end[0], end[1]) self.Delay(0.1) self.mouse.release(end[0], end[1], 1) def Screen_size(self): width, height = self.mouse.screen_size() return width, height def WhereXY(self): x_axis, y_axis = self.mouse.position() return x_axis, y_axis """ ====== Keyboard Macro ====== """ """COMBINATION组合键""" """Ctrl系列""" def Ctrl_c(self, dl=0): """Ctrl + c 复制 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("c") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_x(self, dl=0): """Ctrl + x 剪切 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("x") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_v(self, dl=0): """Ctrl + v 粘贴 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("v") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_z(self, dl=0): """Ctrl + z 撤销上一次操作 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("z") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_y(self, dl=0): """Ctrl + y 重复上一次操作 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("y") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_a(self, dl=0): """Ctrl + a 全选 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("a") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_Fn(self, n, dl=0): """Ctrl + Fn1~12 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key(self.keyboard.function_keys[n]) self.keyboard.release_key(self.keyboard.control_key) """Alt系列""" def Alt_Tab(self, dl=0): """Alt + Tab 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.tap_key(self.keyboard.tab_key) self.keyboard.release_key(self.keyboard.alt_key) def Alt_Fn(self, n, dl=0): """Alt + Fn1~12 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.tap_key(self.keyboard.function_keys[n]) self.keyboard.release_key(self.keyboard.alt_key) """SINGLE KEY单个键盘键""" def Up(self, n=1, dl=0): """上方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.up_key, n) def Down(self, n=1, dl=0): """下方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.down_key, n) def Left(self, n=1, dl=0): """左方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.left_key, n) def Right(self, n=1, dl=0): """右方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.right_key, n) def Enter(self, n=1, dl=0): """回车键/换行键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.enter_key, n) def Delete(self, n=1, dl=0): """删除键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.delete_key, n) def Back(self, n=1, dl=0): """退格键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.backspace_key, n) def Space(self, n=1, dl=0): """空格键n次 """ self.Delay(dl) self.keyboard.tap_key(" ", n) def Fn(self, n, dl=0): """功能键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.function_keys[n]) def Char(self, char, n=1, dl=0): """输入任意单字符n次,只要能在键盘上打出来的字符都可以 """ if len(char) == 1: self.Delay(dl) self.keyboard.tap_key(char) else: raise Exception("""method "Char()" can only take one character.""") def Type_string(self, text, interval=0, dl=0): """键盘输入字符串,interval是字符间输入时间间隔,单位"秒" """ self.Delay(dl) self.keyboard.type_string(text, interval)
class BaseCursorListener(Listener): """ Defines generic callback functions in order to convert frame events into mouse actions. """ # After this amount of time, a click event is generared (ms) click_timeout = 600 # After this amount of time, a press event is generared (ms) press_timeout = 600 # Number of frames to average numframes = 8 def __init__(self, *args, **kwargs): super(BaseCursorListener, self).__init__(*args, **kwargs) # The following sets are maintained to determine the actions to be performed from # the average values fetched from the last 10 frames self.frames_set = list() self.previousframes_set = list() # Fetch a given mouse object or instanciate it if necessary if 'mouse' in kwargs and isinstance(kwargs['mouse'], PyMouse): self.mouse = kwargs['mouse'] else: self.mouse = PyMouse() # Fetch a given keyboard object or instanciate it if necessary if 'keyboard' in kwargs and isinstance(kwargs['keyboard'], PyKeyboard): self.keyboard = kwargs['keyboard'] else: self.keyboard = PyKeyboard() if 'click' in kwargs: print("test") self.click_timeout = kwargs['click'] if 'press' in kwargs: self.press_timeout = kwargs['press'] # Init mouse position tracking self.previous_pos = self.mouse.position() # Init timers self.click_dtstart = 0 self.press_dtstart = 0 # Init flags self.active_fist = False #Indicates if a clenched fist is considered self.press_requested = False # Indicates if a mouse press event is requested def is_clicking(self, data): """ Determines whether the mouse is clicking or not. The default behavior is to cause a click when the mouse position remains the same during a fixed amount of time. This value is given by the click_timeout attribute. """ # Get the required data hand_state = data['leap_state']['current'] if self.previous_pos == data['pos']: current_time = time.time() elapsed_time = current_time - self.click_dtstart if (elapsed_time * 1000) >= self.click_timeout and hand_state.av_fingers >=4: self.click_dtstart = time.time() return True else: self.click_dtstart = time.time() return False def is_pressing(self, data): """ Determines whether the mouse is pressing or not. The default behavior is to cause a press action when no fingers are available (the hand is closed). """ # Get the required data hand_state = data['leap_state']['current'] hand_state_prev = data['leap_state']['prev'] current_time = time.time() elapsed_time = current_time - self.press_dtstart if hand_state.av_fingers <= 1.2: if hand_state.av_numhands == 1 and hand_state.is_horizontal and ((hand_state_prev.av_fingers >= 3 and hand_state.av_fingers <= 1.2) or (self.active_fist and hand_state.av_fingers < 1)): self.press_requested = True elif hand_state.av_numhands == 1 and hand_state.av_fingers <= 1.2 and self.press_requested == True and (elapsed_time * 1000) >= self.press_timeout: self.active_fist = True return True else: self.press_dtstart = time.time() self.press_requested = False return False def is_releasing(self, data): """ Determines whether the mouse is releasing or not. The default behabior is to cause a release action when the hand is not closed. """ # Get the required data hand_state = data['leap_state']['current'] hand_state_prev = data['leap_state']['prev'] if hand_state.av_fingers >= 2.5 and hand_state.av_palm_pos[2] < 0 and self.active_fist: self.active_fist = False self.want_press = False return True return False def is_scrolling_up(self, data): """ Determines whether the mouse is scrolling up or not. """ # Get the required data hand_state = data['leap_state']['current'] if hand_state.av_fingers >= 4 and not self.active_fist and not self.press_requested: if hand_state.av_fingers_speed - hand_state.av_palm_vel < -150: repeats = abs(int(hand_state.av_fingers_speed / 50.)) repeats = max(repeats, 0) repeats = min(repeats, 5) return repeats return False def is_scrolling_down(self, data): """ Determines whether the mouse is scrolling down or not. """ # Get the required data hand_state = data['leap_state']['current'] if hand_state.av_fingers >= 4 and not self.active_fist and not self.press_requested: if hand_state.av_fingers_speed -hand_state.av_palm_vel > 150: repeats = abs(int(hand_state.av_fingers_speed / 50.)) repeats = max(repeats, 0) repeats = min(repeats, 5) return repeats return False def is_switching_desktop(self, latest_frame): """ Determines whether a desktop switch must be induced or not. """ for gesture in latest_frame.gestures(): if gesture.type == Gesture.TYPE_SWIPE: swipe = SwipeGesture(gesture) if swipe.state >= 3: if swipe.direction[0] >= 0.8: return 'right' elif swipe.direction[0] <= -0.8: return 'left' return False def on_init(self, controller): # Force the listener to stop if therse is no controller and no leapd daemon launched showmessage("Initializing listener", Status.RUNNING, Colors.BLUE) i_try = 0 while not controller.frame(2*self.numframes).is_valid: i_try += 1 if i_try >= 1e6: showmessage("Initializing listener", Status.ERROR, Colors.RED, update=True) sys.exit(1) showmessage("Initializing listener", Status.SUCCESS, Colors.GREEN, update=True) # Fill the frames and previousframes lists with the first available frames for i_frame in range(self.numframes): self.frames_set.append(controller.frame(i_frame)) self.previousframes_set.append(controller.frame(self.numframes + i_frame)) def on_connect(self, controller): # Enable gestures controller.enable_gesture(Gesture.TYPE_SWIPE); showmessage("Initializing listener", Status.SUCCESS, Colors.GREEN, update=True, newline=True) def on_disconnect(self, controller): print("Disconnected") def on_exit(self, controller): print("Exit") def on_frame(self, controller): # Get the most recent frame latest_frame = controller.frame() # Update the frames sets self.previousframes_set.pop(0) self.previousframes_set.append(self.frames_set[0]) self.frames_set.pop(0) self.frames_set.append(latest_frame) data = dict() # Fetch some usefull information pos = self.mouse.position() current_state = CursorState(self.frames_set, self.numframes) previous_state = CursorState(self.previousframes_set, self.numframes) data.update({ 'pos': pos, 'leap_state': { 'current': current_state, 'prev': previous_state, } }) # Determine what is going on above the Leap click = self.is_clicking(data) press = self.is_pressing(data) release = self.is_releasing(data) scroll_up = self.is_scrolling_up(data) scroll_down = self.is_scrolling_down(data) switch_desk = self.is_switching_desktop(latest_frame) data.update({ 'actions': { 'click': click, 'press': press, 'release': release, 'scroll_up': scroll_up, 'scroll_down': scroll_down, 'switch_desk': switch_desk, } }) # Update the previous mouse position self.previous_pos = pos self.update(latest_frame, data) def update(self, frame, data): """ Translates frames dispatched by the Leap Motion to mouse events. """ raise NotImplementedError def move(self, x, y): """ Moves the mouse by using x and y pixels offsets. """ # Get the mouse position current_x, current_y = self.mouse.position() # Move! self.mouse.move(current_x + round(x), current_y + round(y)) def click(self): """ Do a click at the current mouse position. """ # Get the mouse position current_pos = self.mouse.position() # Click! self.mouse.click(*current_pos) def press(self): """ Do a press at the current mouse position. """ # Get the mouse position current_pos = self.mouse.position() # Click! self.mouse.press(*current_pos) def release(self): """ Do a release at the current mouse position. """ # Get the mouse position current_pos = self.mouse.position() # Click! self.mouse.release(*current_pos) def scroll_up(self, repeats=0): """ Do a scroll up action. """ # Scroll! for irep in range(repeats): self.mouse.scroll(vertical=4) time.sleep(.1) def scroll_down(self, repeats=0): """ Do a scroll down action. """ # Scroll! for irep in range(repeats): self.mouse.scroll(vertical=-4) time.sleep(.1) def switch_desktop(self, direction): """ Switch to the desktop to the left, right, top or bottom. """ if direction == 'right': self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.press_key(self.keyboard.control_l_key) self.keyboard.tap_key(self.keyboard.right_key) self.keyboard.release_key(self.keyboard.control_l_key) self.keyboard.release_key(self.keyboard.alt_key) elif direction == 'left': self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.press_key(self.keyboard.control_l_key) self.keyboard.tap_key(self.keyboard.left_key) self.keyboard.release_key(self.keyboard.control_l_key) self.keyboard.release_key(self.keyboard.alt_key)
c = max(contours, key=cv2.contourArea) m = cv2.moments(c) center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00'])) screen_x = int((FRAME_WIDTH - center[0]) * WIDTH_RATIO) - WIDTH_OFFSET screen_y = int(center[1] * HEIGHT_RATIO) - HEIGHT_OFFSET screen_x = 0 if screen_x < 0 else screen_x screen_x = SCREEN_WIDTH if screen_x > SCREEN_WIDTH else screen_x screen_y = 0 if screen_y < 0 else screen_y screen_y = SCREEN_HEIGHT if screen_y > SCREEN_HEIGHT else screen_y screen_center = [screen_x, screen_y] #pyautogui.moveTo(screen_center[0],screen_center[1]) mouse.move(screen_x, screen_y) area = cv2.contourArea(c) if area > CLICK_THRESHOLD and not clicked: clicked = True mouse.click(screen_x, screen_y) pygame.mixer.music.play() elif area < SCROLL_THRESHOLD and screen_center[ 1] < SCREEN_HEIGHT / 2 and not clicked: mouse.scroll(vertical=2) elif area < SCROLL_THRESHOLD and screen_center[ 1] > SCREEN_HEIGHT / 2 and not clicked: mouse.scroll(vertical=-2) elif area < CLICK_THRESHOLD and clicked: clicked = False if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
if not all(landmarks): NOT_SIGNAL += 1 continue hand_w = max(landmarks, key=lambda x: x[0])[0] - min( landmarks, key=lambda x: x[0])[0] hand_h = max(landmarks, key=lambda x: x[1])[1] - min( landmarks, key=lambda x: x[1])[1] d = distance( (landmarks[8][0] / hand_w, landmarks[8][1] / hand_h), (landmarks[12][0] / hand_w, landmarks[12][1] / hand_h)) < 0.25 if not SCROLLING and d: SCROLLING = True prev_pos = landmarks[8] if SCROLLING and d: m.scroll(horizontal=(landmarks[8][0] - prev_pos[0]) // 20, vertical=(landmarks[8][1] - prev_pos[1]) // 20) if not d: SCROLLING = False landmarks = np.array(landmarks) median_center = landmarks[4] mouse_x = int( translate(landmarks[8][0], 0, image_cols, 0, SCREEN_WIDTH)) mouse_y = int( translate(landmarks[8][1], 0, image_rows, SCREEN_HEIGHT / 2, SCREEN_HEIGHT * 4)) if wait_time > 5: if distance( (landmarks[8][0] / hand_w, landmarks[8][1] / hand_h), (median_center[0] / hand_w, median_center[1] / hand_h)) < 0.2:
right = ord(data[5]) & 1 != 0 scroll = ord(data[5]) & 4 != 0 if leftPressed != left: leftPressed = left if left: m.press(position[0], position[1], 1) else: m.release(position[0], position[1], 1) if rightPressed != right: rightPressed = right if right: m.press(position[0], position[1], 2) else: m.release(position[0], position[1], 2) if not scroll and (movement[0] != 0 or movement[1] != 0): m.move(position[0] + movement[0], position[1] + movement[1]) elif scroll: if movement[1] >= 0: m.scroll(position[0], position[1], False, movement[1]) else: m.scroll(position[0], position[1], True, -movement[1]) except IOError: pass print "disconnected" client_sock.close() server_sock.close() print "all done"
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)
class Bot(object): """Mouse and Keyboard robot class. Abbreviation table: - m: stands for mouse - k: stands for keyboard - dl: stands for delay - n: how many times you want to tap the key - i: usually for the ith function key, F1 ~ F12 Almost every method have an option keyword ``dl`` (dl stands for delay), there is ``dl`` seconds delay applied at begin. By default it is ``None``, means no delay applied. Keyboard Key Name Table (Case Insensitive):: # Main Keyboard Keys "ctrl": self.k.control_key, "l_ctrl": self.k.control_l_key, "r_ctrl": self.k.control_r_key, "alt": self.k.alt_key, "l_alt": self.k.alt_l_key, "r_alt": self.k.alt_r_key, "shift": self.k.shift_key, "l_shift": self.k.shift_l_key, "r_shift": self.k.shift_r_key, "tab": self.k.tab_key, "space": self.k.space_key, "enter": self.k.enter_key, "back": self.k.backspace_key, "backspace": self.k.backspace_key, # Side Keyboard Keys "home": self.k.home_key, "end": self.k.end_key, "page_up": self.k.page_up_key, "pageup": self.k.page_up_key, "page_down": self.k.page_down_key, "page_down": self.k.page_down_key, "insert": self.k.insert_key, "ins": self.k.insert_key, "delete": self.k.delete_key, "del": self.k.delete_key, "up": self.k.up_key, "down": self.k.down_key, "left": self.k.left_key, "right": self.k.right_key, # Function Keys "f1": F1 """ def __init__(self): self.m = PyMouse() self.k = PyKeyboard() self.dl = 0 self._key_mapper = { # Main Keyboard Keys "ctrl": self.k.control_key, "l_ctrl": self.k.control_l_key, "r_ctrl": self.k.control_r_key, "alt": self.k.alt_key, "l_alt": self.k.alt_l_key, "r_alt": self.k.alt_r_key, "shift": self.k.shift_key, "l_shift": self.k.shift_l_key, "r_shift": self.k.shift_r_key, "tab": self.k.tab_key, "space": self.k.space_key, "enter": self.k.enter_key, "back": self.k.backspace_key, "backspace": self.k.backspace_key, # Side Keyboard Keys "home": self.k.home_key, "end": self.k.end_key, "page_up": self.k.page_up_key, "pageup": self.k.page_up_key, "page_down": self.k.page_down_key, "page_down": self.k.page_down_key, "insert": self.k.insert_key, "ins": self.k.insert_key, "delete": self.k.delete_key, "del": self.k.delete_key, "up": self.k.up_key, "down": self.k.down_key, "left": self.k.left_key, "right": self.k.right_key, # f1 - f12 is the function key } for i in range(1, 1+12): self._key_mapper["f%s" % i] = self.k.function_keys[i] def _parse_key(self, name): name = str(name) if name in string.printable: return name elif name.lower() in self._key_mapper: return self._key_mapper[name.lower()] else: raise ValueError( "%r is not a valid key name, use one of %s." % (name, list(self._key_mapper))) def delay(self, dl=0): """Delay for ``dl`` seconds. """ if dl is None: time.sleep(self.dl) elif dl < 0: sys.stderr.write( "delay cannot less than zero, this takes no effects.\n") else: time.sleep(dl) #--- Meta --- def get_screen_size(self): """Return screen's width and height in pixel. **中文文档** 返回屏幕的像素尺寸。 """ width, height = self.m.screen_size() return width, height def get_position(self): """Return the current coordinate of mouse. **中文文档** 返回鼠标所处的位置坐标。 """ x_axis, y_axis = self.m.position() return x_axis, y_axis #--- Mouse Macro --- def left_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Left click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处左键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 1, n) self.delay(post_dl) def right_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Right click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处右键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 2, n) self.delay(post_dl) def middle_click(self, x, y, n=1, pre_dl=None, post_dl=None): """Middle click at ``(x, y)`` on screen for ``n`` times. at begin. **中文文档** 在屏幕的 ``(x, y)`` 坐标处中键单击 ``n`` 次。 """ self.delay(pre_dl) self.m.click(x, y, 3, n) self.delay(post_dl) def scroll_up(self, n, pre_dl=None, post_dl=None): """Scroll up ``n`` times. **中文文档** 鼠标滚轮向上滚动n次。 """ self.delay(pre_dl) self.m.scroll(vertical=n) self.delay(post_dl) def scroll_down(self, n, pre_dl=None, post_dl=None): """Scroll down ``n`` times. **中文文档** 鼠标滚轮向下滚动n次。 """ self.delay(pre_dl) self.m.scroll(vertical=-n) self.delay(post_dl) def scroll_right(self, n, pre_dl=None, post_dl=None): """Scroll right ``n`` times. **中文文档** 鼠标滚轮向右滚动n次(如果可能的话)。 """ self.delay(pre_dl) self.m.scroll(horizontal=n) self.delay(post_dl) def scroll_left(self, n, pre_dl=None, post_dl=None): """Scroll left ``n`` times. **中文文档** 鼠标滚轮向左滚动n次(如果可能的话)。 """ self.delay(pre_dl) self.m.scroll(horizontal=-n) self.delay(post_dl) def move_to(self, x, y, pre_dl=None, post_dl=None): """Move mouse to (x, y) **中文文档** 移动鼠标到 (x, y) 的坐标处。 """ self.delay(pre_dl) self.m.move(x, y) self.delay(post_dl) def drag_and_release(self, start_x, start_y, end_x, end_y, pre_dl=None, post_dl=None): """Drag something from (start_x, start_y) to (end_x, endy) **中文文档** 从start的坐标处鼠标左键单击拖曳到end的坐标处 start, end是tuple. 格式是(x, y) """ self.delay(pre_dl) self.m.press(start_x, start_y, 1) self.m.drag(end_x, end_y) self.m.release(end_x, end_y, 1) self.delay(post_dl) #--- Keyboard Single Key --- def tap_key(self, key_name, n=1, interval=0, pre_dl=None, post_dl=None): """Tap a key on keyboard for ``n`` times, with ``interval`` seconds of interval. Key is declared by it's name Example:: bot.tap_key("a") bot.tap_key(1) bot.tap_key("up") bot.tap_key("space") bot.tap_key("enter") bot.tap_key("tab") **中文文档** 以 ``interval`` 中定义的频率按下某个按键 ``n`` 次。接受按键名作为输入。 """ key = self._parse_key(key_name) self.delay(pre_dl) self.k.tap_key(key, n, interval) self.delay(post_dl) def enter(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press enter key n times. **中文文档** 按回车键/换行键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.enter_key, n, interval) self.delay(post_dl) def backspace(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press backspace key n times. **中文文档** 按退格键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.backspace_key, n, interval) self.delay(post_dl) def space(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press white space key n times. **中文文档** 按空格键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.space_key, n) self.delay(post_dl) def fn(self, i, n=1, interval=0, pre_dl=None, post_dl=None): """Press Fn key n times. **中文文档** 按 Fn 功能键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.function_keys[i], n, interval) self.delay(post_dl) def tab(self, n=1, interval=0, pre_dl=None, post_dl=None): """Tap ``tab`` key for ``n`` times, with ``interval`` seconds of interval. **中文文档** 以 ``interval`` 中定义的频率按下某个tab键 ``n`` 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.tab_key, n, interval) self.delay(post_dl) def up(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press up key n times. **中文文档** 按上方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.up_key, n, interval) self.delay(post_dl) def down(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press down key n times. **中文文档** 按下方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.down_key, n, interval) self.delay(post_dl) def left(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press left key n times **中文文档** 按左方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.left_key, n, interval) self.delay(post_dl) def right(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press right key n times. **中文文档** 按右方向键 n 次。 """ self.delay(pre_dl) self.k.tap_key(self.k.right_key, n, interval) self.delay(post_dl) def delete(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres delete key n times. **中文文档** 按 delete 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.delete_key, n, interval) self.delay(post_dl) def insert(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres insert key n times. **中文文档** 按 insert 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.insert_key, n, interval) self.delay(post_dl) def home(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres home key n times. **中文文档** 按 home 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.home_key, n, interval) self.delay(post_dl) def end(self, n=1, interval=0, pre_dl=None, post_dl=None): """Press end key n times. **中文文档** 按 end 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.end_key, n, interval) self.delay(post_dl) def page_up(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres page_up key n times. **中文文档** 按 page_up 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.page_up_key, n, interval) self.delay(post_dl) def page_down(self, n=1, interval=0, pre_dl=None, post_dl=None): """Pres page_down key n times. **中文文档** 按 page_down 键n次。 """ self.delay(pre_dl) self.k.tap_key(self.k.page_down, n, interval) self.delay(post_dl) #--- Keyboard Combination --- def press_and_tap(self, press_key, tap_key, n=1, interval=0, pre_dl=None, post_dl=None): """Press combination of two keys, like Ctrl + C, Alt + F4. The second key could be tapped for multiple time. Examples:: bot.press_and_tap("ctrl", "c") bot.press_and_tap("shift", "1") **中文文档** 按下两个键的组合键。 """ press_key = self._parse_key(press_key) tap_key = self._parse_key(tap_key) self.delay(pre_dl) self.k.press_key(press_key) self.k.tap_key(tap_key, n, interval) self.k.release_key(press_key) self.delay(post_dl) def press_two_and_tap(self, press_key1, press_key2, tap_key, n=1, interval=0, pre_dl=None, post_dl=None): """Press combination of three keys, like Ctrl + Shift + C, The tap key could be tapped for multiple time. Examples:: bot.press_and_tap("ctrl", "shift", "c") **中文文档** 按下三个键的组合键。 """ press_key1 = self._parse_key(press_key1) press_key2 = self._parse_key(press_key2) tap_key = self._parse_key(tap_key) self.delay(pre_dl) self.k.press_key(press_key1) self.k.press_key(press_key2) self.k.tap_key(tap_key, n, interval) self.k.release_key(press_key1) self.k.release_key(press_key2) self.delay(post_dl) def ctrl_c(self, pre_dl=None, post_dl=None): """Press Ctrl + C, usually for copy. **中文文档** 按下 Ctrl + C 组合键, 通常用于复制。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("c") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_v(self, pre_dl=None, post_dl=None): """Press Ctrl + V, usually for paste. **中文文档** 按下 Ctrl + V 组合键, 通常用于粘贴。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("v") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_x(self, pre_dl=None, post_dl=None): """Press Ctrl + X, usually for cut. **中文文档** 按下 Ctrl + X 组合键, 通常用于剪切。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("x") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_z(self, pre_dl=None, post_dl=None): """Press Ctrl + Z, usually for undo. **中文文档** 按下 Ctrl + Z 组合键, 通常用于撤销上一次动作。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("z") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_y(self, pre_dl=None, post_dl=None): """Press Ctrl + Y, usually for redo. **中文文档** 按下 Ctrl + Y 组合键, 通常用于重复上一次动作。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("y") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_a(self, pre_dl=None, post_dl=None): """Press Ctrl + A, usually for select all. **中文文档** 按下 Ctrl + A 组合键, 通常用于选择全部。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("a") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_f(self, pre_dl=None, post_dl=None): """Press Ctrl + F, usually for search. **中文文档** 按下 Ctrl + F 组合键, 通常用于搜索。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key("f") self.k.release_key(self.k.control_key) self.delay(post_dl) def ctrl_fn(self, i, pre_dl=None, post_dl=None): """Press Ctrl + Fn1 ~ 12 once. **中文文档** 按下 Ctrl + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.control_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.control_key) self.delay(post_dl) def alt_fn(self, i, pre_dl=None, post_dl=None): """Press Alt + Fn1 ~ 12 once. **中文文档** 按下 Alt + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.alt_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.alt_key) self.delay(post_dl) def shift_fn(self, i, pre_dl=None, post_dl=None): """Press Shift + Fn1 ~ 12 once. **中文文档** 按下 Shift + Fn1 ~ 12 组合键。 """ self.delay(pre_dl) self.k.press_key(self.k.shift_key) self.k.tap_key(self.k.function_keys[i]) self.k.release_key(self.k.shift_key) self.delay(post_dl) def alt_tab(self, n=1, pre_dl=None, post_dl=None): """Press Alt + Tab once, usually for switching between windows. Tab can be tapped for n times, default once. **中文文档** 按下 Alt + Tab 组合键, 其中Tab键按 n 次, 通常用于切换窗口。 """ self.delay(pre_dl) self.k.press_key(self.k.alt_key) self.k.tap_key(self.k.tab_key, n=n, interval=0.1) self.k.release_key(self.k.alt_key) self.delay(post_dl) #--- Other --- def type_string(self, text, interval=0, pre_dl=None, post_dl=None): """Enter strings. **中文文档** 从键盘输入字符串, interval是字符间输入时间间隔, 单位是秒。 """ self.delay(pre_dl) self.k.type_string(text, interval) self.delay(post_dl) def copy_text_to_clipboard(self, text): """Copy text to clipboard. **中文文档** 拷贝字符串到剪贴板。 """ pyperclip.copy(text)
def 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.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") 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.define([" "], user_full_name) userin.execute(0) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") 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.define([" "]," ") 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.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.") previous_command = com elif "WHAT IS THE 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() with nostdout(): with nostderr(): capture = re.search("WHAT IS THE TEMPERATURE (?:IN|ON|AT)? (?P<query_city>.*) (?:IN|ON|AT|)? (?:THE)? (?P<query_state>.*)",com) if capture: search_query_city = capture.group('query_city') try: owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574") city_and_state = str(search_query_city + ",us") weather = owm.weather_at_place(city_and_state).get_weather() userin.define([" "],str("The temperature in " + search_query_city + " is " + str(weather.get_temperature("celsius")['temp']))) userin.execute(0) userin.say("The temperature in " + search_query_city + " is " + str(weather.get_temperature("celsius")['temp'])) previous_command = com except: pass 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 + ".") previous_command = com 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") previous_command = com elif "WEB BROWSER" in com: tts_kill() userin.define(["sensible-browser"],"Web Browser") userin.execute(0) userin.say("Web Browser") previous_command = com elif "OPEN BLENDER" == com: tts_kill() userin.define(["blender"],"Blender") userin.execute(0) userin.say("Blender 3D computer graphics software") previous_command = com 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") previous_command = com 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") previous_command = com 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") previous_command = com 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") previous_command = com 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") previous_command = com 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") previous_command = com elif "OPEN STEAM" == com: tts_kill() userin.define(["steam"],"Steam") userin.execute(0) userin.say("Steam Game Store") previous_command = com 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") previous_command = com elif "OFFICE SUITE" in com: tts_kill() userin.define(["libreoffice"],"LibreOffice") userin.execute(0) userin.say("Office Suite") previous_command = com elif "OPEN WRITER" == com: tts_kill() userin.define(["libreoffice","--writer"],"LibreOffice Writer") userin.execute(0) userin.say("Writer") previous_command = com elif "OPEN MATH" == com: tts_kill() userin.define(["libreoffice","--math"],"LibreOffice Math") userin.execute(0) userin.say("Math") previous_command = com elif "OPEN IMPRESS" == com: tts_kill() userin.define(["libreoffice","--impress"],"LibreOffice Impress") userin.execute(0) userin.say("Impress") previous_command = com elif "OPEN DRAW" == com: tts_kill() userin.define(["libreoffice","--draw"],"LibreOffice Draw") userin.execute(0) userin.say("Draw") 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.define(["sudo","poweroff"],"Shutting down") userin.say("Shutting down") userin.execute(3) previous_command = com elif com == "GOODBYE" or com == "BYE BYE" or com == "SEE YOU LATER": tts_kill() userin.define([" "]," ") 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.define(["sensible-browser",wikipage.url],search_query) userin.execute(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.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') else: tts_kill() #dragonfire_respond = kernel.respond(com) aiml_respond = learn_.respond(com) if aiml_respond: userin.define([" "]," ") userin.say(aiml_respond) else: omniscient_.respond(original_com,True,userin) previous_command = com
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.omniscient.Omniscient.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) if args["verbose"]: userin.pretty_print_nlp_parsing_results(doc) if self.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 "" 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) return userin.say( wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return userin.say( "Sorry, " + user_prefix + ". But I'm unable to connect to Wikipedia servers." ) except Exception: return False 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)): 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." ])) 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") 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 if h.check_wh_lemma("what") and h.check_deps_contains("your name"): return userin.execute([" "], "My name is Dragonfire.", True) 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 + ".") 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 + ".") 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_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.") 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.") if h.check_noun_lemma("office") and h.check_noun_lemma("suite"): userin.execute(["libreoffice"], "LibreOffice") return userin.say("Opening LibreOffice") 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.") 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") 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 return userin.say("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("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") 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" return userin.say("Pardon, " + user_prefix + ".") 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" return userin.say("Pardon, " + user_prefix + ".") 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}) user_prefix = title return userin.say("OK, " + user_prefix + ".") 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) 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" 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 h.check_lemma("back") and h.max_word_count( 4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.left_key]) return "back" if h.check_lemma("forward") and h.max_word_count( 4) and not args["server"]: with nostdout(): with nostderr(): k = PyKeyboard() if not self.testing: k.press_keys([k.alt_l_key, k.right_key]) return "forward" 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" 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" 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) 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 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) return userin.say( wikicontent, cmd=["sensible-browser", wikipage.url]) except requests.exceptions.ConnectionError: userin.execute([" "], "Wikipedia connection error.") return 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] 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 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 ]) 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 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) 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: omniscient_response = omniscient.respond( com, not args["silent"], userin, user_prefix, args["server"]) if omniscient_response: return omniscient_response else: dc_response = dc.respond(original_com, user_prefix) if dc_response: return userin.say(dc_response)
class ServerMouseController: """ class ServerMouseController --- This class should be able to control the mouse with the data received from a client. It should be able to move, scroll and click. It works only with one client. """ PACKET_MOVE = 1 PACKET_CLICK = 2 PACKET_SCROLL = 3 # We want only one connection for the server. MAX_CONNECTION = 1 def __init__(self, port=34555): print ('You should connect on', ( socket.gethostbyname_ex(socket.gethostname()), port)) self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.mouse_controller = PyMouse() self.server_running = True self.host = '' self.port = port self.buffer = '' # array.array('b', '\0' * 512) self.client = None def start(self): def handler_interupt(signum, frame): print('interrupt server') self.stop() self.server_socket.bind((self.host, self.port)) self.server_socket.listen(ServerMouseController.MAX_CONNECTION) signal.signal(signal.SIGINT, handler_interupt) print('start server') self.run() def run(self): while self.server_running: potential_read = [self.server_socket] if self.client is not None: potential_read.append(self.client) try: ready_to_read, ready_to_write, in_erro = select.select( potential_read, [], []) if self.server_socket in ready_to_read: conn, addr = self.server_socket.accept() self.client = conn print('New connection from ', addr) elif self.client in ready_to_read: # self.client.recv_into(self.buffer, 512) recv = self.client.recv(128) self.buffer += recv if len(recv) == 0: print('Disconnection from client') self.client.close() self.client = None self.buffer = '' continue unpack = Unpacker(self.buffer) if len(self.buffer) >= unpack.unpack_int(): unpack.set_position(0) size = unpack.unpack_int() cmd = unpack.unpack_int() if cmd == ServerMouseController.PACKET_MOVE: # Mouse move control x = unpack.unpack_float() y = unpack.unpack_float() print(size, cmd, x, y) self.mouse_controller.move( self.mouse_controller.position()[0] - x, self.mouse_controller.position()[1] - y) elif cmd == ServerMouseController.PACKET_CLICK: # Mouse click control button = unpack.unpack_int() nb_click = unpack.unpack_int() print(size, cmd, button, nb_click) self.mouse_controller.click( self.mouse_controller.position()[0], self.mouse_controller.position()[1], button, nb_click) elif cmd == ServerMouseController.PACKET_SCROLL: # Mouse scrolling x = unpack.unpack_float() y = unpack.unpack_float() print(size, cmd, x, y) self.mouse_controller.scroll( vertical=int(y), horizontal=int(x)) self.buffer = self.buffer[unpack.get_position():] except select.error as e: print(e) if self.client is not None: self.client.close() self.server_socket.close() print('Server stop') def stop(self): self.server_running = False
class Bot(): def __init__(self): self.mouse = PyMouse() self.keyboard = PyKeyboard() def Delay(self, n): time.sleep(n) """ ====== Mouse Macro ====== """ def Left_click(self, x, y, n = 1, dl = 0): """在屏幕某点左键点击若干次 """ self.Delay(dl) self.mouse.click(x, y, 1, n) def Right_click(self, x, y, n = 1, dl = 0): """在屏幕某点右键点击若干次 """ self.Delay(dl) self.mouse.click(x, y, 2, n) def Double_click(self, x, y, dl = 0): """在屏幕的某点双击 """ self.Delay(dl) self.mouse.click(x, y, 1, n = 2) def Scroll_up(self, n, dl = 0): """鼠标滚轮向上n次 """ self.Delay(dl) self.mouse.scroll(vertical = n) def Scroll_down(self, n, dl = 0): """鼠标滚轮向下n次 """ self.Delay(dl) self.mouse.scroll(vertical = -n) def Move_to(self, x, y, dl = 0): """鼠标移动到x, y的坐标处 """ self.Delay(dl) self.mouse.move(x, y) def Drag_and_release(self, start, end, dl = 0): """从start的坐标处鼠标左键单击拖曳到end的坐标处 start, end是tuple. 格式是(x, y) """ self.Delay(dl) self.mouse.press(start[0], start[1], 1) self.mouse.drag(end[0], end[1]) self.Delay(0.1) self.mouse.release(end[0], end[1], 1) def Screen_size(self): width, height = self.mouse.screen_size() return width, height def WhereXY(self): x_axis, y_axis = self.mouse.position() return x_axis, y_axis """ ====== Keyboard Macro ====== """ """COMBINATION组合键""" """Ctrl系列""" def Ctrl_c(self, dl = 0): """Ctrl + c 复制 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("c") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_x(self, dl = 0): """Ctrl + x 剪切 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("x") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_v(self, dl = 0): """Ctrl + v 粘贴 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("v") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_z(self, dl = 0): """Ctrl + z 撤销上一次操作 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("z") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_y(self, dl = 0): """Ctrl + y 重复上一次操作 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("y") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_a(self, dl = 0): """Ctrl + a 全选 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key("a") self.keyboard.release_key(self.keyboard.control_key) def Ctrl_Fn(self, n, dl = 0): """Ctrl + Fn1~12 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.control_key) self.keyboard.tap_key(self.keyboard.function_keys[n]) self.keyboard.release_key(self.keyboard.control_key) """Alt系列""" def Alt_Tab(self, dl = 0): """Alt + Tab 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.tap_key(self.keyboard.tab_key) self.keyboard.release_key(self.keyboard.alt_key) def Alt_Fn(self, n, dl = 0): """Alt + Fn1~12 组合键 """ self.Delay(dl) self.keyboard.press_key(self.keyboard.alt_key) self.keyboard.tap_key(self.keyboard.function_keys[n]) self.keyboard.release_key(self.keyboard.alt_key) """SINGLE KEY单个键盘键""" def Up(self, n = 1, dl = 0): """上方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.up_key, n) def Down(self, n = 1, dl = 0): """下方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.down_key, n) def Left(self, n = 1, dl = 0): """左方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.left_key, n) def Right(self, n = 1, dl = 0): """右方向键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.right_key, n) def Enter(self, n = 1, dl = 0): """回车键/换行键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.enter_key, n) def Delete(self, n = 1, dl = 0): """删除键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.delete_key, n) def Back(self, n = 1, dl = 0): """退格键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.backspace_key, n) def Space(self, n = 1, dl = 0): """空格键n次 """ self.Delay(dl) self.keyboard.tap_key(" ", n) def Fn(self, n, dl = 0): """功能键n次 """ self.Delay(dl) self.keyboard.tap_key(self.keyboard.function_keys[n]) def Char(self, char, n = 1, dl = 0): """输入任意单字符n次,只要能在键盘上打出来的字符都可以 """ if len(char) == 1: self.Delay(dl) self.keyboard.tap_key(char) else: raise Exception("""method "Char()" can only take one character.""") def Type_string(self, text, interval = 0, dl = 0): """键盘输入字符串,interval是字符间输入时间间隔,单位"秒" """ self.Delay(dl) self.keyboard.type_string(text, interval)
tracker.update_image() tracker.update() # Check the tracking status status = tracker.get_status(move) if status == psmove.Tracker_TRACKING: x, y, radius = tracker.get_position(move) # print 'Position: (%5.2f, %5.2f), Radius: %3.2f, Trigger: %3d' % ( # 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)
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 or not isinstance(com, str): return False original_com = com global inactive global user_full_name global user_prefix global config_file userin.twitter_user = tw_user com = com.upper() com = re.sub(r'([^\s\w]|_)+', '', com).strip() print("You: " + com) if inactive 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 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.define_and_execute( ["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) return True except requests.exceptions.ConnectionError: userin.define_and_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 com in ("DRAGONFIRE", "DRAGON FIRE", "WAKE UP", "HEY"): tts_kill() inactive = False userin.define([" "], " ") userin.say(choice([ "Yes, " + user_prefix + ".", "Yes. I'm waiting.", "What is your order?", "Ready for the orders!", user_prefix + ", tell me your wish." ])) elif "GO TO SLEEP" == com: tts_kill() inactive = True userin.define_and_execute( ["echo"], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'") userin.say("I'm going to sleep") elif com in ("ENOUGH", "SHUT UP"): print("Dragonfire quiets.") tts_kill() elif VirtualAssistant.exact_match(com): return True # the request has been handled elif VirtualAssistant.in_match(com): return True # the request has been handled elif ("SEARCH" in com or "FIND" in com) and VirtualAssistant.search_command(com): pass # the request has been handled 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 + ".") # only for The United States today but prepared for all countries. Also # only for celsius degrees today. --> by Radan Liska :-) elif "WHAT" in com and "TEMPERATURE" in com: 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() fmt = "The temperature in {} is {} degrees celsius" msg = fmt.format(city, weather.get_temperature('celsius')['temp']) userin.define_and_execute([" "], msg) userin.say(msg) elif "FILE MANAGER" in com or "OPEN FILES" == com: tts_kill() userin.define_and_execute(["dolphin"], "File Manager") # KDE neon userin.define_and_execute(["pantheon-files"], "File Manager") # elementary OS userin.define_and_execute(["nautilus", "--browser"], "File Manager") # Ubuntu userin.say("File Manager") elif "OPEN CAMERA" == com: tts_kill() userin.define_and_execute(["kamoso"], "Camera") # KDE neon userin.define_and_execute(["snap-photobooth"], "Camera") # elementary OS userin.define_and_execute(["cheese"], "Camera") # Ubuntu userin.say("Camera") elif "OPEN CALENDAR" == com: tts_kill() userin.define_and_execute(["korganizer"], "Calendar") # KDE neon userin.define_and_execute(["maya-calendar"], "Calendar") # elementary OS userin.define_and_execute(["orage"], "Calendar") # Ubuntu userin.say("Calendar") elif "OPEN CALCULATOR" == com: tts_kill() userin.define_and_execute(["kcalc"], "Calculator") # KDE neon userin.define_and_execute(["pantheon-calculator"], "Calculator") # elementary OS userin.define_and_execute(["gnome-calculator"], "Calculator") # Ubuntu userin.say("Calculator") elif "SOFTWARE CENTER" in com: tts_kill() userin.define_and_execute(["plasma-discover"], "Software Center") # KDE neon userin.define_and_execute( ["software-center"], "Software Center") # elementary OS & Ubuntu userin.say("Software Center") 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", "GOOD BYE", "BYE BYE", "SEE YOU LATER", "CATCH 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_and_execute( ["sensible-browser", wikipage.url], search_query) userin.say(wikicontent) except requests.exceptions.ConnectionError: userin.define_and_execute( [" "], "Wikipedia connection error.") 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_and_execute([" "], notify) userin.say(message) except BaseException: 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_and_execute( ["sensible-browser", tab_url], search_query) 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() arithmetic_response = arithmeticParser(com) if arithmetic_response: userin.define([" "], " ") userin.say(arithmetic_response) else: learn_response = learn_.respond(original_com) if learn_response: userin.define([" "], " ") userin.say(learn_response) else: if not omniscient_.respond(original_com, not args["silent"], userin, user_prefix, args["server"]): dc_response = dc.respond(original_com, user_prefix) if dc_response: userin.define([" "], " ") userin.say(dc_response)
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)
class ControlMainClass(): def __init__(self): self.osName = platform.system() self.pcName = os.environ['computername'] self.screenSize = pyautogui.size() print "Operation system is: ", self.osName print "PC name is: ", self.pcName self.mouseMap = ['left', 'middle', 'right'] self.controller = pyautogui self.pyMouse = PyMouse() self.pyKeyboard = PyKeyboard() self.keyboardMap = {} self.initKeyboardMap() def __del__(self): pass def initKeyboardMap(self): self.keyboardMap[3] = ' ' self.keyboardMap[10] = '\'' self.keyboardMap[15] = ',' self.keyboardMap[16] = '-' self.keyboardMap[17] = '.' self.keyboardMap[18] = '/' self.keyboardMap[19] = '0' self.keyboardMap[20] = '1' self.keyboardMap[21] = '2' self.keyboardMap[22] = '3' self.keyboardMap[23] = '4' self.keyboardMap[24] = '5' self.keyboardMap[25] = '6' self.keyboardMap[26] = '7' self.keyboardMap[27] = '8' self.keyboardMap[28] = '9' self.keyboardMap[30] = ';' self.keyboardMap[32] = '=' self.keyboardMap[36] = '[' self.keyboardMap[37] = '\\' self.keyboardMap[38] = ']' self.keyboardMap[41] = '`' self.keyboardMap[42] = 'a' self.keyboardMap[43] = 'b' self.keyboardMap[44] = 'c' self.keyboardMap[45] = 'd' self.keyboardMap[46] = 'e' self.keyboardMap[47] = 'f' self.keyboardMap[48] = 'g' self.keyboardMap[49] = 'h' self.keyboardMap[50] = 'i' self.keyboardMap[51] = 'j' self.keyboardMap[52] = 'k' self.keyboardMap[53] = 'l' self.keyboardMap[54] = 'm' self.keyboardMap[55] = 'n' self.keyboardMap[56] = 'o' self.keyboardMap[57] = 'p' self.keyboardMap[58] = 'q' self.keyboardMap[59] = 'r' self.keyboardMap[60] = 's' self.keyboardMap[61] = 't' self.keyboardMap[62] = 'u' self.keyboardMap[63] = 'v' self.keyboardMap[64] = 'w' self.keyboardMap[65] = 'x' self.keyboardMap[66] = 'y' self.keyboardMap[67] = 'z' self.keyboardMap[75] = self.pyKeyboard.alt_l_key self.keyboardMap[76] = self.pyKeyboard.alt_r_key self.keyboardMap[78] = self.pyKeyboard.backspace_key self.keyboardMap[90] = self.pyKeyboard.control_l_key self.keyboardMap[91] = self.pyKeyboard.control_r_key self.keyboardMap[93] = self.pyKeyboard.delete_key self.keyboardMap[94] = self.pyKeyboard.delete_key self.keyboardMap[96] = self.pyKeyboard.down_key self.keyboardMap[97] = self.pyKeyboard.end_key self.keyboardMap[98] = self.pyKeyboard.enter_key self.keyboardMap[99] = self.pyKeyboard.escape_key self.keyboardMap[102] = self.pyKeyboard.function_keys[1] self.keyboardMap[103] = self.pyKeyboard.function_keys[10] self.keyboardMap[104] = self.pyKeyboard.function_keys[11] self.keyboardMap[105] = self.pyKeyboard.function_keys[12] self.keyboardMap[113] = self.pyKeyboard.function_keys[2] self.keyboardMap[119] = self.pyKeyboard.function_keys[3] self.keyboardMap[120] = self.pyKeyboard.function_keys[4] self.keyboardMap[121] = self.pyKeyboard.function_keys[5] self.keyboardMap[122] = self.pyKeyboard.function_keys[6] self.keyboardMap[123] = self.pyKeyboard.function_keys[7] self.keyboardMap[124] = self.pyKeyboard.function_keys[8] self.keyboardMap[125] = self.pyKeyboard.function_keys[9] if self.osName == "Windows": self.keyboardMap[129] = self.pyKeyboard.hangul_key elif self.osName == "Linux": self.keyboardMap[129] = -1 pass self.keyboardMap[132] = self.pyKeyboard.home_key self.keyboardMap[141] = self.pyKeyboard.left_key self.keyboardMap[146] = '0' self.keyboardMap[147] = '1' self.keyboardMap[148] = '2' self.keyboardMap[149] = '3' self.keyboardMap[150] = '4' self.keyboardMap[151] = '5' self.keyboardMap[152] = '6' self.keyboardMap[153] = '7' self.keyboardMap[154] = '8' self.keyboardMap[155] = '9' self.keyboardMap[156] = self.pyKeyboard.num_lock_key self.keyboardMap[157] = self.pyKeyboard.page_down_key self.keyboardMap[158] = self.pyKeyboard.page_up_key self.keyboardMap[160] = self.pyKeyboard.page_down_key self.keyboardMap[161] = self.pyKeyboard.page_up_key self.keyboardMap[170] = self.pyKeyboard.right_key self.keyboardMap[171] = self.pyKeyboard.scroll_lock_key self.keyboardMap[175] = self.pyKeyboard.shift_l_key self.keyboardMap[176] = self.pyKeyboard.shift_r_key self.keyboardMap[180] = self.pyKeyboard.tab_key self.keyboardMap[181] = self.pyKeyboard.up_key pass def command(self, data): if data[0] == 'm': x = (ord(data[3]) * 100) + ord(data[4]) y = (ord(data[5]) * 100) + ord(data[6]) #print "x:",x," y:",y #self.controller.moveTo( x, y) # x,y self.pyMouse.move(x, y) if data[1] == 'p': #self.controller.mouseDown(x,y,self.mouseMap[ord(data[2])]) # x,y,b #print "press" self.pyMouse.press(x, y, ord(data[2])) pass elif data[1] == 'r' and ord(data[2]) != 0: #self.controller.mouseUp(x,y,self.mouseMap[ord(data[2])]) # x,y,b #print "release" self.pyMouse.release(x, y, ord(data[2])) pass if data[7] == 's': if data[8] == 'u': #self.controller.scroll(10) self.pyMouse.scroll(vertical=10) pass else: #self.controller.scroll(-10) self.pyMouse.scroll(vertical=-10) pass pass else: print 'data: ', data[0], ' ' + data[1], ' ', ord(data[2]) keyCode = ord(data[2]) if data[1] == 'p' and keyCode != 0: #self.controller.keyDown(self.controller.KEYBOARD_KEYS[ord(data[2])]) print 'press ' + str(ord(data[2])) if self.keyboardMap[ord(data[2])] != -1: self.pyKeyboard.press_key(self.keyboardMap[ord(data[2])]) pass elif data[1] == 'r' and keyCode != 0: #self.controller.keyUp(self.controller.KEYBOARD_KEYS[ord(data[2])]) print 'release ' + str(ord(data[2])) if self.keyboardMap[ord(data[2])] != -1: self.pyKeyboard.release_key(self.keyboardMap[ord(data[2])]) pass pass