Using keyboard in my program

Hi guys, I’m new to python and today I want to add keyboard support to my program, namely the ability to exit the program by pressing ‘esc’, but it’s isn’t work, a screenshot will tell you more about it.
python code

Two tips:

  1. It’s best not to use screenshots on technical forums (various reasons, eg not searchable, not accessible for some users)

  2. Try to include all relevant context. In this case, presumably you’ve imported the keyboard module but rather than have people guess or have to ask you to clarify, just give the context up front.

Hope you get the issue figured out :+1:

1 Like

The condition on line 5 will always be true because when you say value == "this" or "that" it’s interpreted as (value == "this") or "that", and "that" is treated as true. Try converting sel_num to lowercase and then doing a single comparison.

On line 8 it’ll wait for you to maybe type something, but then press Enter. It’ll then check to see whether Escape is being pressed at that time, which is unlikely! The simplest fix would be to cancel if no user name is entered, i.e. just press Enter and nothing else.

1 Like

Ohh, okay, i will remember that.

1 Like

Yeah i notice that, now I tried to fix that. Thanks for that

Regarding line 8, indeed the program was waiting for “Enter”, but the program gave an error that I entered the wrong user.

I replaced Escape, to enter, but the program continued to work after entering enter

I tried to replace the if == to if != and it worked. Very illogical (at least for me, but it worked)

In line 5, there is no way to do what you suggested to do, which is to convert sel_num to lowercase and then doing single comparison. I know that there is a function “lower” in python, but I don’t understand how to use it in my case, i tried to do something like that,

if sel_num.lower == “Add new user”:

I know this can’t work, but i tried. Any ideas what to do?

To call a function or method you need the parentheses:

if sel_num.lower() == “Add new user”:

otherwise you’re comparing the method itself to the string.

This doesn’t work. The app simply doesn’t recognize my input, whether it’s in capital letters or lowercase letters.

I think it’s time to show us your current code again. Paste the text in
between code fences:

 ```
 your code goes here
 ```

There’s a </> button in the compose bar to make such a section.

Also, put some debugging statements into your code. Eg, before the
if-statement which tests sel_num, print out both the type and value of
sel_num, for example:

 print("sel_num:", type(sel_num), repr(sel_num))

That may tell you what’s wrong and will give the rest of us some
knowledge of exactly what you’re working with.

Cheers,
Cameron Simpson cs@cskk.id.au

Okay, no problem with that, i can show you part of my code which linked with my function.

import time, keyboard
list_of_reg_num = ['Владимир Петрович', 'Мария Иванова', 'Андрей Осипенко']
sel_num = input("Select a driver from the list: " + str(list_of_reg_num) + "To go to the mode of creating a new driver, enter \"Add new user\"")
def add_new_user():
    if sel_num.lower() == "Add a new user":
        print("You are now in add new user mode...")
        time.sleep(1)
        back = input("To cancel this action, press \"Enter\" on the keyboard: " )
        if back != keyboard.press_and_release('Enter'):
            print("The program will now complete its work...")
            time.sleep(1)
            exit()
        return back
add_new_user()

My output in terminal

Select a driver from the list: ['Владимир Петрович', 'Мария Иванова', 'Андрей Осипенко'] To go to the mode of creating a new driver, enter "Add new user" Add new user
sel_num: <class 'str'> 'Add new user'

I turn off the rest off the program, because i don’t have any problem with this part off the code, and i know i have problem with keyboard method, but right now I am interesting how i can to solving my problem with with the addition of the ability for the user to enter small letters

You should print out the value of sel_num.lower(). It won’t have any upper case letters, so you would want to compare it with "add a new user" (no upper case “A”).

Oh, it really works! Thank you so much! Secondly, I want to finish it with a keyboard. Any ideas on how I can close my program using only the ‘ESC’ key? Currently, my program finishes when I press Enter, but it doesn’t work correctly. In the ‘if’ statement, I used ‘!=’ which means if I don’t press Enter, the program doesn’t close. However, when I use ‘==’, the program doesn’t recognize my input. Any ideas on how to fix that?