[Tutor] need help with msvcrt.getch()

Richard D. Moores rdmoores at gmail.com
Tue Jul 27 19:47:12 CEST 2010


On Tue, Jul 27, 2010 at 09:09, Alan Gauld <alan.gauld at btinternet.com> wrote:

>
> "Richard D. Moores" <rdmoores at gmail.com> wrote
>
>
>  Please see <http://tutoree7.pastebin.com/f3TaxDBc>.
>>
>> I'm trying to recall what I used to know, thus this simple script. But 'y'
>> or 'q' do nothing. What's wrong?
>>
>
> I don't think you need kbhit()


I tried taking it out. You're right, Alan, I don't need it. See <
http://tutoree7.pastebin.com/N8hdyqLZ>


> or the sleep(.1)
>

Testing with and without sleep(.001) shows that sleep(.001) cuts the CPU
usage dramatically, from ~50% to ~5%!

The if kbhit test will only pass if the key is actually hit at that time.
> Often it won't be so you will miss it.
>

I didn't see that, but with a bigger loop I may, I suppose.


> I think getch() will wait for a key to be hit.
>
> But thats from memory and I'm too busy to try it out right now.
>

So I tried putting a counter, c, in the loop. See <
http://tutoree7.pastebin.com/9Q8u3wLw>.

It seems you're correct, in that if I hit 'y' 10 times and then 'q' to
close, I'll get "c = 11". 5 'x's and 5 'y's and a 'q' also gets "c = 11", of
course. Out of curiosity, I tried holding down 'z' (i.e., neither 'y' nor
'q' for exactly 10 seconds, and then hit 'q'. Got "c = 267".

Thanks, Alan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100727/2f171bb7/attachment.html>


More information about the Tutor mailing list