[python-win32] More relating to catching cursor keystrokes in command line

Jacob Kruger jacobk at mailzone.co.za
Tue Jul 5 21:43:32 CEST 2011


Thanks.

Will check it out, but, BTW, the documentation isn't perfectly rendered as
far as I'm concerned, since I find mailing lists the best place to do
research on these matters, although have looked through documentation
already, searched around places like codeproject, done a few google
searches, etc., but didn't find a real answer until now, but, like you said,
since it's not really platform independent, I think will for now leave it
alone as such.

Thanks again for help, either way

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

----- Original Message ----- 
From: "Tim Roberts" <timr at probo.com>
To: "Python-Win32 List" <python-win32 at python.org>
Sent: Tuesday, July 05, 2011 9:20 PM
Subject: Re: [python-win32] More relating to catching cursor keystrokes in
command line


> Jacob Kruger wrote:
>> The following seems to actually work relatively consistently after just a
>> bit of testing:
>> #bit of code sample
>> while True:
>>   sKey = msvcrt.getwch()
>>     if str(ord(sKey)) == "77":
>> #end of code sample
>>
>> And then on this windows7 machine, the following are the strings that
>> seem to match:
>> 72 = up
>> 75 = left
>> 77 = right
>> 80 = down
>
> "SEEM" to match?  Are you reading any of the replies in this thread?
> Did you read my explanation earlier?  Have you read ANY of the
> documentation?  You don't need to guess about this.  The behavior of
> getch in DOS is well-defined.
>
> The arrow keys (ALL of the non-ASCII keys, in fact) generate TWO bytes
> in getch.  The first byte is 224 (or hex 0xE0).  That is a special code
> that tells you "this is the first byte of a two-byte key code).  The
> second byte will then be the set you have posted.
>
> What on earth is the point of converting to string with
> "str(ord(sKey))"?  Why wouldn't you just write "if ord(sKey) == 77"?
>
> So, a more general solution might be:
>
>    def getKeyCode():
>        x = ord(msvcrt.getch())
>        if x != 224: return x
>        return (x << 8) | ord(msvcrt.getch())
>
> Now, you can tell the difference between up-arrow and the H key.
>
>    k = getKeyCode()
>    if k == ord('H'):
>        print "H key pressed"
>    elif k == 0xE048:
>        print "Up-arrow pressed"
>
>> Anyway, this is just testing, and like you said, would most likely not
>> work too cross platform as such, but anyway.
>
> It DEFINITELY does not work cross-platform.  That key-code sequencing is
> a remnant of the BIOS in PCs, and this getch behavior is leftover from
> MS-DOS.  Linux requires an entirely different approach.
>
> -- 
> Tim Roberts, timr at probo.com
> Providenza & Boekelheide, Inc.
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32



More information about the python-win32 mailing list