From 7aa21c0199d8ef3f8463920ae27cab71eecb23a1 Mon Sep 17 00:00:00 2001 From: Moustafa Fadlalla <156524696+Moustafa-FD@users.noreply.github.com> Date: Sun, 5 Jan 2025 23:13:18 -0500 Subject: [PATCH] Keypad repetition removal - This stops from a key being repeated multiple times while pressed, once a key is pressed it will not be registered until it has been released. --- keypad.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/keypad.py b/keypad.py index f7b25af..00757d2 100644 --- a/keypad.py +++ b/keypad.py @@ -73,3 +73,29 @@ def read_keypad(self): return key_pressed col_pin.value(1) # Set column pin back to HIGH return None # Return None if no key is pressed + + + + + + def key_read(keypad): + """ + Reads a single key press and waits for its release before returning. + + Args: + keypad (Keypad): An instance of the Keypad class. + + Returns: + str: The pressed key. + """ + value = None + + while True: + key = keypad.read_keypad() # Read the keypad + if key: # A key is pressed + value = key # Store the pressed key + while keypad.read_keypad(): # Wait until the key is released + sleep(0.05) # Small delay to reduce CPU usage + break # Exit the loop once the key is released + + return value # Return the detected key