What I am trying to do
I am trying to design a stopwatch with lap timing. When you press "L" then a lap is completed and when you press "S" all laps are completed and the timings are displayed in order.
While in C++ I can do this with the function _getch() from conio.h and it would be quite easy. I want to write this program in python as it would be a lot more easier and also the time handling in C++ proved to be hard.
Still I did write a program (which was for cube timing) on this link: Cube timer
Problem
There is no function like _getch(). And this is a problem because at the end of a lap you can't press a key and an enter key because it would be time taking and irritating for the user.
things I read
I read about the curses library but alas it has no windows port.
I tried a program that was supposed to work according to the website. This was from the link
Link to getch recipe
But it did not work.
What I tried:
msvcrt.getch()
>>> import msvcrt >>> msvcrt.getch() '\xff'
I believe that FF is the hexadecimal equivalent of 255.
I did not understand why this is happenning.
readch()
as sugggested in @martineauimport msvcrt
def readch(echo=True): "Get a single character on Windows." while msvcrt.kbhit(): # clear out keyboard buffer msvcrt.getch() ch = msvcrt.getch() while ch in '\x00\xe0': # arrow or function key prefix? msvcrt.getch() ch = msvcrt.getch() # second call returns the actual key code if echo: msvcrt.putch(ch) return ch.decode()
a = []
for i in range(10): a.append(readch())
The error that i got:
>>>
Traceback (most recent call last):
File "C:/Python25/1.py", line 30, in <module>
a.append(readch())
File "C:/Python25/1.py", line 25, in readch
return ch.decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
What I need help with
A function that works like _getch() or something equivalent that works on a windows machine.
Machine specs
Python IDLE 2.5.4 or 2.6 or 2.7
Windows XP SP3