def test_get_key(self, devices): """Get key reads from the first keyboard.""" keyboard = mock.MagicMock() reader = mock.MagicMock() keyboard.read = reader devices.keyboards = [keyboard] inputs.get_key() reader.assert_called_once()
def handle_inputs(engine): """Handle user input using the https://github.com/zeth/inputs This may be helpful if the user interface library you are using doesn't provide its own way of accessing the inputs. """ from inputs import get_key history = {} while True: events = get_key() for event in events: if event.code == 'KEY_LEFT': if history.get(event.code, 0) != event.state: engine.direction(left=True, pressed=event.state == 1) history[event.code] = event.state if event.code == 'KEY_RIGHT': if history.get(event.code, 0) != event.state: engine.direction(right=True, pressed=event.state == 1) history[event.code] = event.state if event.code == 'KEY_SPACE' and event.state == 1: if history.get('KEY_SPACE', 0) != event.state: engine.fire() if event.code == 'KEY_ESC': return True
def main(): pygame.midi.init() player = pygame.midi.Output(0) player.set_instrument(0) print("started") sd = -1 prev = -1 t0 = 0 while 1: events = get_key() if events: for event in events: sd = event.state break print(prev, sd) if prev == sd: if t0 == 0: t0 = time.time() else: t1 = time.time() player.note_on(prev + 36, 127) if t0 == 0: t0 = t1 - 0.1 print(t1 - t0) time.sleep(t1 - t0) player.note_off(prev + 36, 127) prev = sd t0 = 0
def main(): """Just print out some event infomation when keys are pressed.""" while 1: events = get_key() if events: for event in events: print(event.ev_type, event.code, event.state)
def keyPressed(event, data): print("hello1!") events = get_key() for a in events: if a.code == "BTN_SOUTH" and event.state == 1: data.clicked = True print("hello!")
def main(): """Just print out some event infomation when keys are pressed.""" while 1: events = get_key() if events: for event in events: print(event.ev_type, event.code, event.state)
def run(self): while True: events = get_key() for evt in events: if evt.ev_type == "Key": if evt.state == 1: print(evt.code) if evt.code == "KEY_A": self.key_a = True elif evt.code == "KEY_S": self.key_s = True elif evt.code == "KEY_RESERVED": self.key_d = True elif evt.code == "KEY_W": self.key_w = True elif evt.state == 0: if evt.code == "KEY_A": self.key_a = False elif evt.code == "KEY_S": self.key_s = False elif evt.code == "KEY_RESERVED": self.key_d = False elif evt.code == "KEY_W": self.key_w = False if self.key_change_handler is not None: self.key_change_handler(self.get_keys_output())
def update(self): if self.comport == 'keyboard_legacy': return try: kb_event = None for d in devices.keyboards: if d._device_path == self.comport: kb_event = d.read() if kb_event is None: kb_event = get_key() for e in kb_event: val = False if e.state > 0: val = True if e.ev_type == 'Key': self.on_key_data({ 'type': 'button', 'name': e.code, 'val': val }) #mouse_event = get_mouse() #for e in mouse_event: # print(e.ev_type, e.code, e.state) #controller_event = get_gamepad() #for e in controller_event: # print(e.ev_type, e.code, e.state) except UnpluggedError as e: util.logger.error(str(e))
def event(self): while 1: keys = get_key() for key in keys: if key.ev_type == "Key": if key.state == PRESSED and key.code not in self.pressed: self.pressed.append(key.code) elif key.state == RELEASED: if key.code in self.pressed: self.pressed.remove(key.code) return self.__handle_key(key.code)
def run(self): while not self._done: events = get_key() self.keys = [] for event in events: if event.state == 1 and event.ev_type == "Key": if event.code == 'KEY_T': self.keys.append('T') elif event.code == 'KEY_Q': self.keys.append('Q') print("KeyboardListener thread exiting!!!")
def update_key(self): prefix = "KEY_" while True: events = get_key() for event in events: for c in self.codes: if prefix + c == event.code and self.codes.index(c) < self.bot_num: self.intended_index = self.codes.index(c) if prefix + 'LEFTBRACE' == event.code: self.training_paused = False if prefix + 'RIGHTBRACE' == event.code: self.training_paused = True
def update_keys(): global global_key key_presses = ["KEY_UP", "KEY_RIGHT", "KEY_DOWN", "KEY_LEFT"] while True: events = get_key() for event in events: if event.code in key_presses and event.state == 1: print("Got input: ", event.code) key_mutex = False global_key = (event.code, time.time()) key_mutex = True
def emit(self): try: events = get_key() for event in events: self.queue.put( Event( Event.KEYBOARD, event.code, {"ev_type": event.ev_type, "state": event.state, "code": event.code}, ) ) except Exception: pass
def main(): """Just print out some event infomation when keys are pressed.""" global switchBool global x while True: events = get_key() if events: x = int(events[0].state) switchBool = not switchBool if switchBool: print(x)
def main(): global switchBool global x z = 0 a = 0 c = 0 d = 0 e = 0 plt.axis([0, 100, 0, 50]) i = 0 # for i in range(50): while i < 1000: i = i + 1 events = get_key() if events: x = int(events[0].state) switchBool = not switchBool if switchBool: d = x if x == 72: a = 1 elif x == 80: a = -1 elif x == 77: a = 0 z = z + a plt.plot(i / 10, z, 'ro') plt.plot(c / 10, e, 'bo') print(i / 10, z, "heyo") print(c / 10, e, "oyeh") c = i e = z print(i / 10, z, " heyo") print(c / 10, e, " oyeh") plt.pause(0.001) print(i) plt.copper() plt.autoscale(enable=True, axis='both', tight=False) #plt.autoscale(enable=True, axis='y', tight=False) plt.show()
def hi(): global end last = "" #infinite loop. while not end: #get events from the gamepad events = get_key() if events: for event in events: x = re.search("KEY_\S", str(event.code)) if x != None and last != x.group(): #print(x.group()) lbl['text']=x.group() last = x.group() return
def speedrun(): pz.stop() mylcd.lcd_display_string("Speed Run ", 1) mylcd.lcd_display_string("Press E to End ", 2) time.sleep(2) speed = 100 GO = 1 PREP = 1 while PREP == 1: #get ready to go for event in get_key(): mylcd.lcd_display_string("Press G to GO ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_G": PREP = 0 elif event.code == "KEY_E": pz.stop GO = 0 PREP = 0 while GO == 1: #new simple quit - works perfectly if button.is_pressed: pz.stop() GO = 0 else: RIGHTIR = pz.readInput(0) LEFTIR = pz.readInput(1) if RIGHTIR == 0: pz.spinLeft(75) mylcd.lcd_display_string("Right IR on ", 1) mylcd.lcd_display_string("Use switch=STOP ", 2) elif LEFTIR == 0: pz.spinRight(75) mylcd.lcd_display_string("left IR on ", 1) mylcd.lcd_display_string("Use switch=STOP ", 2) elif RIGHTIR == 1 and LEFTIR == 1: pz.forward(100) mylcd.lcd_display_string("No IR on ", 1) mylcd.lcd_display_string("Use switch=STOP ", 2)
def handle_inputs(engine): """Handle user input using the https://github.com/zeth/inputs This may be helpful if the user interface library you are using doesn't provide its own way of accessing the inputs. """ from inputs import get_key history = {} while True: events = get_key() for event in events: if event.code == 'KEY_LEFT': if history.get(event.code, 0) != event.state: engine.direction(left=True, pressed=event.state == 1) history[event.code] = event.state if event.code == 'KEY_RIGHT': if history.get(event.code, 0) != event.state: engine.direction(right=True, pressed=event.state == 1) history[event.code] = event.state if event.code == 'KEY_ESC': return True
def linefollower(): #works perfectly on a new set of batteries mylcd.lcd_display_string("Line Follower ", 1) mylcd.lcd_display_string("Press E to End ", 2) PREP = 1 GO = 1 LFSPEED = 75 #only changes cornering speed pz.stop() while PREP == 1: #setup ready for line following for event in get_key(): mylcd.lcd_display_string("Press G to GO ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_G": PREP = 0 elif event.code == "KEY_E": GO = 0 PREP = 0 while GO == 1: #new simple quit - works perfectly if button.is_pressed: pz.stop() GO = 0 else: mylcd.lcd_display_string("GO!!!!!!!!!!! ", 1) mylcd.lcd_display_string("Switch = stop ", 2) LEFTLINE = pz.readInput(2) #assign right line sensor to a variable RIGHTLINE = pz.readInput(3) #assign left line sensor to a variable if RIGHTLINE == 1: pz.spinRight(LFSPEED) #time.sleep(0.5) elif LEFTLINE == 1: pz.spinLeft(LFSPEED) #time.sleep(0.5) elif LEFTLINE == 0 and RIGHTLINE == 0: pz.forward(15) #time.sleep(0.5) elif LEFTLINE == 1 and RIGHTLINE == 1: pz.reverse(15)
def main(): global switchBool global x z =0 a = 0 c = 0 d = 0 plt.axis([0, 100, 0, 50]) i = 0 # for i in range(50): while i < 1000: i = i + 1 events = get_key() if events: x = int(events[0].state) switchBool = not switchBool if switchBool: d = x if x == 72: a = 1 elif x == 80: a = -1 y = m.sin(i/10) z = d b = (c + a)*y c = b print("x ",x) print("b" ,b) plt.scatter(i/2, b) plt.pause(0.001) print(i) plt.copper() plt.autoscale(enable=True, axis='both', tight=False) plt.show()
def main(): global switchBool global x # initializing variables z = 0 a = 0 c = 0 d = 0 e = 0 h = 0 xp = 0 yp = 0 xv = 0 # x velocity yv = 0 # y velocity incrementVar = 0 plt.axis([0, 100, 0, 100]) i = 0 # for i in range(50): <-- early attempt at a for loop while i < 1000: i = i + 1 events = get_key() if events: x = int(events[0].state) switchBool = not switchBool # attempt at creating an acceleration vector if switchBool: d = x # values thrown from arrow keys if x == 72: xv = 1 yv = 0 if x == h: xp = xp + (2 * xv) else: xp = xp + xv elif x == 80: xv = -1 yv = 0 if x == h: xp = xp + (2 * xv) else: xp = xp + xv elif x == 77: yv = 1 xv = 0 xp = xp + xv yp = yp + yv elif x == 75: yv = -1 xv = 0 xp = xp + xv yp = yp + yv # creating red circule object twice, to maintain # shape while moving by creating an empty circle # overtop the previous, twice plt.plot(yp, xp, 'ro', markersize=15) plt.plot(c, e, 'wo', markersize=16) plt.plot(yp, xp, 'ro', markersize=15) c = yp e = xp plt.pause(0.001) #print(i) h = x plt.show()
print(device) GPIO.setup("CSID4", GPIO.OUT, initial=1) GPIO.setup("CSID6", GPIO.OUT, initial=1) current_state = 0 unlock_time = 0 while True: print("current state: ", current_state) if ((time.time() - unlock_time) < 5.0): print("Unlocking for 5 seconds") GPIO.output("CSID4", GPIO.LOW) GPIO.output("CSID6", GPIO.LOW) else: GPIO.output("CSID4", GPIO.HIGH) GPIO.output("CSID6", GPIO.HIGH) events = get_key() for event in events: print(event.ev_type, event.code, event.state) if event.state == 1: if current_state == 0 and event.code == 'KEY_D': current_state = 1 elif current_state == 1 and event.code == 'KEY_A': current_state = 2 elif current_state == 2 and event.code == 'KEY_N': current_state = 3 unlock_time = time.time() else: current_state = 0
def remotecontrol(): #works perfect pz.stop() mylcd.lcd_display_string("Remote Control ", 1) mylcd.lcd_display_string("Press E to End ", 2) time.sleep(2) speed = 100 turnspeed = 100 mylcd.lcd_display_string("Speed = %d " % speed, 1) mylcd.lcd_display_string("Press E to End ", 2) GO = 1 BURST = 0.2 while GO == 1: for event in get_key(): if event.code == "KEY_UP": if event.state == 1: pz.forward(speed) time.sleep(BURST) pz.stop() elif event.state == 2: pz.forward(speed) elif event.state == 0: pz.stop() if event.code == "KEY_DOWN": if event.state == 1: pz.reverse(speed) time.sleep(BURST) pz.stop() elif event.state == 2: pz.reverse(speed) elif event.state == 0: pz.stop() if event.code == "KEY_RIGHT": if event.state == 1: pz.spinRight(speed) time.sleep(BURST) pz.stop() elif event.state == 2: pz.spinRight(speed) elif event.state == 0: pz.stop() if event.code == "KEY_LEFT": if event.state == 1: pz.spinLeft(speed) time.sleep(BURST) pz.stop() elif event.state == 2: pz.spinLeft(speed) elif event.state == 0: pz.stop() if event.code == "KEY_DOT" or event.code == "KEY_>": if event.state == 1 or event.state == 2: speed = min(100, speed + 10) turnspeed = min(100, turnspeed + 10) mylcd.lcd_display_string("Speed = %d " % speed, 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_COMMA" or event.code == "KEY_<": if event.state == 1 or event.state == 2: speed = max(0, speed - 10) turnspeed = max(0, turnspeed - 10) mylcd.lcd_display_string("Speed = %d " % speed, 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_Q": if event.state == 1 or event.state == 2: speed = 75 turnspeed = 100 mylcd.lcd_display_string("Fast Maze ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_W": if event.state == 1 or event.state == 2: speed = 60 turnspeed = 80 mylcd.lcd_display_string("slow Maze ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_SPACE": if event.state == 1 or event.state == 2: pz.stop() if event.code == "KEY_E": if event.state == 1 or event.state == 2: pz.stop() GO = 0
v = 1. t = 50 coords = [] while True: x, y, z, phi = arm.coords c.append(pos=vector(x, y, z)) L.text = '\u03B8=[%.1f, %.1f, %.1f, %.1f]' \ '\nx=%.3f cm' \ '\ny=%.3f cm' \ '\nz=%.3f cm' \ '\n\u03D5=%.2f' % (*arm.thetas[:GRIP], *arm.coords) event, = get_key() key = event.code # stop program if key == 'KEY_ESC': arm.close() stop_server() break # movement keys if key == 'KEY_A': arm.increment(BASE, v, t) if key == 'KEY_Z': arm.increment(BASE, -v, t) if key == 'KEY_S': arm.increment(SHOULDER, v, t) if key == 'KEY_X': arm.increment(SHOULDER, -v, t)
def automaze(): pz.stop() mylcd.lcd_display_string("Auto Maze ", 1) mylcd.lcd_display_string("Press E to End ", 2) time.sleep(2) MSPEED = 20 MTURN = 90 GO = 1 PREP = 1 STEP = 0 # start step count while PREP == 1: #get ready to go for event in get_key(): mylcd.lcd_display_string("Press G to GO ", 1) mylcd.lcd_display_string("Press E to End ", 2) if event.code == "KEY_G": PREP = 0 elif event.code == "KEY_E": GO = 0 PREP = 0 while GO == 1: #new simple quit - works perfectly if button.is_pressed: pz.stop() GO = 0 else: RIGHTIR = pz.readInput(0) #assign right IR to a variable LEFTIR = pz.readInput(1) #assign left IR to a variable RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) pz.forward(MSPEED) #steps to follow to complete the maze if RANGE < 25 and STEP == 0: #first right turn pz.stop() pz.spinRight(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 1 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 1: #second right turn pz.stop() pz.spinRight(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 2 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 2: #third right pz.stop() pz.spinRight(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 3 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 3: #first left pz.stop() pz.spinLeft(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 4 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 4: #second left pz.stop() pz.spinLeft(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 5 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable if RANGE < 25 and STEP == 5: #third left pz.stop() pz.spinLeft(MTURN) time.sleep(0.5) pz.stop() pz.forward(MSPEED) time.sleep(1) STEP = 6 mylcd.lcd_display_string("Range = %d %%" % RANGE, 1) mylcd.lcd_display_string("Step = %d %%" % STEP, 2) RANGE = hcsr04.getDistance() #assign HC-SR04 range to variable #emergency wall avoidance protocol if LEFTIR == 0: pz.spinRight(100) time.sleep(0.01) elif RIGHTIR == 0: pz.spinLeft(100) time.sleep(0.01)
mylcd.lcd_display_string("Use switch=STOP ", 2) #end functions #_____________________________________________________________________________ #____________________________________________________________________________________ #Main Loop mylcd.lcd_display_string("Select Option ", 1) mylcd.lcd_display_string("S to Shutdown ", 2) MAINLOOP = 1 try: while True: for event in get_key(): mylcd.lcd_display_string("Select Option ", 1) mylcd.lcd_display_string("S to Shutdown ", 2) if event.code == "KEY_S": mylcd.lcd_display_string("Shutting Down ", 1) mylcd.lcd_display_string(" ", 2) pz.stop() pz.cleanup() os.system("sudo shutdown -h now") elif event.code == "KEY_ESC": mylcd.lcd_display_string("Ending Program ", 1) mylcd.lcd_display_string(" ", 2) pz.stop() pz.cleanup() sys.exit() elif event.code == "KEY_1":
def _main_loop(self): while self._run: events = inputs.get_key() for event in events: if event.ev_type == 'Key' and event.state: self._count += 1
def handle_events(self): events = inputs.get_key() if events: for event in events: self.handle_event(event)
def test_get_key_index_error(self, devices): """Raises unpluggged error if no keyboard attached.""" devices.keyboards = [] with self.assertRaises(inputs.UnpluggedError): # pylint: disable=pointless-statement inputs.get_key()