4

My code is below:

import msvcrt
while True:
    if msvcrt.getch() == 'q':    
       print "Q was pressed"
    elif msvcrt.getch() == 'x':    
       sys.exit()
    else:
       print "Key Pressed:" + str(msvcrt.getch()

This code is based on this question; I was using it to acquaint myself with getch.

I've noticed that it takes 3 pressing the key 3 times to output the text once. Why is this? I'm trying to use it as an event loop, and that's too much of a lag...

Even if I type 3 different keys, it only outputs the 3rd keypress.

How can I force it to go faster? Is there a better way to achieve what I'm trying to achieve?

Thanks!

evamvid

Community
  • 1
  • 1
evamvid
  • 831
  • 6
  • 18
  • 40

2 Answers2

10

you call the function 3 times in your loop. try calling it only once like this:

import msvcrt
while True:
    pressedKey = msvcrt.getch()
    if pressedKey == 'q':    
       print "Q was pressed"
    elif pressedKey == 'x':    
       sys.exit()
    else:
       print "Key Pressed:" + str(pressedKey)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
wizard23
  • 191
  • 2
  • 4
  • why it is 'var pressedKey = msvcrt.getch()'? Don't think it is valid python syntax – Anthony Kong Mar 12 '14 at 22:50
  • 1
    @wizard23 probably had his javascript hat on, but he had the right idea. just remove the `var` – jdigital Mar 12 '14 at 22:52
  • I see the answer is already fixed :) tnx! Yes js slipped in...I could not live test it because I don't have windows here and msvcrt is win32 specific ;) – wizard23 Mar 12 '14 at 23:24
1

You can optimize things a little bit by also using themsvcrt.kbhit function which will allow you callmsvcrt.getch()only as much as is necessary:

while True:
    if msvcrt.kbhit():
        ch = msvcrt.getch()
        if ch in '\x00\xe0':  # arrow or function key prefix?
            ch = msvcrt.getch()  # second call returns the scan code
        if ch == 'q':
           print "Q was pressed"
        elif ch == 'x':
           sys.exit()
        else:
           print "Key Pressed:", ch

Note that theKey Pressedvalue printed won't make sense for things like function keys. That's because it those cases it's really the Windows scan code for the key, not a regular key code for the character.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • how does the 'msvcrt.khbit()` work, exactly? Does it only call `mscvrt.getch()` when it detects a keypress? – evamvid Mar 12 '14 at 23:36
  • I don't know the details, it's part of the MS C/C++ Runtime library. It doesn't call `mscvrt.getch()` AFAIK, it just tells you if there's a key available to get from it. Key strokes are buffered by both the OS and the keyboard hardware. – martineau Mar 12 '14 at 23:46
  • Note that by using the scan code table you can detect function and arrow keys in addition to the regular ones. – martineau Mar 12 '14 at 23:51
  • I wouldn't call this an optimization. In fact it uses much more CPU when you have `if msvcrt.kbhit():` in the code. Try removing the `if msvcrt.kbhit():` and you'll see a much smaller CPU usage (`getch()` is blocking and hence the code without the `if msvcrt.kbhit():` you propose uses much less CPU). – desowin May 18 '18 at 12:50