473,287 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Using msvcrt (in Windows), how to catch Enter key?

Windows XP Pro, Python 2.5.1

import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'Enter'
do something

Is there a way to catch the pressing of the 'Enter' key?

Thanks,

Dick Moores

Oct 29 '07 #1
11 20487
On Oct 29, 11:26 am, Dick Moores <r...@rcblue.comwrote:
Windows XP Pro, Python 2.5.1

import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'Enter'
do something

Is there a way to catch the pressing of the 'Enter' key?
Yes there is. Just open the Python shell and see what is being
returned by `getch` or `getche` functions when you press Enter:
>>import msvcrt
msvcrt.getch()
'\r'

Also try to avoid `busy waiting` and calling msvcrt.kbhit in a loop
without a sleep statement. I don't know your case but probably this
should be enough:

while True:
if msvcrt.getch() == '\r':
...

fw

Oct 29 '07 #2
On Oct 29, 4:26 am, Dick Moores <r...@rcblue.comwrote:
Windows XP Pro, Python 2.5.1

import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'Enter'
do something

Is there a way to catch the pressing of the 'Enter' key?

Thanks,

Dick Moores
Let's find out:
>>from msvcrt import getch
while True:
.... key = getch()
.... if key: print repr(key)+',',
....
'p', 'r', 'e', 's', 's', 'i', 'n', 'g', ' ', 'e', 'n', 't', 'e', 'r',
':', ' ', '\r',

Gee, I pressed enter, and it returned '\r'. I wonder...
>>import msvcrt
while True:
.... if msvcrt.kbhit():
.... key = msvcrt.getch()
.... if key == '\r':
.... print "success!"
....
success!

Oct 29 '07 #3
At 04:29 AM 10/29/2007, Filip Wasilewski wrote:
>On Oct 29, 11:26 am, Dick Moores <r...@rcblue.comwrote:
Windows XP Pro, Python 2.5.1

import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'Enter'
do something

Is there a way to catch the pressing of the 'Enter' key?

Yes there is. Just open the Python shell and see what is being
returned by `getch` or `getche` functions when you press Enter:
>import msvcrt
msvcrt.getch()
'\r'
Terrific! Thanks.
>Also try to avoid `busy waiting` and calling msvcrt.kbhit in a loop
without a sleep statement.
I don't know your case but probably this
should be enough:

while True:
if msvcrt.getch() == '\r':
I tried it and find that without the msvcrt.kbhit the first key I hit
doesn't do anything. I have to hit that key again, or another key.

Dick

Oct 29 '07 #4
On 29 oct, 09:23, Dick Moores <r...@rcblue.comwrote:
while True:
if msvcrt.getch() == '\r':

I tried it and find that without the msvcrt.kbhit the first key I hit
doesn't do anything. I have to hit that key again, or another key.
I'd say there is a logic error in your program then; keys don't "do
anything" by themselves.
Try posting a small sample, telling what you get and what you expect.

--
Gabriel Genellina

Oct 29 '07 #5
At 09:26 AM 10/29/2007, Gabriel Genellina wrote:
>On 29 oct, 09:23, Dick Moores <r...@rcblue.comwrote:
>while True:
if msvcrt.getch() == '\r':
I tried it and find that without the msvcrt.kbhit the first key I hit
doesn't do anything. I have to hit that key again, or another key.

I'd say there is a logic error in your program then; keys don't "do
anything" by themselves.
Try posting a small sample, telling what you get and what you expect.
Huh. Works now.

import msvcrt
while True:
key = msvcrt.getch()
if key == 'h':
print 'Hello'
if key == 'b':
print 'Bye'
if key == '\r': # 'Enter' key
break

Dick

Oct 29 '07 #6
At 09:53 AM 10/29/2007, Dick Moores wrote:
>At 09:26 AM 10/29/2007, Gabriel Genellina wrote:
On 29 oct, 09:23, Dick Moores <r...@rcblue.comwrote:
while True:
if msvcrt.getch() == '\r':
>
I tried it and find that without the msvcrt.kbhit the first key I hit
doesn't do anything. I have to hit that key again, or another key.
I'd say there is a logic error in your program then; keys don't "do
anything" by themselves.
Try posting a small sample, telling what you get and what you expect.

Huh. Works now.

import msvcrt
while True:
key = msvcrt.getch()
if key == 'h':
print 'Hello'
if key == 'b':
print 'Bye'
if key == '\r': # 'Enter' key
break

Dick
But here's a case where it seems I do need the

if msvcrt.kbhit() line

=========================
#!/usr/bin/env python
#coding=utf-8
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'h':
print 'Hello'
if key == 'b':
print 'Bye'
if key == '\r': # Enter key
break
timeNow = time.time()
if timeNow - oldTimeNow 5:
print "5 seconds passed"
oldTimeNow = timeNow
==========================

Without that line:
==========================
#!/usr/bin/env python
#coding=utf-8
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
#if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'h':
print 'Hello'
if key == 'b':
print 'Bye'
if key == '\r': # Enter key
break
timeNow = time.time()
if timeNow - oldTimeNow 5:
print "5 seconds passed"
oldTimeNow = timeNow
============================

Without that line the "5 seconds passed" report is printed ONLY after
a "b" or an "h", not what I want.

Dick

Oct 29 '07 #7
En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <rd*@rcblue.comescribió:
But here's a case where it seems I do need the

if msvcrt.kbhit() line
At least add a small sleep() call inside the loop, to be nice to other
running processes:
=========================
#!/usr/bin/env python
#coding=utf-8
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'h':
print 'Hello'
if key == 'b':
print 'Bye'
if key == '\r': # Enter key
break
else:
time.sleep(0.1)
timeNow = time.time()
if timeNow - oldTimeNow 5:
print "5 seconds passed"
oldTimeNow = timeNow
==========================
--
Gabriel Genellina

Oct 29 '07 #8
At 03:23 PM 10/29/2007, Gabriel Genellina wrote:
>En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <rd*@rcblue.comescribió:
But here's a case where it seems I do need the

if msvcrt.kbhit() line

At least add a small sleep() call inside the loop, to be nice to other
running processes:
=========================
#!/usr/bin/env python
#coding=utf-8
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == 'h':
print 'Hello'
if key == 'b':
print 'Bye'
if key == '\r': # Enter key
break
else:
time.sleep(0.1)
timeNow = time.time()
if timeNow - oldTimeNow 5:
print "5 seconds passed"
oldTimeNow = timeNow
=========================
Yes, that makes a major difference in the CPU
usage percentage on my computer. In fact I can't
even tell that there is anything going on other
than the usual behind-the-scenes XP stuff. CPU
usage stays right around 0% or 6%, with an
occasional 6% and a very occasional 15%.
Interestingly, sleep(0.001) makes as big a
difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady100%!

Thanks,

Dick

Oct 30 '07 #9
En Mon, 29 Oct 2007 21:22:36 -0300, Dick Moores <rd*@rcblue.comescribió:
At 03:23 PM 10/29/2007, Gabriel Genellina wrote:
>En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <rd*@rcblue.com>
escribió:
At least add a small sleep() call inside the loop, to be nice to other
running processes:

Yes, that makes a major difference in the CPU
usage percentage on my computer. In fact I can't
even tell that there is anything going on other
than the usual behind-the-scenes XP stuff. CPU
usage stays right around 0% or 6%, with an
occasional 6% and a very occasional 15%.
Interestingly, sleep(0.001) makes as big a
difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady
100%!
The underlying function in Windows is Sleep (or SleepEx) which takes an
argument in milliseconds. 0.0001s = 0.1ms and it's rounded to 0. Sleep(0)
has very specific semantics - for a single threaded program, it does
nothing, so your code is effectively a busy loop taking 100% CPU.

--
Gabriel Genellina

Oct 30 '07 #10
At 06:34 PM 10/29/2007, Gabriel Genellina wrote:
>En Mon, 29 Oct 2007 21:22:36 -0300, Dick Moores <rd*@rcblue.comescribió:
At 03:23 PM 10/29/2007, Gabriel Genellina wrote:
En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <rd*@rcblue.com>
escribió:
At least add a small sleep() call inside the loop, to be nice to other
running processes:
Yes, that makes a major difference in the CPU
usage percentage on my computer. In fact I can't
even tell that there is anything going on other
than the usual behind-the-scenes XP stuff. CPU
usage stays right around 0% or 6%, with an
occasional 6% and a very occasional 15%.
Interestingly, sleep(0.001) makes as big a
difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady
100%!

The underlying function in Windows is Sleep (or SleepEx) which takes an
argument in milliseconds. 0.0001s = 0.1ms and it's rounded to 0. Sleep(0)
has very specific semantics - for a single threaded program, it does
nothing, so your code is effectively a busy loop taking 100% CPU.
Ah, useful information. Thank you. Where'd you learn that?

Dick Moores
Oct 30 '07 #11
En Tue, 30 Oct 2007 01:46:19 -0300, Dick Moores <rd*@rcblue.comescribió:
At 06:34 PM 10/29/2007, Gabriel Genellina wrote:
>The underlying function in Windows is Sleep (or SleepEx) which takes an
argument in milliseconds. 0.0001s = 0.1ms and it's rounded to 0.

Ah, useful information. Thank you. Where'd you learn that?
Microsoft's Windows API Reference:
http://msdn2.microsoft.com/en-us/library/aa383749.aspx

The Sleep function is at
http://msdn2.microsoft.com/en-us/library/ms686298.aspx

--
Gabriel Genellina

Oct 30 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oleg | last post by:
Hi People, Does anybody knows how to set up a V.S. .NET. I am trying to use Windows libraries in my code, but got en error that library in not found, not installed, etc. here is the simple...
1
by: ½£¾ªºè | last post by:
Give an example:When user pressed "B" or "b",HOOK get that message and let TextBox1.text display "a". The executing founction is as following: **************************************** IntPtr...
5
by: Richard Lionheart | last post by:
Hi All, I tried creating a C# Form using Petzold's "Programming MS Windows with C#" guidance. He recommended using the namespace System.Windows.Forms. However, VS.NET claims "the type or...
2
by: Mario | last post by:
Hi, I am trying to create an application with multiple windows forms. The problem that I have is that after creating the window forms, I do not know how to open formN after closing Main form. ...
2
by: pratcp | last post by:
I have a remote server (a shared hosting webserver) where I have few users ftp some flat files. I need to access these files using a windows app/windows service written using c# and run it on a...
9
by: garyusenet | last post by:
I am using a C# solution that somebody kindly sent me. It was a console application. I am trying to get use of the MessageBox. I have added the following using directive to the namespaces at...
4
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
3
by: piedpiper | last post by:
Dear All, When i use: using System.Windows.Forms; I get the following error when compiling: Error 3 : The type or namespace name 'Windows' does not exist in the namespace 'System' (are you...
3
by: parez | last post by:
Whats the best way of handing exception of using? Is it method 1 or method 2 or some other way ########## Method 1 ########### using(something) { try { } catch {
1
by: maheshgupta024 | last post by:
Im very new to PHP, and interseted in network stuff, using php can i enter into different computers from network within my LAN. Can anyone help on this.. Thanks in Advance
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.