def spamKey(code): knobkeys = [Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.SPACE, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET] spamRate = [0.01, 0.05, 0.125, 0.25, 0.5, 0.5, 0.5, 0.25, 0.125, 0.05, 0.01] kbd = Keyboard() kbd.press(knobkeys[code]) # which keycode is entered kbd.release_all() time.sleep(spamRate[code]) # how fast the key is spammed
if len(pulse1) != len(pulse2): return False for i in range(len(pulse1)): threshold = int(pulse1[i] * fuzzyness) if abs(pulse1[i] - pulse2[i]) > threshold: return False return True # Create pulse input and IR decoder. pulsein.clear() pulsein.resume() # Loop waiting to receive pulses. while True: led[0] = (0, 0, 0) # LED off # Wait for a pulse to be detected. pulses = decoder.read_pulses(pulsein) led[0] = (0, 0, 100) # flash blue print("\tHeard", len(pulses), "Pulses:", pulses) # Got a pulse set, now compare. if fuzzy_pulse_compare(key1_pulses, pulses): print("****** KEY 1 DETECTED! ******") keyboard.press(Keycode.SPACE) keyboard.release_all() if fuzzy_pulse_compare(key2_pulses, pulses): print("****** KEY 2 DETECTED! ******") keyboard_layout.write("hello!")
if encoder_direction == 1: consumer_control.send( ConsumerControlCode.VOLUME_DECREMENT) #Turn Down Volume # kbd.press(Keycode.LEFT_ARROW) # kbd.release_all() # Check if rotary encoder went down if encoder_direction == -1: consumer_control.send( ConsumerControlCode.VOLUME_INCREMENT) #Turn Up Volume # kbd.press(Keycode.RIGHT_ARROW) # kbd.release_all() # Button was 'just pressed' if (not button.value) and last_button: print("Button pressed!") kbd.press(Keycode.SPACE) #Keycode for spacebar kbd.release_all() ring[dot_location] = PRESSED_DOT_COLOR # show it was pressed on ring timestamp = time.monotonic() # something happened! elif button.value and (not last_button): print("Button Released!") # kbd.press(Keycode.SHIFT, Keycode.SIX) # kbd.release_all() ring[dot_location] = DOT_COLOR # show it was released on ring timestamp = time.monotonic() # something happened! last_button = button.value if encoder_direction != 0: timestamp = time.monotonic() # something happened! # spin neopixel LED around! previous_location = dot_location dot_location += encoder_direction # move dot in the direction
# For QT Py M0: # led = digitalio.DigitalInOut(board.SCK) led.direction = digitalio.Direction.OUTPUT print("Waiting for key pin...") while True: # Check each pin for key_pin in key_pin_array: if not key_pin.value: # Is it grounded? i = key_pin_array.index(key_pin) print("Pin #%d is grounded." % i) # Turn on the red LED led.value = True while not key_pin.value: pass # Wait for it to be ungrounded! # "Type" the Keycode or string key = keys_pressed[i] # Get the corresponding Keycode or string if isinstance(key, str): # If it's a string... keyboard_layout.write(key) # ...Print the string else: # If it's not a string... keyboard.press(control_key, key) # "Press"... keyboard.release_all() # ..."Release"! # Turn off the red LED led.value = False time.sleep(0.01)
print("Waiting for button presses") while True: # check each button # when pressed, the LED will light up, # when released, the keycode or string will be sent # this prevents rapid-fire repeats! for button in buttons: if button.value: # pressed? i = buttons.index(button) print("Button #%d Pressed" % i) # turn on the LED led.value = True while button.value: pass # wait for it to be released! # type the keycode or string k = buttonkeys[i] # get the corresp. keycode/str if type(k) is str: layout.write(k) else: kbd.press(controlkey, k) # press... kbd.release_all() # release! # turn off the LED led.value = False time.sleep(0.01)
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)
def fuzzy_pulse_compare(pulse1, pulse2, fuzzyness=0.2): if len(pulse1) != len(pulse2): return False for i in range(len(pulse1)): threshold = int(pulse1[i] * fuzzyness) if abs(pulse1[i] - pulse2[i]) > threshold: return False return True # Create pulse input and IR decoder. pulsein.clear() pulsein.resume() # Loop waiting to receive pulses. while True: led[0] = (0, 0, 0) # LED off # Wait for a pulse to be detected. pulses = decoder.read_pulses(pulsein) led[0] = (0, 0, 100) # flash blue print("\tHeard", len(pulses), "Pulses:", pulses) # Got a pulse set, now compare. if fuzzy_pulse_compare(key1_pulses, pulses): print("****** KEY 1 DETECTED! ******") keyboard.press(Keycode.SPACE) keyboard.release_all() if fuzzy_pulse_compare(key2_pulses, pulses): print("****** KEY 2 DETECTED! ******") keyboard_layout.write("hello!")
i = key_pin_array.index(key_pin) print("Pin #%d is grounded." % i) # Turn on the red LED led.value = True while not key_pin.value: pass # Wait for it to be ungrounded! # "Type" the Keycode or string key = keys_pressed[i] # Get the corresponding Keycode or string if isinstance(key, str): # If it's a string... keyboard_layout.write(key) # ...Print the string elif isinstance(key, list): # if it's a sequence to be typed... for key_press in key: if isinstance( key_press, list ): # if it's a combination within the sequence, like a key with a modifier (ctrl, shift, etc) for a_press in key_press: keyboard.press(a_press) else: # no modifier included so just send the key press keyboard.press(key_press) keyboard.release_all( ) # release the key or combination of presses for each step in the sequence else: # If it's not a string or a list, treat it as a single key press... keyboard.press(key) # "Press"... keyboard.release_all() # ..."Release"! # Turn off the red LED led.value = False time.sleep(0.02) # debounce the keys
# --------------------------------------- # ------- 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
class CPKFKeyboard: def __init__(self, keymap, scan_method): self.scan_method = scan_method self.keymap = keymap self.currentLayer = 0 self.layerHistory = [self.currentLayer] self.press = [] for i in range(len(keymap[0])): self.press.append(None) usb_status = len(usb_hid.devices) try: if (usb_status): self.adakbd = Keyboard(usb_hid.devices) self.mouse = Mouse(usb_hid.devices) else: self.adakbd = None self.mouse = None except OSError: self.adakbd = None self.mouse = None def updateHIDdevice(self, hid_device=None): if self.adakbd: try: self.release_all() except OSError: pass if hid_device: self.hid_device = hid_device elif supervisor.runtime.serial_connected: self.hid_device = usb_hid.devices print(self.hid_device) if self.hid_device: self.adakbd = Keyboard(self.hid_device) self.mouse = Mouse(self.hid_device) def start(self): print(TAPPING_TERM_FORCE_HOLD if "TTFH is True" else "TTFH is False") self.scan_method(self) def physicalKeypress(self, i): if not self.press[i]: if TAPPING_TERM_FORCE_HOLD: for k in self.press: if isinstance(k, KeyObject): k.force_tick(self) k = self.keymap[self.layerHistory[-1]][i] if k: print("Button #%d Pressed" % i) self.press[i] = k self.keyPress(k) def keyPress(self, k): if (self.adakbd is None) and supervisor.runtime.serial_connected: self.adakbd = Keyboard(usb_hid.devices) self.mouse = Mouse(usb_hid.devices) if type(k) is int: modifier_bit = k >> 8 if modifier_bit: self.adakbd.report_modifier[0] |= modifier_bit self.adakbd.press(k & 0xFF) elif isinstance(k, KeyObject): k.press(self, time.monotonic()) def physicalKeyrelease(self, i): if self.press[i]: print("Button #%d released" % i) k = self.press[i] if k: self.keyRelease(k) self.press[i] = None def keyRelease(self, k): if type(k) is int: modifier_bit = k >> 8 if modifier_bit: self.adakbd.report_modifier[0] &= ~modifier_bit self.adakbd.release(k & 0xFF) elif isinstance(k, KeyObject): k.release(self, time.monotonic()) def send(self, kc): # kbd.send()だとmodifierの情報が消える self.adakbd.press(kc) self.adakbd.release(kc) def release_all(self): self.adakbd.release_all() def tick(self, t): for k in self.press: if isinstance(k, KeyObject): k.tick(self, t) def changeLayer(self, to): self.layerHistory.append(to) def backLayer(self, current): self.layerHistory.remove(current)