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, t...@probo.com
Providenza & Boekelheide, Inc.

_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to