class ButtonModel: def __init__(self): #read json button defs f = open("/buttons.json") # load buttons pages self.buttonPages = json.load(f) f.close() for buttonPage in self.buttonPages["buttons"]: print("Button Page : %s" % (buttonPage["name"])) parseButtons(buttonPage) self.LoadButtonPage(0) self.currButtonPageIndex = 0 #setup keyboard device self.keyboard = Keyboard(usb_hid.devices) self.cc = ConsumerControl(usb_hid.devices) # Loads the button page specified by the index def LoadButtonPage(self, newButtonPageIndex): self.currButtonDef = self.buttonPages["buttons"][newButtonPageIndex] #print("Changeing BGImg To " + buttonDef['img'] ) #imgbg=Image.open(buttonDef['img']) print("Loaded Button Page - %s" % (self.currButtonDef["name"])) #Flip the button page index def FlipButtonPage(self, changeIndex): self.currButtonPageIndex += changeIndex if self.currButtonPageIndex < 0: self.currButtonPageIndex = len(self.buttonPages['buttons']) - 1 elif self.currButtonPageIndex >= len(self.buttonPages['buttons']): self.currButtonPageIndex = 0 self.LoadButtonPage(self.currButtonPageIndex) # Button presed on current page def ButtonPressed(self, keyIndex): button = self.currButtonDef.get(keyIndex, None) if button != None and button['keycodes'] != None: print(keyIndex + "pressed") if (button["isCC"]) == False: self.keyboard.send(*button['keycodes']) time.sleep(150 / 1000) else: self.cc.send(button["keycodes"][0]) #Current Page Dictionary @property def CurrentButtons(self): return self.currButtonDef
import time import board import digitalio import usb_hid from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode kbd = Keyboard(usb_hid.devices) # define buttons. these can be any physical switches/buttons, but the values # here work out-of-the-box with a CircuitPlayground Express' A and B buttons. swap = digitalio.DigitalInOut(board.D4) swap.direction = digitalio.Direction.INPUT swap.pull = digitalio.Pull.DOWN search = digitalio.DigitalInOut(board.D5) search.direction = digitalio.Direction.INPUT search.pull = digitalio.Pull.DOWN while True: # press ALT+TAB to swap windows if swap.value: kbd.send(Keycode.ALT, Keycode.TAB) # press CTRL+K, which in a web browser will open the search dialog elif search.value: kbd.send(Keycode.CONTROL, Keycode.K) time.sleep(0.1)
# if your camera was muted... if vid_mute: print("top") # your camera is NOT muted vid_mute = False # resets escape state just in case escape_1 = False # if your camera was NOT muted... elif not vid_mute: print("top") # your camera is muted vid_mute = True # resets escape state just in case escape_1 = False # sends camera mute/unmute shortcut keyboard.send(alt_key, Keycode.V) # if you press the top touch cap touch pad... if (bot_touch.value and not bot_pressed): bot_pressed = True # start time count for exit clock = time.monotonic() # slight delay so that you don't automatically mute/unmute # if your intent is to exit time.sleep(0.12) # if after the delay you're still pressing the cap touch pad... if bot_touch.value and bot_pressed: print("escape bot") # initial escape state is set to true escape_2 = True # if you aren't still pressing the cap touch pad... else:
def keyboard_config(key_to_be_pressed): kbd = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(kbd) kbd.send(key_to_be_pressed)
event = keys.events.get() # wakeup if knob turned or button pressed if knob.position != knob_pos or event: if not awake: last_update = 0 # force an update awake = True knob_pos = knob.position wake_up_time = now # handle key presses if event: if event.pressed: key_pressed = event.key_number # knob if key_pressed == 12: keyboard_layout.write(totp_codes[current_key]) keyboard.send(Keycode.ENTER) # keeb elif key_pressed != current_key: # is it a configured key? if totp_keys[key_pressed]: current_key = key_pressed pixels.fill(0) last_update = 0 # force an update # update codes if progress_bar.value < 0.5 and now - last_compute > 2: totp_codes = compute_codes(timebase(now)) last_compute = now # update display if now - last_update > DISPLAY_RATE and awake: pixels[current_key] = totp_keys[current_key][2] name.text = totp_keys[current_key][0][:18]
import usb_hid from adafruit_circuitplayground.express import cpx from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode kbd = Keyboard(usb_hid.devices) while True: if cpx.button_a: kbd.send(Keycode.SHIFT, Keycode.A) # Type capital 'A' while cpx.button_a: # Wait for button to be released pass if cpx.button_b: kbd.send(Keycode.CONTROL, Keycode.X) # control-X key while cpx.button_b: # Wait for button to be released pass
num_lock = False # NUM LOCK caps_lock = False # CAPS LOCK scroll_lock = False # SCROLL LOCK def update_lock(): global num_lock, caps_lock, scroll_lock report = int.from_bytes(usb_hid.devices[0].last_received_report, "big") num_lock = (report & 0x01) == 0x01 # NUM LOCK caps_lock = (report & 0x02) == 0x02 # CAPS LOCK scroll_lock = (report & 0x04) == 0x04 # SCROLL LOCK update_lock() if not scroll_lock: # Force the Scroll lock to be ON kbd.send(Keycode.SCROLL_LOCK) print("Scroll lock forced.") time.sleep(0.5) print("Press 'Scroll lock' to start the meeting timer." ) # Wait for the Scroll Lock to be disabled to start the timer. scroll_lock = True while scroll_lock: update_lock() time.sleep(0.1) print("Meeting starting") start = time.monotonic() # Press reset on your board to reset the time previous_scroll_lock = False
DASH_DURATION = 0.5 button_a = DigitalInOut(board.A4) button_a.direction = Direction.INPUT button_a.pull = Pull.UP button_b = DigitalInOut(board.A3) button_b.direction = Direction.INPUT button_b.pull = Pull.UP kbd = Keyboard() def touch_a(): return not button_a.value def touch_b(): return not button_b.value while True: if touch_a(): kbd.send(Keycode.PERIOD) while touch_a(): pass elif touch_b(): kbd.send(Keycode.MINUS) while touch_b(): pass
class M2PicoMacro: sysKeyActions={ 'key1':'showlist', #shows the list page 'key2':'flipback', #Cycle to previous page 'key3':'showlist', 'key4':'flip' #Cycle to next page } listKeyActions={ 'key1':'up', 'key2':'down', 'key3':'select', 'key4':'select' } # ui = ButtonPageUI object def __init__(self,display): self._display=display #read json button defs f=open("/buttons.json") # load buttons pages self.buttonPages=json.load(f) f.close() for buttonPage in self.buttonPages["buttons"]: print("Button Page : %s"%(buttonPage["name"])) parseButtons(buttonPage) self.currButtonPageIndex=0 #setup keyboard device self.keyboard=Keyboard(usb_hid.devices) self.cc=ConsumerControl(usb_hid.devices) #init button ui self.ui=ButtonPageUI() #init list ui self.uilist=ButtonListUI(self.buttonPages["buttons"]) self.listVisible=False #Initialize first button page self.LoadButtonPage(0) self._display.show(self.ui.group) # Loads the button page specified by the index def LoadButtonPage(self,newButtonPageIndex): self.currButtonDef=self.buttonPages["buttons"][newButtonPageIndex] self.ui.LoadButtonPage(self.CurrentButtons) #print("Loaded Button Page - %s"%(self.currButtonDef["name"])) #Flip the button page index def FlipButtonPage(self,changeIndex,changePage=True): self.currButtonPageIndex+=changeIndex if self.currButtonPageIndex < 0: self.currButtonPageIndex=len(self.buttonPages['buttons'])-1 elif self.currButtonPageIndex>=len(self.buttonPages['buttons']): self.currButtonPageIndex=0 if changePage==True: self.LoadButtonPage(self.currButtonPageIndex) #Button pressed down def ButtonDown(self,keyIndex): self.ui.SetButtonState(keyIndex,True) pass #Button released def ButtonUp(self,keyIndex): self.ui.SetButtonState(keyIndex,False) pass # Button presed on current page def ButtonPressed(self,keyIndex): if self.listVisible==True: self.ListButtonPressed(keyIndex) else: button = self.currButtonDef.get(keyIndex,None) if button != None and button['keycodes'] != None: print(keyIndex + "pressed") if(button["isCC"])==False: self.keyboard.send(*button['keycodes']) time.sleep(150/1000) else: self.cc.send(button["keycodes"][0]) # Button pressed in list mode def ListButtonPressed(self,keyIndex): listAction=self.listKeyActions[keyIndex] self._display.auto_refresh=False if listAction=='up': self.FlipButtonPage(-1,False) self.uilist.setActiveItem(self.currButtonPageIndex) elif listAction=='down': self.FlipButtonPage(1,False) self.uilist.setActiveItem(self.currButtonPageIndex) elif listAction=="select": self.LoadButtonPage(self.currButtonPageIndex) self._display.show(self.ui.group) self.listVisible=False self._display.auto_refresh=True time.sleep(50/1000) #Long press of a button def ButtonLongPressed(self,keyIndex): sysaction=self.sysKeyActions[keyIndex] print("Sys Key action :" + sysaction) self._display.auto_refresh=False if sysaction=='flip': self.FlipButtonPage(1) elif sysaction=='flipback': self.FlipButtonPage(-1) elif sysaction=="showlist": #show page list self.uilist.setActiveItem(self.currButtonPageIndex) self._display.show(self.uilist.group) self.listVisible=True self._display.auto_refresh=True time.sleep(150/1000) #Current Page Dictionary @property def CurrentButtons(self): return self.currButtonDef
# cols = [digitalio.DigitalInOut(x) for x in (board.D11, board.D12, board.D13)] # rows = [digitalio.DigitalInOut(x) for x in (board.D7, board.D9, board.D10)] # keys = ( # (6,7,8), # (3,4,5), # (0,1,2), # ) keypad = adafruit_matrixkeypad.Matrix_Keypad(rows, cols, keys) kbd = Keyboard(usb_hid.devices) AVAILABLE_KEYS = [ Keycode.ONE, Keycode.TWO, Keycode.THREE, Keycode.FOUR, Keycode.FIVE, Keycode.SIX Keycode.SEVEN, Keycode.EIGHT, Keycode.NINE ] while True: keys = keypad.pressed_keys if keys: print(keys) for key in keys: kbd.send(AVAILABLE_KEYS[key]) time.sleep(0.1)
# Create MPR121 object. mpr121 = adafruit_mpr121.MPR121(i2c) # Note you can optionally change the address of the device: # mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91) kbd = Keyboard(usb_hid.devices) keylist = [ Keycode.RIGHT_ARROW, Keycode.ONE, Keycode.TWO, Keycode.THREE, Keycode.FOUR, Keycode.W, Keycode.A, Keycode.S, Keycode.D, Keycode.B, Keycode.I, Keycode.H, ] # Loop forever testing each input and sending keystrokes when they're touched. while True: # Loop through all 12 inputs (0-11). for i in range(12): # Call is_touched and pass it then number of the input. If it's touched # it will return True, otherwise it will return False. if mpr121[i].value: # print("Input {} touched!".format(i)) kbd.send(keylist[i]) time.sleep(0.15) # Small delay to keep from spamming output messages.
# create keypad keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True) p = 0 # variable for tilegrid index while True: # get keypad inputs event = keys.events.get() if event: # if a key is pressed.. if event.pressed: # if a midi keyboard if midi_mode: # send note number midi.send(NoteOn(midi_notes[event.key_number], 120)) # if hid keyboard if keyboard_mode: # send hid keyboard shortcut keyboard.send(ctrl, shortcuts[event.key_number]) # advance parrot index p = (p + 1) % 10 # update parrot bitmap parrot0_grid[0] = p # if a key is released if event.released: # if a midi keyboard if midi_mode: # send note off message midi.send(NoteOff(midi_notes[event.key_number], 120))
# Above we define output_values as 0 through 8. 0 will point to Keycode.ONE and so one. # For more Keycodes, check here: https://circuitpython.readthedocs.io/projects/hid/en/latest/index.html KEYBOARD_KEYS = [ Keycode.ONE, Keycode.TWO, Keycode.THREE, Keycode.FOUR, Keycode.FIVE, Keycode.SIX, Keycode.SEVEN, Keycode.EIGHT, Keycode.NINE, ] # The following code sets up the ItsyBitsy as a keyboard kbd = Keyboard(usb_hid.devices) # This code will run forever since while True is always True while True: # Check for the keys being pressed pressed_keys = keypad.pressed_keys if len(pressed_keys) > 0: # This is a helpful way to debug if the keys are being pressed. # Open The Serial from the menu above in Mu to see this. print(pressed_keys) for pressed_key in pressed_keys: # For each pressed key kbd.send( KEYBOARD_KEYS[pressed_key]) # Send the value to the computer time.sleep(0.1) # Wait for a brief period before continuing
if not ble.connected: print("advertising") ble.start_advertising(advertisement, scan_response) else: print("already connected") print(ble.connections) k = Keyboard(hid.devices) kl = KeyboardLayoutUS(k) pressedButton = False pulledSwitch = False while True: while not ble.connected: pass print("Start typing:") while ble.connected: if pull_cord.value and not pulledSwitch: k.send(Keycode.COMMAND, Keycode.F6) print("ENDING ZOOM CALL!!!!!!!!") pulledSwitch = True time.sleep(0.4) if not pull_cord.value and pulledSwitch: k.send(Keycode.COMMAND, Keycode.F6) pulledSwitch = False time.sleep(0.4) ble.start_advertising(advertisement)
import time import board import digitalio from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode kbd = Keyboard() # define buttons. these can be any physical switches/buttons, but the values # here work out-of-the-box with a CircuitPlayground Express' A and B buttons. swap = digitalio.DigitalInOut(board.D4) swap.direction = digitalio.Direction.INPUT swap.pull = digitalio.Pull.DOWN search = digitalio.DigitalInOut(board.D5) search.direction = digitalio.Direction.INPUT search.pull = digitalio.Pull.DOWN while True: # press ALT+TAB to swap windows if swap.value: kbd.send(Keycode.ALT, Keycode.TAB) # press CTRL+K, which in a web browser will open the search dialog elif search.value: kbd.send(Keycode.CONTROL, Keycode.K) time.sleep(0.1)
ble.start_advertising(advertisement, scan_response) else: print("already connected") print(ble.connections) k = Keyboard(hid.devices) kl = KeyboardLayoutUS(k) while True: while not ble.connected: pass print("Start typing:") while ble.connected: if not button_1.value: # pull up logic means button low when pressed #print("back") # for debug in REPL k.send(Keycode.BACKSPACE) time.sleep(0.1) if not button_2.value: kl.write("Bluefruit") # use keyboard_layout for words time.sleep(0.4) if not button_3.value: k.send(Keycode.SHIFT, Keycode.L) # add shift modifier time.sleep(0.4) if not button_4.value: kl.write("e") time.sleep(0.4) if not button_5.value:
position_change = current_position - last_position if position_change > 0: for _ in range(position_change): m.move(0, 0, 1) print(current_position) elif position_change < 0: for _ in range(-position_change): m.move(0, 0, -1) print(current_position) last_position = current_position # Refresh (F5) if button5.value and button5_state is None: button5_state = "pressed" if not button5.value and button5_state == "pressed": print("Refresh #5 button pressed.") kbd.send(Keycode.F5) button5_state = None #Twitter if not button2.value and button2_state is None: button2_state = "pressed" if button2.value and button2_state == "pressed": print("Twitter button #2 pressed.") # 63 is F6, focus on the toolbar #kbd.press(Keycode.F6) # Type out Twitter #kbd.press(Keycode.T) #time.sleep(.1) #kbd.press(Keycode.W) #time.sleep(.1) #kbd.press(Keycode.I)
sleep(0.2) # --------------------------------------- # ------- BOUCLE PRINCIPALE ----------- # --------------------------------------- mesure = 1 print("debut des mesures") # Boucle de 10 mesures while mesure < 11: # Création et envoi au PC de la chaîne 'mesure x' où x est le numéro de # la mesure chaine_mesure = "mesure {}".format(mesure) ecriture_lente(chaine_mesure) # Changement de cellule clavier.send(Keycode.TAB) # mesure et envoi au PC de la valeur de l'accélération sur l'axe X valeur_accel_X = str(round(accelerometre.acceleration[0], 2)) print(valeur_accel_X) ecriture_lente(valeur_accel_X) # Changement de ligne et retour à la première colonne clavier.press(Keycode.DOWN_ARROW) sleep(0.2) clavier.press(Keycode.HOME) clavier.release_all() # incrémentation du numéro de la mesure mesure = mesure + 1 # Attente de 1s sleep(1.0) print("fin des mesures") led.value = False
kbd.press(keycode) kbd.release(keycode) kbd.release_all() while True: caps = read_caps() print(caps) # light up the matching LED for i,c in enumerate(caps): leds[i].value = c if caps[0]: if ENABLE_KEYBOARD: if OS == WINDOWS: type_alt_code(234) elif OS == MAC: kbd.send(Keycode.ALT, Keycode.Z) elif OS == LINUX: kbd.press(Keycode.CONTROL, Keycode.SHIFT) kbd.press(Keycode.U) kbd.release_all() kbd.send(Keycode.TWO) kbd.send(Keycode.ONE) kbd.send(Keycode.TWO) kbd.send(Keycode.SIX) kbd.send(Keycode.ENTER) if caps[1]: if ENABLE_KEYBOARD: if OS == WINDOWS: type_alt_code(230) elif OS == MAC: kbd.send(Keycode.ALT, Keycode.M)
D3.direction = Direction.INPUT D3.pull = Pull.UP # fourth button D4 = DigitalInOut(board.D4) D4.direction = Direction.INPUT D4.pull = Pull.UP # loop forever while True: if not D1.value: # configure device as keyboard kbd = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(kbd) kbd.send(Keycode.SPACE) # press the SPACEBAR time.sleep(debounce_time) # debounce delay if not D2.value: # configure device as mouse mouse = Mouse(usb_hid.devices) mouse.click(Mouse.LEFT_BUTTON) # press the Left MOUSE Button time.sleep(debounce_time) # debounce delay if not D3.value: # configure device as mouse mouse = Mouse(usb_hid.devices) mouse.move(-10) # move the mouse right 10 if not D4.value: # configure device as mouse mouse = Mouse(usb_hid.devices) mouse.move(10) # move the mouse left 10
(3,2): (Keycode.SHIFT, Keycode.ONE,), (3,3): (Keycode.ENTER,) } # what colors to use if an assigned/unassigned key is pressed VALID_KEY_COLOR = (0, 0, 255) INVALID_KEY_COLOR = (255, 0, 0) keypad = RGBKeypad() kbd = Keyboard(usb_hid.devices) while True: # loop through all the keys for key in keypad.keys: # has a key been pressed? if key.is_pressed(): # does this key have a keyboard mapping if (key.x, key.y) in KEYBOARD_MAP.keys(): # debug - print out the keyboard strokes # print(KEYBOARD_MAP[(key.x, key.y)]) key.color = VALID_KEY_COLOR # send the keyboard strokes kbd.send(*KEYBOARD_MAP[(key.x, key.y)]) else: key.color = INVALID_KEY_COLOR # what for the key to be released while key.is_pressed(): pass key.clear()
keypad = adafruit_matrixkeypad.Matrix_Keypad(rows, cols, keys) KEYBOARDS = [{ 'color': (10, 0, 0), 'keys': [ lambda: cc.send(ConsumerControlCode.SCAN_NEXT_TRACK), lambda: cc.send(ConsumerControlCode.VOLUME_INCREMENT), lambda: cc.send(ConsumerControlCode.PLAY_PAUSE), lambda: cc.send(ConsumerControlCode.SCAN_PREVIOUS_TRACK), lambda: cc.send(ConsumerControlCode.VOLUME_DECREMENT), lambda: cc.send(ConsumerControlCode.MUTE), ] }, { 'color': (0, 10, 0), 'keys': [ lambda: kbd.send(Keycode.ONE), lambda: kbd.send(Keycode.TWO), lambda: kbd.send(Keycode.THREE), lambda: kbd.send(Keycode.FOUR), lambda: kbd.send(Keycode.FIVE), lambda: kbd.send(Keycode.SIX), lambda: kbd.send(Keycode.SEVEN), lambda: kbd.send(Keycode.EIGHT), lambda: kbd.send(Keycode.NINE) ] }] selected_keyboard = 0 while True: keys = keypad.pressed_keys dot[0] = KEYBOARDS[selected_keyboard]['color'] if keys: if 0 in keys and 5 in keys: # if the first and sixth keys are pressed, change keyboard next_keyboard = selected_keyboard + 1 if next_keyboard == len(KEYBOARDS):
# affects the keycodes assigned to the encoder's inputs if not SWITCH.value: chill = False doom = True if SWITCH.value: chill = True doom = False # rotary encoder position tracking position = encoder.position # if the encoder is turned to the right if position > last_position: # if in streaming mode if chill: # send UP arrow for volume kbd.send(Keycode.UP_ARROW) # if in doom mode if doom: # send period for right strafe kbd.send(Keycode.PERIOD) # reset encoder position last_position = position # if the encoder is turned to the left if position < last_position: # if in streaming mode if chill: # send DOWN arrow for volume kbd.send(Keycode.DOWN_ARROW) # if in doom mode if doom: # send comma for left strafe
class ArrowKeyboard: def __init__(self, rows_, cols_, keymaps_): self.LED_ON = False self.LED_OFF = True self.kbd = Keyboard(usb_hid.devices) self.rows = self.set_rows(rows_) self.cols = self.set_cols(cols_) self.keymaps = keymaps_ self.led = DigitalInOut(board.D13) self.led.direction = Direction.OUTPUT def set_rows(self, row_pins): rows = [] for row_pin in row_pins: row = DigitalInOut(row_pin) row.direction = Direction.OUTPUT row.value = False rows.append(row) return rows def set_cols(self, col_pins): cols = [] for col_pin in col_pins: col = DigitalInOut(col_pin) col.direction = Direction.INPUT col.pull = Pull.UP cols.append(col) return cols def scan_matrix(self): aiKeyOn = [] for idxRow, row in enumerate(self.rows): row.value = True for idxCol, col in enumerate(self.cols): if not col.value: ary = [idxRow, idxCol] aiKeyOn.append(ary) row.value = False return aiKeyOn def autoUDRL(self): self.led.value = self.LED_ON bLoop = True while bLoop: self.kbd.send(Keycode.UP_ARROW) time.sleep(1) self.led.value = self.LED_OFF self.kbd.send(Keycode.DOWN_ARROW) time.sleep(1) self.led.value = self.LED_ON self.kbd.send(Keycode.DOWN_ARROW) time.sleep(1) self.led.value = self.LED_OFF self.kbd.send(Keycode.UP_ARROW) time.sleep(1) self.led.value = self.LED_ON self.kbd.send(Keycode.LEFT_ARROW) time.sleep(1) self.led.value = self.LED_OFF self.kbd.send(Keycode.RIGHT_ARROW) time.sleep(1) self.led.value = self.LED_ON self.kbd.send(Keycode.RIGHT_ARROW) time.sleep(1) self.led.value = self.LED_OFF self.kbd.send(Keycode.LEFT_ARROW) time.sleep(1) self.led.value = self.LED_ON aiKeyOn = self.scan_matrix() if len(aiKeyOn) == 0: time.sleep(12) else: bLoop = False self.led.value = self.LED_OFF return def main_loop(self): self.led.value = self.LED_OFF while True: aiKeyOn = self.scan_matrix() if len(aiKeyOn) == 3: self.autoUDRL() elif len(aiKeyOn) > 0: for ary in aiKeyOn: self.kbd.send(self.keymaps[ary[0]][ary[1]]) self.led.value = self.LED_ON time.sleep(0.1) else: self.led.value = self.LED_OFF
## mouse scroll if not encClick.value: if position < last_position: m.move(wheel=1) bD(1) last_position = position if position > last_position: m.move(wheel=-1) bD(1) last_position = position ## switch desktops when pressed if osChoice == 0 and encClick.value: if position < last_position: kbd.send(Keycode.GUI, Keycode.CONTROL, Keycode.LEFT_ARROW) bD(1) last_position = position if position > last_position: kbd.send(Keycode.GUI, Keycode.CONTROL, Keycode.RIGHT_ARROW) bD(1) last_position = position if osChoice == 1 and encClick.value: if position < last_position: kbd.send(Keycode.CONTROL, Keycode.LEFT_ARROW) bD(1) last_position = position if position > last_position: kbd.send(Keycode.CONTROL, Keycode.RIGHT_ARROW) bD(1) last_position = position
while True: # Test buttons on Maker Pi Pico if not btn1.value: # Debounce time.sleep(0.3) while not btn1.value: pass # Type 'abc' followed by newline. layout.write('abc\n') # Type numbers followed by newline. layout.write('789\n') # Type symbols followed by tab. layout.write('!?_*\t') # Type lowercase 'x'. kbd.send(Keycode.X) # Type capital 'Y'. kbd.send(Keycode.SHIFT, Keycode.Y) if not btn2.value: # Debounce time.sleep(0.3) while not btn2.value: pass # Toggle mouse wheel up/down by one unit if direction: mouse.move(wheel=1) direction = False else: mouse.move(wheel=-1) direction = True
import usb_hid from adafruit_circuitplayground.express import cpx from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode kbd = Keyboard(usb_hid.devices) while True: if cpx.button_a: # Alt-A is mute/unmute kbd.send(Keycode.ALT, Keycode.A) while cpx.button_a: # Wait for button to be released pass if cpx.button_b: # Alt-V is video on/off kbd.send(Keycode.ALT, Keycode.V) while cpx.button_b: # Wait for button to be released pass
class HidScript: def __init__(self, debug=False): self.debug = debug #self.mouse = Mouse(usb_hid.devices) self.keyboard = Keyboard(usb_hid.devices) self.keyboard_layout = KeyboardLayoutUS( self.keyboard) # Change for non-US self.default_delay = 0 self.string_delay = 0 self.last_cmd = "" self.last_arg = "" # Keycode only has US mappings, not sure if AdaFruit has others # # TODO - consider a data structure which consumes less RAM for # constrained platforms. # # Keep a separate structure for multi-key combos to lower memory # usage. # self.tuple_keymap = { "CTRL-ALT": (Keycode.CONTROL, Keycode.ALT), "CTRL-SHIFT": (Keycode.CONTROL, Keycode.SHIFT), "ALT-SHIFT": (Keycode.ALT, Keycode.SHIFT) } self.keymap = { "ALT": Keycode.ALT, "APP": Keycode.APPLICATION, "BREAK": Keycode.PAUSE, "CAPSLOCK": Keycode.CAPS_LOCK, "CONTROL": Keycode.CONTROL, "CTRL": Keycode.CONTROL, "DELETE": Keycode.DELETE, "DOWNARROW": Keycode.DOWN_ARROW, "DOWN": Keycode.DOWN_ARROW, "END": Keycode.END, "ENTER": Keycode.ENTER, "ESC": Keycode.ESCAPE, "ESCAPE": Keycode.ESCAPE, "GUI": Keycode.GUI, "HOME": Keycode.HOME, "INSERT": Keycode.INSERT, "LEFTARROW": Keycode.LEFT_ARROW, "LEFT": Keycode.LEFT_ARROW, "MENU": Keycode.APPLICATION, "NUMLOCK": Keycode.KEYPAD_NUMLOCK, "PAGEUP": Keycode.PAGE_UP, "PAGEDOWN": Keycode.PAGE_DOWN, "PAUSE": Keycode.PAUSE, "PRINTSCREEN": Keycode.PRINT_SCREEN, "RIGHTARROW": Keycode.RIGHT_ARROW, "RIGHT": Keycode.RIGHT_ARROW, "SCROLLLOCK": Keycode.SCROLL_LOCK, "SHIFT": Keycode.SHIFT, "SPACE": Keycode.SPACE, "TAB": Keycode.TAB, "UPARROW": Keycode.UP_ARROW, "UP": Keycode.UP_ARROW, "WINDOWS": Keycode.WINDOWS, "a": Keycode.A, "A": Keycode.A, "b": Keycode.B, "B": Keycode.B, "c": Keycode.C, "C": Keycode.C, "d": Keycode.D, "D": Keycode.D, "e": Keycode.E, "E": Keycode.E, "f": Keycode.F, "F": Keycode.F, "g": Keycode.G, "G": Keycode.G, "h": Keycode.H, "H": Keycode.H, "i": Keycode.I, "I": Keycode.I, "j": Keycode.J, "J": Keycode.J, "k": Keycode.K, "K": Keycode.K, "l": Keycode.L, "L": Keycode.L, "m": Keycode.M, "M": Keycode.M, "n": Keycode.N, "N": Keycode.N, "o": Keycode.O, "O": Keycode.O, "p": Keycode.P, "P": Keycode.P, "q": Keycode.Q, "Q": Keycode.Q, "r": Keycode.R, "R": Keycode.R, "s": Keycode.S, "S": Keycode.S, "t": Keycode.T, "T": Keycode.T, "u": Keycode.U, "U": Keycode.U, "v": Keycode.V, "V": Keycode.V, "w": Keycode.W, "W": Keycode.W, "x": Keycode.X, "X": Keycode.X, "y": Keycode.Y, "Y": Keycode.Y, "z": Keycode.Z, "Z": Keycode.Z, # # The DuckyScript encoder didn't appear to have the following codes and # probably used the STRING command to deal with some of them. Adding them shouldn't # hurt but does consume RAM. Some are Mac specific. # "F1": Keycode.F1, "F2": Keycode.F2, "F3": Keycode.F3, "F4": Keycode.F4, "F5": Keycode.F5, "F6": Keycode.F6, "F7": Keycode.F7, "F8": Keycode.F8, "F9": Keycode.F9, "F10": Keycode.F10, "F11": Keycode.F11, "F12": Keycode.F12, "F13": Keycode.F13, "F14": Keycode.F14, "F15": Keycode.F15, "F16": Keycode.F16, "F17": Keycode.F17, "F18": Keycode.F18, "F19": Keycode.F19, "BACKSLASH": Keycode.BACKSLASH, "COMMA": Keycode.COMMA, "COMMAND": Keycode.COMMAND, "FORWARD_SLASH": Keycode.FORWARD_SLASH, "GRAVE_ACCENT": Keycode.GRAVE_ACCENT, "LEFT_BRACKET": Keycode.LEFT_BRACKET, "OPTION": Keycode.ALT, "PERIOD": Keycode.PERIOD, "POUND": Keycode.POUND, "QUOTE": Keycode.QUOTE } # Dropped the following to save memory (for now): # "0": Keycode.ZERO, "1": Keycode.ONE, # "2": Keycode.TWO, "3": Keycode.THREE, # "4": Keycode.FOUR, "5": Keycode.FIVE, # "6": Keycode.SIX, "7": Keycode.SEVEN, # "8": Keycode.EIGHT, "9": Keycode.NINE, # "RIGHT_ALT": Keycode.RIGHT_ALT, # "RIGHT_BRACKET": Keycode.RIGHT_BRACKET, # "RIGHT_CONTROL": Keycode.RIGHT_CONTROL, # "RIGHT_GUI": Keycode.RIGHT_GUI, # "RIGHT_SHIFT": Keycode.RIGHT_SHIFT, # "SEMICOLON": Keycode.SEMICOLON, # Execute a single command in a HidScript file # REPEAT handled in process # def _exec_cmd(self, cmd, arg): if cmd == "REM": pass elif cmd == "STRING": if self.string_delay: for c in arg: self.keyboard_layout.write(c) time.sleep(self.string_delay) else: self.keyboard_layout.write(arg) elif cmd in self.keymap: if arg and arg in self.keymap: self.keyboard.send(self.keymap[cmd], self.keymap[arg]) else: self.keyboard.send(self.keymap[cmd]) elif cmd in self.tuple_keymap: for keycode in self.tuple_keymap[cmd]: self.keyboard.press(keycode) if arg and arg in self.keymap: self.keyboard.send(self.keymap[arg]) else: self.keyboard.release_all() if cmd == "DELAY": time.sleep(int(arg) / 100.0) elif (cmd == "DEFAULTDELAY") or (cmd == "DEFAULT_DELAY"): self.default_delay = int(arg) / 100.0 elif (cmd == "STRINGDELAY") or (cmd == "STRING_DELAY"): self.string_delay = int(arg) / 1000.0 else: time.sleep(self.default_delay) # Process a HidScript file # def process(self, scriptname): try: with open(scriptname) as f: for line in f: # Eliminate leading or trailing whitespace # TODO - does this break any STRING commands? # line = line.strip() values = line.split(" ", 1) if self.debug: print("Script line: ", line) cmd = values[0] if len(values) == 2: arg = values[1] else: arg = "" # Handle REPEAT command at this level, but all other commands # are handled by _exec_cmd() # if cmd == "REPEAT": arg = int(arg) for i in range(arg): self._exec_cmd(self.last_cmd, self.last_arg) else: self.last_cmd = cmd self.last_arg = arg self._exec_cmd(cmd, arg) except OSError: if self.debug: print("Could not read file: ", scriptname)
D6.direction = Direction.INPUT D6.pull = Pull.UP D7 = DigitalInOut(board.D7) D7.direction = Direction.INPUT D7.pull = Pull.UP # loop forever while True: if not D0.value: # press the SPACEBAR led.value = False # led on kbd.send(first_button) # press the SPACEBAR time.sleep(debounce_time) # debounce delay led.value = True # led off if not D1.value: # press the SPACEBAR led.value = False # led on kbd.send(second_button) # press the SPACEBAR time.sleep(debounce_time) # debounce delay led.value = True # led off if not D3.value: # press the SPACEBAR led.value = False # led on
for i in range(x, y): if not (1 << i) & b: pressed[i] = 1 else: pressed[i] = 0 return pressed held = [0] * 16 while True: pressed = read_button_states(0, 16) if pressed[0]: pixels[0] = colourwheel(0 * 16) # Map pixel index to 0-255 range if not held[0]: layout.write("volu") kbd.send(Keycode.ENTER) #held[0] = 1 elif pressed[1]: pixels[1] = colourwheel(1 * 16) # Map pixel index to 0-255 range if not held[1]: layout.write("mpc next") kbd.send(Keycode.ENTER) held[1] = 1 elif pressed[2]: pixels[2] = colourwheel(2 * 16) # Map pixel index to 0-255 range if not held[2]: layout.write("rad4")
key_1_state = False neokey.pixels[1] = 0x0 if not neokey[2] and key_2_state: key_2_state = False neokey.pixels[2] = 0x0 if not neokey[3] and key_3_state: key_3_state = False neokey.pixels[3] = 0x0 # if 1st neokey is pressed... if neokey[0] and not key_0_state: print("Button A") # turn on NeoPixel neokey.pixels[0] = 0xFF0000 # open macOS emoji menu keyboard.send(Keycode.CONTROL, Keycode.COMMAND, Keycode.SPACE) # delay for opening menu time.sleep(.2) # send key presses for emoji_0 for i in emoji_0: keyboard.send(i) time.sleep(0.05) # update key state key_0_state = True # if 2nd neokey is pressed... if neokey[1] and not key_1_state: print("Button B") # turn on NeoPixel neokey.pixels[1] = 0xFFFF00 # open macOS emoji menu