Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression of keypad.*.reset() behavior: send key_pressed events #9819

Merged
merged 2 commits into from
Nov 20, 2024

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Nov 19, 2024

@aseanwatson pointed out that there was a regression from 9.0.0 to 9.1.0 for keypad.*.reset(). It started sending key_released instead of key_pressed events. @aseanwatson also figured out the fix (thanks!), which is what is here.

Also documents how to use reset() to determine which keys are held down at program startup.

Fixes #9818, by fixing the mentioned bug.

Using reset() to determine pressed keys makes a new API unneeded (tagging @todbot on this).

Tested on a CPB:

import board
import keypad
import time

keys = keypad.Keys((board.BUTTON_A, board.BUTTON_B), value_when_pressed=True, pull=True)

while True:
    print("reset in 3 seconds...")
    time.sleep(3)
    keys.events.clear()
    keys.reset()
    while event := keys.events.get():
        print(event)

Confirmed that it worked in 9.0.0, gave the wrong events in 9.1.0, and is fixed by the change.

@dhalbert dhalbert requested a review from jepler November 19, 2024 23:10
@todbot
Copy link

todbot commented Nov 20, 2024

This works great for my simple cases of detecting a held button on reset/boot. e.g.

import board, keypad, time
keys = keypad.Keys( (board.GP8,), value_when_pressed=False, pull=True )
keys.reset()
if key := keys.events.get():
    print("KEY HELD AT BOOT")
while True:
    print("HI")
    if key := keys.events.get():
        print(key)  # will print release message for held key
    time.sleep(1)

Copy link
Member

@jepler jepler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't test but I think I understand what the change intends to do. Thanks!

@dhalbert dhalbert merged commit 17d40f5 into adafruit:main Nov 20, 2024
476 checks passed
@dhalbert dhalbert deleted the keys-generic-reset-fix branch November 20, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Need a way to handle keys pressed at keypad init
4 participants