|
| 1 | +"""NeoKey Trinkey Capacitive Touch and HID Keyboard example""" |
| 2 | +import time |
| 3 | +import board |
| 4 | +import neopixel |
| 5 | +import usb_hid |
| 6 | +from adafruit_hid.keyboard import Keyboard |
| 7 | +from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS |
| 8 | +from adafruit_hid.keycode import Keycode # pylint: disable=unused-import |
| 9 | +from digitalio import DigitalInOut, Pull |
| 10 | +import touchio |
| 11 | + |
| 12 | +print("NeoKey Trinkey HID") |
| 13 | + |
| 14 | +# create the pixel and turn it off |
| 15 | +pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1) |
| 16 | +pixel.fill(0x0) |
| 17 | + |
| 18 | +time.sleep(1) # Sleep for a bit to avoid a race condition on some systems |
| 19 | +keyboard = Keyboard(usb_hid.devices) |
| 20 | +keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) |
| 21 | + |
| 22 | +# create the switch, add a pullup, start it with not being pressed |
| 23 | +button = DigitalInOut(board.SWITCH) |
| 24 | +button.switch_to_input(pull=Pull.DOWN) |
| 25 | +button_state = False |
| 26 | + |
| 27 | +# create the captouch element and start it with not touched |
| 28 | +touch = touchio.TouchIn(board.TOUCH) |
| 29 | +touch_state = False |
| 30 | + |
| 31 | +# print a string on keypress |
| 32 | +key_output = "Hello World!\n" |
| 33 | + |
| 34 | +# one character on keypress |
| 35 | +# key_output = Keycode.A |
| 36 | + |
| 37 | +# multiple simultaneous keypresses |
| 38 | +# key_output = (Keycode.SHIFT, Keycode.A) # capital A |
| 39 | +# key_output = (Keycode.CONTROL, Keycode.ALT, Keycode.DELETE) # three finger salute! |
| 40 | + |
| 41 | +# complex commands! we make a list of dictionary entries for each command |
| 42 | +# each line has 'keys' which is either a single key, a list of keys, or a string |
| 43 | +# then the 'delay' is in seconds, since we often need to give the computer a minute |
| 44 | +# before doing something! |
| 45 | + |
| 46 | +# this will open up a notepad in windows, and ducky the user |
| 47 | +""" |
| 48 | +key_output = ( |
| 49 | + {'keys': Keycode.GUI, 'delay': 0.1}, |
| 50 | + {'keys': "notepad\n", 'delay': 1}, # give it a moment to launch! |
| 51 | + {'keys': "YOU HAVE BEEN DUCKIED!", 'delay': 0.1}, |
| 52 | + {'keys': (Keycode.ALT, Keycode.O), 'delay': 0.1}, # open format menu |
| 53 | + {'keys': Keycode.F, 'delay': 0.1}, # open font submenu |
| 54 | + {'keys': "\t\t100\n", 'delay': 0.1}, # tab over to font size, enter 100 |
| 55 | +) |
| 56 | +""" |
| 57 | + |
| 58 | + |
| 59 | +# our helper function will press the keys themselves |
| 60 | +def make_keystrokes(keys, delay): |
| 61 | + if isinstance(keys, str): # If it's a string... |
| 62 | + keyboard_layout.write(keys) # ...Print the string |
| 63 | + elif isinstance(keys, int): # If its a single key |
| 64 | + keyboard.press(keys) # "Press"... |
| 65 | + keyboard.release_all() # ..."Release"! |
| 66 | + elif isinstance(keys, (list, tuple)): # If its multiple keys |
| 67 | + keyboard.press(*keys) # "Press"... |
| 68 | + keyboard.release_all() # ..."Release"! |
| 69 | + time.sleep(delay) |
| 70 | + |
| 71 | + |
| 72 | +while True: |
| 73 | + if button.value and not button_state: |
| 74 | + pixel.fill((255, 0, 255)) |
| 75 | + print("Button pressed.") |
| 76 | + button_state = True |
| 77 | + |
| 78 | + if not button.value and button_state: |
| 79 | + pixel.fill(0x0) |
| 80 | + print("Button released.") |
| 81 | + if isinstance(key_output, (list, tuple)) and isinstance(key_output[0], dict): |
| 82 | + for k in key_output: |
| 83 | + make_keystrokes(k['keys'], k['delay']) |
| 84 | + else: |
| 85 | + make_keystrokes(key_output, delay=0) |
| 86 | + button_state = False |
| 87 | + |
| 88 | + if touch.value and not touch_state: |
| 89 | + print("Touched!") |
| 90 | + pixel.fill((0, 255, 0)) |
| 91 | + touch_state = True |
| 92 | + if not touch.value and touch_state: |
| 93 | + print("Untouched!") |
| 94 | + pixel.fill(0x0) |
| 95 | + touch_state = False |
0 commit comments