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
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
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"
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
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)
>>>> 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
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 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
> 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.
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
> 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 <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).