2

I would like to know how to get user input in python without using the command line or an input box. Let me explain. I do not want to do this

#All code is python 3
name=input("What is your name?")

Why? When running scripts, the command line is not auto-focused. Furthermore, it pops up another window, something I do not want because I can't hit escape to close it in a hurry (Something which you may want to do if you're playing a game).

What have I tried? I looked at WX and it's dialog function, something like this:

import wx
app=wx.App()
def text_entry(title,message):
    result=None
    dlg=wx.TextEntryDialog(None, message,title)
    if dlg.ShowModal()==wx.ID_OK: result=dlg.GetValue()
    dlg.Destroy()
    return result
text_entry("Text entry","Enter something here")

While this works, it pops up another window which again, I do not want. However, it is closer to what I am ultimately looking for, because I can hit escape to make it go away.

I have tried using pygame and it's key.get_pressed() function, but it inserts a lot of the same letter into the entry, even if I gently tap the key. Also, when I implemented it into the project, it can only pick up on normal letters. Writing 26 if statements to detect key presses for a single letter with or without the shift key seems a little counter intuitive. Finally, I am a bit hesitant to try tkinter. I happen to be blind, and from what I read, tk is very visual, which makes me concerned that it won't play nicely with my screen reader (NVDA).

So, I'm here. After searching on google for "getting input without using command line in python 3", "input in the same window", and "input without using input()" yielded nothing.

To recap, I want to accept user input without using the input() function, and without any additional windows popping up for the duration of me doing so. Thank you.

2 Answers 2

1

What about this solution using the msvcrt module. At any time if you press escape then the program will exit. Python sys.exit(), and built-ins exit() and quit() all call raise SystemExit so this is just one less call to perform. If you press the enter or return key then the while loop ends and you can use the keys that were pressed later in your program as they are stored in the variable user_input. The print at the end just proves that the pressed keys are stored in user_input variable and the input() function simply to leave the window open so you can see it working.

import msvcrt


user_input = b''

while True:
    pressed_key = msvcrt.getche()  # getch() will not echo key to window if that is what you want
    if pressed_key == b'\x1b':  # b'\x1b' is escape
        raise SystemExit
    elif pressed_key == b'\r':  # b'\r' is enter or return
        break
    else:
        user_input += pressed_key

print('\n' + user_input.decode('utf-8'))  # this just shows you that user_input variable can be used now somewhere else in your code
input()  # input just leaves the window open so you can see before it exits you may want to remove
Sign up to request clarification or add additional context in comments.

5 Comments

Yes and no. While it is a good solution, It still does not allow me to hit escape to close the input dialog, which is what I really would like
Thank you. Do the characters change language to language, or do they stay the same? I am asking because your solution detects every single key that is being pressed, and I'm thinking of creating a tuple to store all the possible byte combinations. That way, I could loop through the list and check if the key pressed is in the list and if so, add it to the user_input. Obviously, that'd only work if there was one set of byte combinations for every language, hence me asking. If you don't understand what I mean, try running your example and hit your arrows, escape, and f 1 through 12 keys.
I am not sure how the characters are read in python in a different language other than English because some languages have different characters for example Mandarin Chinese - which is presumably what you are asking. If you want to see what each key represents you can add this to the else block: else: print(pressed_key) and it will print each key in bytes. Also this link provides the hexadecimal values for each character asciitable.com. What keys do you want to block from being stored in user_input var?
Also one more question, from reading your edited question above do you even want the keys you pressed shown on the screen so a user can see what they are typing? Or do you not want the user to see what they are typing?
In answer to your first question, I would like to block anything that is not alphabetic, Numeric, or symbolic. In answer to your second question, yes, I do want the user to see what they type.
0

So after doing some more research, I found this: https://codeload.github.com/Nearoo/pygame-text-input/zip/master I think this is what I am looking for, though it still needs to be slightly modified. Thank you for the assistance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.