Wait for a keypress before continuing?

J

John Doe

My program does not need a prompt, it just needs to wait for any
key to be pressed before it continues. This is in Windows.

char=0
while not char:
char=msvcrt.getch()

That doesn't delay anything here.

while 1:
char=msvcrt.getch()
break

That appears to put my program into an endless loop.

while msvcrt.kbhit():
sleep(4)
msvcrt.getch()
msvcrt.getch()

How can that not delay anything and then instantly get past the getch()?

char=0
while msvcrt.kbhit():
sleep(4)
msvcrt.getch()
while not char:
char=msvcrt.getch()

That doesn't delay anything.

Something seriously wrong with my system?


Thanks.
 
S

Steven D'Aprano

John said:
My program does not need a prompt, it just needs to wait for any
key to be pressed before it continues. This is in Windows.

char=0
while not char:
char=msvcrt.getch()

That doesn't delay anything here.

Works perfectly for me. You don't need the while loop, since getch blocks
until a key is pressed.

Rather than making arbitrary changes to the code, try printing char after
the loop exits and see what it contains. That may give you a hint as to
what is going on.

Also, are you using an IDE? If so, it could very well be interfering with
the keyboard buffer, for its own purposes. E.g. in IDLE 2.6.2, if I call
getch it *immediately* returns without blocking:
'\xff'
 
J

John Doe

Steven D'Aprano said:
Also, are you using an IDE? If so, it could very well be
interfering with the keyboard buffer

I really don't know how to answer your question. I am using
Windows XP SP3. Komodo Edit 6 for editing the *.py file. Dragon
Naturally Speaking, Natlink, and Dragonfly might all be part of
the process.

Must be my system. I will post the solution if it comes up.
 
G

Gelonida N

I really don't know how to answer your question. I am using
Windows XP SP3. Komodo Edit 6 for editing the *.py file. Dragon
Naturally Speaking, Natlink, and Dragonfly might all be part of
the process.

Must be my system. I will post the solution if it comes up.

Main question is whether your script is exectued in a console window
(like cmd.exe) or from within a graphical library without console.


you yould try to start cmd.exe and call your python script directly from
there.

if this doesn't work, then try just a simple test script containing
nothing else than the code to wait for a keypress.

Simple test script (working fine for me)
import msvcrt
print "before"
msvcrt.getch()
print "after"
 
J

John Doe

def wait_for_keystroke():
char=0
while not char==0x1B:
char=msvcrt.getch()

That freezes the process. Am I using the right code for the escape
key, or doing anything else wrong?

Again, I know it could be my system. But I must find a way to do this
from within Windows. I use a keyboard hook written in C++, maybe
something from that would be useful, but maybe complex.

Thanks.
 
J

John Doe

def wait_for_keystroke():
char=0
while not (char==chr(27) or char==chr(110)):
char=msvcrt.getch()
if char==0:
return

That freezes the process.
That means char=msvcrt.getch() is getting something?
Could it have something to do with the formatting of the character?
 
J

John Doe

Tim Roberts said:
That exact code works perfectly for me. The function returns as
soon as I press the escape key. You are running this from a
console process, and not a GUI process, right?

No. I am running this from within Windows, all sorts of Windows.

So... Does that mean I will need something complex like a keyboard
hook? Or what?

Thanks.
--
 
J

Jerry Hill

No. I am running this from within Windows, all sorts of Windows.

What does that mean? You seem very resistant to answering anyone's
questions about your code. Is your code run from the command line, or
does it have a GUI? If it has a GUI, what windowing toolkit are you
using? If you have code that's not working, please, show us a short,
run-able bit of sample code that demonstrates the problem you're
experiencing. Describe the behavior you see, the behavior you expected
instead, and the full text of any traceback you may be getting.
 
J

John Doe

Jerry Hill said:
John Doe <jdoe usenetlove.invalid> wrote:

What does that mean?

You snipped the context, Benny.
You seem very resistant to answering anyone's questions about
your code.

No one else has had a problem with my code, Benny, and you have
not questioned my code. I laid it out, they tried it, and now
we're getting on with it.
Is your code run from the command line, or does it have a GUI?

Using "does your code have a GUI" produces zero search results.
Maybe that works better in some other language.
If it has a GUI, what windowing toolkit are you using?

I guess the answer is Dragonfly. Or maybe it's Komodo (the IDE),
as previously stated. I have never been interested in making
Windows, just making Windows dance. I do macroing.
If you have code that's not working, please, show us a short,
run-able bit of sample code that demonstrates the problem you're
experiencing.

Benny... Apparently you have missed at least two other replies
that said they tried some of the code I provided and it worked
fine for them.

As already stated, a hook is probably required. It's not a big
surprise to me, but it's a lot more work. And now I am in the
process of getting it done. I expect the results to be very
pleasant, though. I'm going to have a system that does
voice-activated scripting combined with a systemwide hook, all in
one package. If I can get the hook properly installed, it should
rock, and I'm ready to tackle it.
--



















Describe the behavior you see, the behavior you expected
instead, and the full text of any traceback you may be getting.
Path: news.astraweb.com!border6.newsrouter.astraweb.com!news.glorb.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path: <malaclypse2 gmail.com>
X-Original-To: python-list python.org
Delivered-To: python-list mail.python.org
X-Spam-Status: OK 0.054
X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'sorts': 0.04; 'answering': 0.09; 'demonstrates': 0.09; '16,': 0.15; 'doe': 0.16; 'getting.': 0.16; 'subject:continuing': 0.16; 'wrote:': 0.16; 'header:In- Reply-To:1': 0.22; 'tue,': 0.23; 'pm,': 0.24; 'command': 0.24; 'aug': 0.24; 'traceback': 0.24; 'code': 0.25; 'code.': 0.26; 'bit': 0.28; 'problem': 0.28; 'message-id: mail.gmail.com': 0.29; 'toolkit': 0.30; 'subject:?': 0.31; 'seem': 0.31; 'does': 0.32; 'to:addr:python-list': 0.33; 'describe': 0.34; 'see,': 0.34; 'short,': 0.34; 'running': 0.35; 'instead,': 0.37; 'run': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'your': 0.61; 'john': 0.62; 'full': 0.63; 'show': 0.67; 'received:209.85.215.174': 0.67; 'received:mail-ey0-f174.google.com': 0.67; 'resistant': 0.84; 'windowing': 0.84; 'working,': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=09nEr3tlARCpTiOfhP1XOnteJhDd/baJpJhzNhp5UsI=; b=v9cmI/PvRfaLZhXxYs1bHlUOG+IaGEjPq0Xmx+WTkTOPc5YRFRNivsVO8wgKHaWXZm LhxtRbBJaAJuZlXNZ2rX2BxXaT8VJ6wnn2Z1gRv83Jqxi+jJ4zHcfExLPrLRzxLKZXOc oeVqZwTxJRX8VOytC17/RwVjznYcamlxZsG3Q=
MIME-Version: 1.0
In-Reply-To: <4e4ae844$0$29730$c3e8da3$92d0a893 news.astraweb.com>
References: <4e3f2827$0$5826$c3e8da3$12bcf670 news.astraweb.com> <4e4ad039$0$9663$c3e8da3$76491128 news.astraweb.com> <acpl475c1ae1pgcv73hj2oqln7n0qn6tkl 4ax.com> <4e4ae844$0$29730$c3e8da3$92d0a893 news.astraweb.com>
Date: Tue, 16 Aug 2011 20:11:39 -0400
Subject: Re: Wait for a keypress before continuing?
From: Jerry Hill <malaclypse2 gmail.com>
To: python-list python.org
Content-Type: text/plain; charset=UTF-8
X-BeenThere: python-list python.org
X-Mailman-Version: 2.1.12
Precedence: list
List-Id: General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list>
List-Post: <mailto:python-list python.org>
List-Help: <mailto:python-list-request python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.107.1313539907.27778.python-list python.org>
Lines: 13
NNTP-Posting-Host: 2001:888:2000:d::a6
X-Trace: 1313539907 news.xs4all.nl 23971 [2001:888:2000:d::a6]:45697
X-Complaints-To: abuse xs4all.nl
 
S

Seebs

Using "does your code have a GUI" produces zero search results.
Maybe that works better in some other language.

You shouldn't need a search engine to answer a question about your code.
If you do, it suggests that perhaps one or more of the terms are unfamiliar
to you?

-s
 
J

John Doe

Seebs said:
John Doe <jdoe usenetlove.invalid> wrote:

You shouldn't need a search engine to answer a question about
your code.

Context is lost when you quote only one level.

I was not answering a question about my code. I was pointing out
the fact that my questioner's terminology is strange/corrupt.
If you do, it suggests that perhaps one or more of the terms are
unfamiliar to you?

Yes, even the common term "command line" is foreign to me. I do
some powerful stuff in Windows, without need for a command line.
I realize it exists and that some people live by it, but it has
been nearly useless to me.
--
 
S

Seebs

Context is lost when you quote only one level.

Not significantly.
I was not answering a question about my code. I was pointing out
the fact that my questioner's terminology is strange/corrupt.

Well, that's the thing. There was a question there, with perfectly valid
terminology.
Yes, even the common term "command line" is foreign to me. I do
some powerful stuff in Windows, without need for a command line.

So apparently you *do* know the term. Normally, to say that a term is
foreign to you is to say that you have no idea what it means, not that
you know what it means but don't use it.
I realize it exists and that some people live by it, but it has
been nearly useless to me.

In which case, you're not using a command line, and are using a GUI, and
the other poster's question is answered.

The Google results you cite to are uninteresting and frankly irrelevant.
If someone asks me whether the ornamental fish in my 55-gallon tank is a
koi, that Google has no hits for "ornamental fish in your 55-gallon tank is a
koi" does not mean that the terminology is "strange" or "corrupt".

The terminology was fine.

-s
 
C

Chris Angelico

So apparently you *do* know the term.  Normally, to say that a term is
foreign to you is to say that you have no idea what it means, not that
you know what it means but don't use it.

Unless you're saying it for deliberate effect.

Smith: "To whom do you pay rent?"
Arcadian girl: "Rent? We do not know what it is to pay rent!"
Smith: "Ah. They're Irish."

They know full well what "rent" means, but don't truly comprehend the
concept, as they've never done it. I would say that for many people,
command lines are the same thing. For me, photo editing is like that.

ChrisA
 
J

John Doe

Seebs said:
John Doe <jdoe usenetlove.invalid> wrote:

Not significantly.

Whatever you say, Jeebs.
Well, that's the thing. There was a question there, with
perfectly valid terminology.

And I respect your opinion, Jeebs.
So apparently you *do* know the term.

Not very well, obviously.
Normally, to say that a term is foreign to you is to say that
you have no idea what it means,

Sounds like being a lexicographer is a fantasy of yours, Jeebs.
not that you know what it means but don't use it.

But in fact I do not have a clear understanding of what it means,
Jeebs, but I know that it's a common term.

You are not a lexicographer, dude.
In which case, you're not using a command line, and are using a
GUI, and the other poster's question is answered.

That might have been clear to most normal people in my first reply
to the first follow-up.

"I am using Windows XP SP3. Komodo Edit 6 for editing the *.py
file. Dragon Naturally Speaking, Natlink, and Dragonfly might all
be part of the process."

The answer was pointless by the time the question was asked
straightforward. Thanks to the prior replies, by then I had
already figured out that I need a keyboard hook. The answer
doesn't matter anymore.
The Google results you cite to are uninteresting and frankly
irrelevant.

You have every right to an opinion, Fuckturd.
If someone asks me whether the ornamental fish in my 55-gallon
tank is a koi, that Google has no hits for "ornamental fish in
your 55-gallon tank is a koi" does not mean that the terminology
is "strange" or "corrupt".

No wonder you don't quote relevant material, Jeebs. If anybody
knew what you were comparing that expression to, you would look
stupid.
The terminology was fine.

Are you a master of terminology on wikishit, Jeebs? I think
wikishit sucks. Wannabe lexicographers like you might be a reason.
I've dealt with some real lexicographers, Jeebs, you aren't one.
--


















-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
Path: news.astraweb.com!border6.newsrouter.astraweb.com!news.glorb.com!newsfeeds.sol.net!post2.nntp.sol.net!posts.news.megabitz.net!nnrp3-asbnva.megabitz.net!not-for-mail
Newsgroups: comp.lang.python
From: Seebs <usenet-nospam seebs.net>
Subject: Re: Wait for a keypress before continuing?
References: <4e3f2827$0$5826$c3e8da3$12bcf670 news.astraweb.com> <4e4ad039$0$9663$c3e8da3$76491128 news.astraweb.com> <acpl475c1ae1pgcv73hj2oqln7n0qn6tkl 4ax.com> <4e4ae844$0$29730$c3e8da3$92d0a893 news.astraweb.com> <mailman.107.1313539907.27778.python-list python.org> <4e4b3b02$0$30718$c3e8da3$f017e9df news.astraweb.com> <slrnj4mjr1.2pre.usenet-nospam guild.seebs.net> <4e4b566c$0$3959$c3e8da3$b23f186d news.astraweb.com>
User-Agent: slrn/0.9.9p1 (Darwin)
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <slrnj4mmtc.2sah.usenet-nospam guild.seebs.net>
Date: 17 Aug 2011 06:12:02 GMT
Lines: 40
Organization: Megabitz - More USENET, Faster USENET
NNTP-Posting-Date: 17 Aug 2011 06:12:02 GMT
NNTP-Posting-Host: 3c8d6a06.news.megabitz.net
X-Trace: DXC=BKlkN0:D\OSc::[BQideGP><6FU_Q:4mR^W\Y;gN2lO]C2e6efi]<9Z?jW6Mmc0=4W7d DE\31QYU2V739;<gZ5P?i9TYFJHfQZUBoD_OMJGJU?7AKaKNWGF_
X-Complaints-To: abuse megabitz.net
 
P

peter

Is there an equivalent to msvcrt for Linux users? I haven't found
one, and have resorted to some very clumsy code which turns off
keyboard excho then reads stdin. Seems such an obvious thing to want
to do I am surprised there is not a standard library module for it. Or
have I missed someting (wouldn't be the first time!)

Peter
 
S

Steven D'Aprano

You have every right to an opinion, Fuckturd.

I shouldn't need to say this to anyone over the age of four, but being
obnoxious to people trying to help does not encourage others to answer your
question. You don't win points for insulting people who are trying to solve
your problems.
 
H

Hans Mulder

Is there an equivalent to msvcrt for Linux users? I haven't found
one, and have resorted to some very clumsy code which turns off
keyboard excho then reads stdin. Seems such an obvious thing to want
to do I am surprised there is not a standard library module for it. Or
have I missed someting (wouldn't be the first time!)

The quick and dirty way is to invoke stty(1) using os.system:

import os

def getpassword(prompt="Password: "):
try:
os.system("stty -echo")
passwd = raw_input(prompt)
finally:
os.system("stty echo")
return passwd


Strictly speaking, os.system is deprecated and you should use
the equivalent invocation of subprocess.call:

import subprocess

def getpassword(prompt="Password: "):
try:
subprocess.call(["stty", "-echo"])
passwd = raw_input(prompt)
finally:
subprocess.call(["stty", "echo"])
return passwd


If you don't want to use an external process, use termios:

import termios, sys

def getpassword(prompt="Password: "):
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ECHO # lflags
try:
termios.tcsetattr(fd, termios.TCSADRAIN, new)
passwd = raw_input(prompt)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return passwd


These functions work on any Posix system (including Mac OSX),
but not on Windows.

Hope this helps,

-- HansM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top