Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I handle the char immediately after its input, without waiting an endline?

9 views
Skip to first unread message

Lave

unread,
Oct 22, 2008, 5:04:08 AM10/22/08
to pytho...@python.org
Hi, all.

I'm a new comer. So This question maybe sutpid.:)

I want to write something that handle every char immediately after its
input. Then tehe user don't need to type [RETURN] each time. How can I
do this?

Thanks in advance.


--
Regards

Lave

Lave

unread,
Oct 22, 2008, 7:17:56 AM10/22/08
to rishi pathak, pytho...@python.org
Yes, it's what i want. Many thanks.

BTW,python-list 's reply is so quick. I love it. I like you all guys.

On 10/22/08, rishi pathak <mailmav...@gmail.com> wrote:
> The below piece of code should give you some understanding
>
> import tty
> import sys
> tty.setraw(sys.stdin.fileno())
> char=''
> print "Press x to exit"
> while char != 'x' :
> char = sys.stdin.read(1)
> print "You entered : ",char
> # Your code here

>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
> --
> Regards--
> Rishi Pathak
> Pune-Maharastra
>


--
Regards

Lave

James Mills

unread,
Oct 22, 2008, 7:22:36 AM10/22/08
to Lave, pytho...@python.org
Lave,

If you're doing this btw, you may want to look at
the curses module or urwid (3rd-party).

cheers
James

> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
--
-- "Problems are solved by method"

Chris Ortner

unread,
Oct 22, 2008, 12:31:03 PM10/22/08
to
Is there a way to do the opposite of tty.setraw afterwards to prevent
the terminal from not displaying any characters that are typed in? Of
course, this can be resolved by re-opening it, but thats not really
convenient.

Terry Reedy

unread,
Oct 22, 2008, 12:39:15 PM10/22/08
to pytho...@python.org
rishi pathak wrote:
> The below piece of code should give you some understanding
>
> import tty
> import sys
> tty.setraw(sys.stdin.fileno())
> char=''
> print "Press x to exit"
> while char != 'x' :
> char = sys.stdin.read(1)
> print "You entered : ",char
> # Your code here

Does not work on Windows, at least with 3.0, as tty fails trying to
import termios. There, use msvcrt module
"msvcrt.kbhit()
Return true if a keypress is waiting to be read.
msvcrt.getch()
Read a keypress and return the resulting character. Nothing is echoed to
the console. This call will block if a keypress is not already
available, but will not wait for Enter to be pressed. If the pressed key
was a special function key, this will return '\000' or '\xe0'; the next
call will return the keycode. The Control-C keypress cannot be read with
this function.
"

>
>
> On Wed, Oct 22, 2008 at 2:34 PM, Lave <lave....@gmail.com
> <mailto:lave....@gmail.com>> wrote:
>
> Hi, all.
>
> I'm a new comer. So This question maybe sutpid.:)
>
> I want to write something that handle every char immediately after its
> input. Then tehe user don't need to type [RETURN] each time. How can I
> do this?
>
> Thanks in advance.
>
>
> --
> Regards
>
> Lave
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
>
>
> --
> Regards--
> Rishi Pathak
> Pune-Maharastra
>
>

> ------------------------------------------------------------------------
>
> --
> http://mail.python.org/mailman/listinfo/python-list

Lie Ryan

unread,
Oct 25, 2008, 4:36:32 AM10/25/08
to pytho...@python.org
>>> I want to write something that handle every char immediately after its
>>> input. Then tehe user don't need to type [RETURN] each time. How can I
>>> do this?
>>>
>>> Thanks in advance.

Don't you think that getting a one-character from console is something
that many people do very often? Do you think that all these platform
independent code should be moved to the interpreter level instead (and
raises the appropriate error when the platform somehow cannot do
unbuffered input)? So python developer could do something like this:

raw_input(bufferring = 0)

Steven D'Aprano

unread,
Oct 25, 2008, 5:04:01 AM10/25/08
to
On Sat, 25 Oct 2008 08:36:32 +0000, Lie Ryan wrote:

>>>> I want to write something that handle every char immediately after
>>>> its input. Then tehe user don't need to type [RETURN] each time. How
>>>> can I do this?
>>>>
>>>> Thanks in advance.
>
> Don't you think that getting a one-character from console is something
> that many people do very often?

No.

I can't think of any modern apps that use one character commands like
that. One character plus a modifier (ctrl or alt generally) perhaps, but
even there, it's mostly used in GUI applications.


> Do you think that all these platform
> independent code should be moved to the interpreter level instead

Absolutely not! There's no need for it to be given a keyword or special
syntax.

But maybe there should be a standard library function for it.


> (and
> raises the appropriate error when the platform somehow cannot do
> unbuffered input)? So python developer could do something like this:
>
> raw_input(bufferring = 0)

No. Leave raw_input as it is. A better interface would be:

import input_services
c = input_services.get_char()

Eventually the module could grow other services as well.

--
Steven

Roel Schroeven

unread,
Oct 25, 2008, 10:30:55 AM10/25/08
to
Steven D'Aprano schreef:

> I can't think of any modern apps that use one character commands like
> that. One character plus a modifier (ctrl or alt generally) perhaps, but
> even there, it's mostly used in GUI applications.

less, vi, info, top, cfdisk, lynx, links, ... come to mind. I suppose
there are many more that I can't think of at the moment.

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

Steven D'Aprano

unread,
Oct 25, 2008, 11:27:32 AM10/25/08
to
On Sat, 25 Oct 2008 16:30:55 +0200, Roel Schroeven wrote:

> Steven D'Aprano schreef:
>> I can't think of any modern apps that use one character commands like
>> that. One character plus a modifier (ctrl or alt generally) perhaps,
>> but even there, it's mostly used in GUI applications.
>
> less, vi, info, top, cfdisk, lynx, links, ... come to mind. I suppose
> there are many more that I can't think of at the moment.

I said modern *wink*

But seriously... point taken.


--
Steven

Lie Ryan

unread,
Oct 25, 2008, 1:57:19 PM10/25/08
to pytho...@python.org
On Sat, 25 Oct 2008 09:04:01 +0000, Steven D'Aprano wrote:

> On Sat, 25 Oct 2008 08:36:32 +0000, Lie Ryan wrote:
>
>>>>> I want to write something that handle every char immediately after
>>>>> its input. Then tehe user don't need to type [RETURN] each time. How
>>>>> can I do this?
>>>>>
>>>>> Thanks in advance.
>>
>> Don't you think that getting a one-character from console is something
>> that many people do very often?
>
> No.
>
> I can't think of any modern apps that use one character commands like
> that. One character plus a modifier (ctrl or alt generally) perhaps, but
> even there, it's mostly used in GUI applications.
>
>
>> Do you think that all these platform
>> independent code should be moved to the interpreter level instead
>
> Absolutely not! There's no need for it to be given a keyword or special
> syntax.

By "interpreter level", I meant python's VM including its standard
libraries (i.e. anywhere but at end-programmer's level), I don't mean it
should have a keyword or special syntax or anything of that sort.

> But maybe there should be a standard library function for it.
>
>
>> (and
>> raises the appropriate error when the platform somehow cannot do
>> unbuffered input)? So python developer could do something like this:
>>
>> raw_input(bufferring = 0)
>
> No. Leave raw_input as it is. A better interface would be:
>
> import input_services
> c = input_services.get_char()

That would be fine as well.

Lie Ryan

unread,
Oct 25, 2008, 6:07:30 PM10/25/08
to pytho...@python.org

I uses some of them a lot... less and top is on the top of my list (pun
intended). I sometimes used vi(m), although I never really liked it, but
it's sometimes unavoidable. info is replaced by man. lynx and links...
well I remember a time when I tried to install Gentoo on a VMWare, lynx/
links (I forgot which one) was a life-saver because I wouldn't need to
get out to the Windows host every two seconds to see the installation
instruction (I was new to Linux at that time), and that was on a VMWare,
what if I installed it directly, not on a virtual machine?

And as far as I know, it is impossible to implement a "press any key"
feature with python in a simple way (as it should be). And if std input's
character buffering is easy, it'd contribute a lot to command-line real
time action games (and of course many other programs, but that is the
first genre of programs that crosses my mind).

PS:
>>> modern != GUI
True
>>> commandline == old
False

Duncan Booth

unread,
Oct 26, 2008, 5:23:41 AM10/26/08
to
Lie Ryan <lie....@gmail.com> wrote:

> And as far as I know, it is impossible to implement a "press any key"
> feature with python in a simple way (as it should be).

"press any key" is a misfeature at the best of times. Quite apart from the
people who can't find the key with 'any' written on it there are also the
people who can't figure out why it 'Ctrl', 'Alt', 'Shift', 'Caps Lock'
aren't keys (not to mention the smartass's who think Ctrl+Break is a key).
It is better to always ask for a specific key.

Have you tried Google? Googling for "python getch" gives
http://snippets.dzone.com/posts/show/915 as the first hit.

Lie Ryan

unread,
Oct 26, 2008, 7:57:48 AM10/26/08
to pytho...@python.org
On Sun, 26 Oct 2008 09:23:41 +0000, Duncan Booth wrote:

> Lie Ryan <lie....@gmail.com> wrote:
>
>> And as far as I know, it is impossible to implement a "press any key"
>> feature with python in a simple way (as it should be).
>
> "press any key" is a misfeature at the best of times. Quite apart from
> the people who can't find the key with 'any' written on it there are
> also the people who can't figure out why it 'Ctrl', 'Alt', 'Shift',
> 'Caps Lock' aren't keys (not to mention the smartass's who think
> Ctrl+Break is a key). It is better to always ask for a specific key.

I know about those jokes. And it's the reason why I mentioned that (the
serious point is about getting one-char input for "command-line GUI"
applications like curse-based apps that is too simple to include the
whole curse).

0 new messages