kbhit() getch() don't work

This is the place for queries that don't fit in any of the other categories.

kbhit() getch() don't work

Postby jmheneghan » Sat Jan 25, 2014 3:48 pm

Code: Select all
import msvcrt
import time
key = "e"
while 1:   
  # if msvcrt.kbhit():
    #  key = msvcrt.getch()
      print "a"
      print key
      print "b"
      time.sleep(2.0)

The above program prints a e b as it should. If I uncomment getch() it does not wait for a keypress and prints a y b with 2 dots over the y.
If I uncomment the kbhit() the program does nothing because the kbhit() always returns false. Yes, I am hitting keys.

I am running on a Macbook Pro, vmware fusion, windows XP. The equivalent program compiled on Borland C works just fine.
Any Python hints would be appreciated. Thanks, Mike
Last edited by metulburr on Sat Jan 25, 2014 3:55 pm, edited 1 time in total.
Reason: initial post lock, added code tags
jmheneghan
 
Posts: 3
Joined: Sat Jan 25, 2014 3:38 pm

Re: kbhit() getch() don't work

Postby metulburr » Sat Jan 25, 2014 3:54 pm

msvcrt module is a windows only module

This is the method for linux. However i dont own a mac, so i wouldnt know if this works in mac. I am assuming it would because they are both UNIX based.
Code: Select all
import sys

if sys.platform[:3] == 'win':
    import msvcrt
    def getkey():
        return msvcrt.getch()
else:
    import termios, sys, os
    TERMIOS = termios

    def getkey():
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
        new[6][TERMIOS.VMIN] = 1
        new[6][TERMIOS.VTIME] = 0
        termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
        c = None
        try:
            c = os.read(fd, 1)
        finally:
            termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
        return c

while True:
    k = getkey().decode()
    print(k)



However using a GUI library would probably get you what you are looking for easier.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1499
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: kbhit() getch() don't work

Postby jmheneghan » Sat Jan 25, 2014 7:03 pm

On the Mac I am running windows XP.
I tried it on a PC running XP (no Mac or Fusion) I have EXACTLY same problem.
This is with Python 2.7.6.

The comment about using a GUI is a good one, but I would like to straighten out the kbhit and getch problem.
jmheneghan
 
Posts: 3
Joined: Sat Jan 25, 2014 3:38 pm

Re: kbhit() getch() works! But not in IDLE

Postby jmheneghan » Sat Jan 25, 2014 10:42 pm

I found the following from craftsman-hambs.blogspot.com/2009/11/getch-in-python-read-character-without.html

" Funny thing is that the code (ie. getch() and kbhit() ) works fine from command window (cmd.exe), but does not in IDLE: in IDLE it does not wait for a key and gives out '\xff' as the result."

I verified that this is true. Can anybody help with running from IDLE???
jmheneghan
 
Posts: 3
Joined: Sat Jan 25, 2014 3:38 pm


Return to General Coding Help

Who is online

Users browsing this forum: Yoriz and 5 guests