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