0

Simple "input" in python:

code = input("Entrer your code...")
processCode(code)

I need to ask the user for a password on a usb keyboard but without a screen (so the user doesn't see what he is typing). The text here is just for some tests. The sending is validated by the Enter key of course.

To make sure that the input is always blank when the user starts typing and sends his code, I will need to add a condition to this input. I would need some sort of time counter which starts after each character entered and if the Enter key is not pressed for 10 seconds, the input will be automatically reset.

martineau
  • 119,623
  • 25
  • 170
  • 301
rootbot
  • 41
  • 1
  • 1
  • 5
  • 1
    The answers given here may help you : [https://stackoverflow.com/questions/24072790/detect-key-press-in-python](https://stackoverflow.com/questions/24072790/detect-key-press-in-python) – Tobin Nov 04 '20 at 22:01
  • @Tobin keyboard library apparently requires root in linux – rootbot Nov 04 '20 at 23:07

1 Answers1

0

Here is an example of code that approximates your question. You can improve it or take inspiration from it:

import keyboard
import time
from threading import Thread

start_time = time.time()
saved_pwd = False
stop_thread = False


def dedupe(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)


def count_time():
    global saved_pwd, start_time
    while True:
        lap_time = round(time.time() - start_time, 2)
        global stop_thread
        if lap_time >= 10:
            print("Please Re-enter the password")
            saved_pwd = True
            break
        elif stop_thread:
            break


password = []
Thread(target=count_time).start()
while saved_pwd is False:
    key = keyboard.read_key()
    start_time = time.time()
    if key == 'enter':
        saved_pwd = True
        stop_thread = True
    else:
        password.append(key)

print("Your pwd: ", ''.join(dedupe(password)))
Tobin
  • 2,029
  • 2
  • 11
  • 19