[Tutor] Input
Steven D'Aprano
steve at pearwood.info
Sat Oct 1 04:36:57 CEST 2011
Cameron Macleod wrote:
> Hi,
>
> When you type
>
> Input("\n\nPress The Enter Key To Exit")
>
> it forces you to press the enter key to close the program. Why is it the
> enter key instead of e.g. the 'esc' key?
Because the convention is that you use the Enter key to ENTER
information. That's why it is called Enter.
Your question is kind of like asking "Why do you use a screw driver for
driving screws, instead of a socket wrench?" <wink>
In all terminals I know of, the user's input is collected in a buffer
until the Enter key is pressed. Until then, what the user types isn't
available to the caller. This is how raw_input (Python 2) and input
(Python 3) work.
If you want to read the user's input character by character, as they are
typed, it is actually quite tricky, but it can be done. You can install
a full-blown GUI tool kit, like wxPython or Tkinter, and use that. Or
you can use the curses module, which is a bit lighter than Tkinter but
still pretty heavyweight. For Linux, there's a FAQ:
http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
On Windows, you can use msvcrt.getch().
--
Steven
More information about the Tutor
mailing list