Skip to content

Instantly share code, notes, and snippets.

@thnikk
Created April 17, 2019 18:32
Show Gist options
  • Save thnikk/60a8996b9c2ef09af0d639fc48eb7b59 to your computer and use it in GitHub Desktop.
Save thnikk/60a8996b9c2ef09af0d639fc48eb7b59 to your computer and use it in GitHub Desktop.
Final circuitpython keypad with minimal dependencies
# Import all necessary libraries
import board
import touchio
from digitalio import DigitalInOut, Direction, Pull
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
# Change these values for what you want each key to print when pressed
keys_pressed = ["Button 1 ", "Button 2 ", "Button 3 "]
button1 = DigitalInOut(board.D0) # Pin assignment for button 1
button1.direction = Direction.INPUT
button1.pull = Pull.UP
button2 = DigitalInOut(board.D2) # Pin assignment for button 2
button2.direction = Direction.INPUT
button2.pull = Pull.UP
screw = board.D4 # Pin assignmnet for side screw
touch = touchio.TouchIn(screw)
# Set up keyboard
kbd = Keyboard()
layout = KeyboardLayoutUS(kbd) # Set US layout
# Keeps keys from being constantly pressed/released
button1Check=1
button2Check=1
button3Check=0 # Inverted to avoid press at startup
######################### MAIN LOOP ##############################
while True:
if not button1.value: # If the button isn't pressed...
if button1Check: # Check value so it only does this once per press
layout.write(keys_pressed[0]) # Print string
button1Check = 0 # Update check value
if button1.value: # If button is pressed...
button1Check = 1 # Update check value
if not button2.value:
if button2Check:
layout.write(keys_pressed[1])
button2Check = 0
if button2.value:
button2Check = 1
if not touch.value:
if button3Check:
layout.write(keys_pressed[2])
button3Check = 0
if touch.value:
button3Check = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment