Discussion:
How can I handle the char immediately after its input, without waiting an endline?
(too old to reply)
Lave
2008-10-22 09:04:08 UTC
Permalink
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
2008-10-22 11:17:56 UTC
Permalink
Yes, it's what i want. Many thanks.

BTW,python-list 's reply is so quick. I love it. I like you all guys.
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"
char = sys.stdin.read(1)
print "You entered : ",char
# Your code here
Post by Lave
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
--
Regards

Lave
James Mills
2008-10-22 11:22:36 UTC
Permalink
Lave,

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

cheers
James
Post by Lave
Yes, it's what i want. Many thanks.
BTW,python-list 's reply is so quick. I love it. I like you all guys.
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"
char = sys.stdin.read(1)
print "You entered : ",char
# Your code here
Post by Lave
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
--
Regards
Lave
--
http://mail.python.org/mailman/listinfo/python-list
--
--
-- "Problems are solved by method"
Chris Ortner
2008-10-22 16:31:03 UTC
Permalink
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
2008-10-22 16:39:15 UTC
Permalink
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"
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.
"
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
2008-10-25 08:36:32 UTC
Permalink
Post by Lave
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
2008-10-25 09:04:01 UTC
Permalink
Post by Lie Ryan
Post by Lave
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.
Post by Lie Ryan
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.
Post by Lie Ryan
(and
raises the appropriate error when the platform somehow cannot do
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
2008-10-25 14:30:55 UTC
Permalink
Post by Steven D'Aprano
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
2008-10-25 15:27:32 UTC
Permalink
Post by Roel Schroeven
Post by Steven D'Aprano
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
2008-10-25 22:07:30 UTC
Permalink
Post by Steven D'Aprano
Post by Roel Schroeven
Post by Steven D'Aprano
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.
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).
Post by Steven D'Aprano
Post by Roel Schroeven
Post by Steven D'Aprano
modern != GUI
True
Post by Steven D'Aprano
Post by Roel Schroeven
Post by Steven D'Aprano
commandline == old
False
Duncan Booth
2008-10-26 09:23:41 UTC
Permalink
Post by Lie Ryan
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
2008-10-26 11:57:48 UTC
Permalink
Post by Duncan Booth
Post by Lie Ryan
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).

Lie Ryan
2008-10-25 17:57:19 UTC
Permalink
Post by Steven D'Aprano
Post by Lie Ryan
Post by Lave
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.
Post by Lie Ryan
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.
Post by Steven D'Aprano
But maybe there should be a standard library function for it.
Post by Lie Ryan
(and
raises the appropriate error when the platform somehow cannot do
raw_input(bufferring = 0)
import input_services
c = input_services.get_char()
That would be fine as well.
Post by Steven D'Aprano
Eventually the module could grow other services as well.
Loading...