/ Pico_Four_Keypad / code.py
code.py
1 # SPDX-FileCopyrightText: 2021 john park for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 # Pico Four Key USB HID Keypad 4 5 import board 6 from digitalio import DigitalInOut, Pull 7 from adafruit_debouncer import Debouncer 8 import usb_hid 9 from adafruit_hid.keyboard import Keyboard 10 from adafruit_hid.keycode import Keycode 11 12 kpd = Keyboard(usb_hid.devices) 13 14 # Choose the correct modifier key for Windows or Mac. 15 # Comment one line and uncomment the other. 16 # MODIFIER = Keycode.CONTROL # For Windows 17 MODIFIER = Keycode.COMMAND # For Mac 18 19 # define buttons 20 NUM_KEYS = 4 21 PINS = ( 22 board.GP0, 23 board.GP1, 24 board.GP2, 25 board.GP3, 26 ) 27 28 KEYMAP = ( 29 ("Select all", [MODIFIER, Keycode.A]), 30 ("Cut", [MODIFIER, Keycode.X]), 31 ("Copy", [MODIFIER, Keycode.C]), 32 ("Paste", [MODIFIER, Keycode.V]), 33 ) 34 35 keys = [] 36 for pin in PINS: 37 dio = DigitalInOut(pin) 38 dio.pull = Pull.UP 39 keys.append(Debouncer(dio)) 40 41 print("\nWelcome to keypad") 42 print("keymap:") 43 for k in range(NUM_KEYS): 44 print("\t", (KEYMAP[k][0])) 45 46 47 while True: 48 for i in range(NUM_KEYS): 49 keys[i].update() 50 if keys[i].fell: 51 print(KEYMAP[i][0]) 52 kpd.send(*KEYMAP[i][1])