keypad
– Support for scanning keys and key matrices¶
The keypad
module provides native support to scan sets of keys or buttons,
connected independently to individual pins,
connected to a shift register,
or connected in a row-and-column matrix.
Available on these boards
- class keypad.Event(key_number: int = 0, pressed: bool = True, timestamp: int | None = None)¶
A key transition event.
Create a key transition event, which reports a key-pressed or key-released transition.
- Paramètres:
key_number (int) – the key number
pressed (bool) –
True
if the key was pressed;False
if it was released.timestamp (int) – The time in milliseconds that the keypress occurred in the
supervisor.ticks_ms
time system. If specified as None, the current value ofsupervisor.ticks_ms
is used.
- key_number :int¶
The key number.
- pressed :bool¶
True
if the event represents a key down (pressed) transition. The opposite ofreleased
.
- released :bool¶
True
if the event represents a key up (released) transition. The opposite ofpressed
.
- timestamp :int¶
The timestamp
- class keypad.EventQueue¶
A queue of
Event
objects, filled by akeypad
scanner such asKeys
orKeyMatrix
.You cannot create an instance of
EventQueue
directly. Each scanner creates an instance when it is created.- overflowed :bool¶
True
if an event could not be added to the event queue because it was full. (read-only) Set toFalse
byclear()
.
- get() Event | None ¶
Return the next key transition event. Return
None
if no events are pending.Note that the queue size is limited; see
max_events
in the constructor of a scanner such asKeys
orKeyMatrix
. If a new event arrives when the queue is full, the event is discarded, andoverflowed
is set toTrue
.
- get_into(event: Event) bool ¶
Store the next key transition event in the supplied event, if available, and return
True
. If there are no queued events, do not touchevent
and returnFalse
.The advantage of this method over
get()
is that it does not allocate storage. Instead you can reuse an existingEvent
object.Note that the queue size is limited; see
max_events
in the constructor of a scanner such asKeys
orKeyMatrix
.- Renvoie:
True
if an event was available and stored,False
if not.- Type renvoyé:
- clear() None ¶
Clear any queued key transition events. Also sets
overflowed
toFalse
.
- class keypad.KeyMatrix(row_pins: Sequence[microcontroller.Pin], column_pins: Sequence[microcontroller.Pin], columns_to_anodes: bool = True, interval: float = 0.02, max_events: int = 64)¶
Manage a 2D matrix of keys with row and column pins.
Create a
Keys
object that will scan the key matrix attached to the given row and column pins. There should not be any external pull-ups or pull-downs on the matrix:KeyMatrix
enables internal pull-ups or pull-downs on the pins as necessary.The keys are numbered sequentially from zero. A key number can be computed by
row * len(column_pins) + column
.An
EventQueue
is created when this object is created and is available in theevents
attribute.- Paramètres:
row_pins (Sequence[microcontroller.Pin]) – The pins attached to the rows.
column_pins (Sequence[microcontroller.Pin]) – The pins attached to the colums.
columns_to_anodes (bool) – Default
True
. If the matrix uses diodes, the diode anodes are typically connected to the column pins, and the cathodes should be connected to the row pins. If your diodes are reversed, setcolumns_to_anodes
toFalse
.interval (float) – Scan keys no more often than
interval
to allow for debouncing.interval
is in float seconds. The default is 0.020 (20 msecs).max_events (int) – maximum size of
events
EventQueue
: maximum number of key transition events that are saved. Must be >= 1. If a new event arrives when the queue is full, the oldest event is discarded.
- key_count :int¶
The number of keys that are being scanned. (read-only)
- events :EventQueue¶
The
EventQueue
associated with thisKeys
object. (read-only)
- __exit__() None ¶
Automatically deinitializes when exiting a context. See Lifetime and ContextManagers for more info.
- reset() None ¶
Reset the internal state of the scanner to assume that all keys are now released. Any key that is already pressed at the time of this call will therefore immediately cause a new key-pressed event to occur.
- class keypad.Keys(pins: Sequence[microcontroller.Pin], *, value_when_pressed: bool, pull: bool = True, interval: float = 0.02, max_events: int = 64)¶
Manage a set of independent keys.
Create a
Keys
object that will scan keys attached to the given sequence of pins. Each key is independent and attached to its own pin.An
EventQueue
is created when this object is created and is available in theevents
attribute.- Paramètres:
pins (Sequence[microcontroller.Pin]) – The pins attached to the keys. The key numbers correspond to indices into this sequence.
value_when_pressed (bool) –
True
if the pin reads high when the key is pressed.False
if the pin reads low (is grounded) when the key is pressed. All the pins must be connected in the same way.pull (bool) –
True
if an internal pull-up or pull-down should be enabled on each pin. A pull-up will be used ifvalue_when_pressed
isFalse
; a pull-down will be used if it isTrue
. If an external pull is already provided for all the pins, you can setpull
toFalse
. However, enabling an internal pull when an external one is already present is not a problem; it simply uses slightly more current.interval (float) – Scan keys no more often than
interval
to allow for debouncing.interval
is in float seconds. The default is 0.020 (20 msecs).max_events (int) – maximum size of
events
EventQueue
: maximum number of key transition events that are saved. Must be >= 1. If a new event arrives when the queue is full, the oldest event is discarded.
- key_count :int¶
The number of keys that are being scanned. (read-only)
- events :EventQueue¶
The
EventQueue
associated with thisKeys
object. (read-only)
- __exit__() None ¶
Automatically deinitializes when exiting a context. See Lifetime and ContextManagers for more info.
- class keypad.ShiftRegisterKeys(*, clock: microcontroller.Pin, data: microcontroller.Pin, latch: microcontroller.Pin, value_to_latch: bool = True, key_count: int, value_when_pressed: bool, interval: float = 0.02, max_events: int = 64)¶
Manage a set of keys attached to an incoming shift register.
Create a
Keys
object that will scan keys attached to a parallel-in serial-out shift register like the 74HC165 or CD4021. Note that you may chain shift registers to load in as many values as you need.Key number 0 is the first (or more properly, the zero-th) bit read. In the 74HC165, this bit is labeled
Q7
. Key number 1 will be the value ofQ6
, etc.An
EventQueue
is created when this object is created and is available in theevents
attribute.- Paramètres:
clock (microcontroller.Pin) – The shift register clock pin. The shift register should clock on a low-to-high transition.
data (microcontroller.Pin) – the incoming shift register data pin
latch (microcontroller.Pin) – Pin used to latch parallel data going into the shift register.
value_to_latch (bool) – Pin state to latch data being read.
True
if the data is latched whenlatch
goes highFalse
if the data is latched whenlatch
goes low. The default isTrue
, which is how the 74HC165 operates. The CD4021 latch is the opposite. Once the data is latched, it will be shifted out by toggling the clock pin.key_count (int) – number of data lines to clock in
value_when_pressed (bool) –
True
if the pin reads high when the key is pressed.False
if the pin reads low (is grounded) when the key is pressed.interval (float) – Scan keys no more often than
interval
to allow for debouncing.interval
is in float seconds. The default is 0.020 (20 msecs).max_events (int) – maximum size of
events
EventQueue
: maximum number of key transition events that are saved. Must be >= 1. If a new event arrives when the queue is full, the oldest event is discarded.
- key_count :int¶
The number of keys that are being scanned. (read-only)
- events :EventQueue¶
The
EventQueue
associated with thisKeys
object. (read-only)
- __exit__() None ¶
Automatically deinitializes when exiting a context. See Lifetime and ContextManagers for more info.