[Tutor] how do we represent left arrow key in python programming.

Alan Gauld alan.gauld at btinternet.com
Fri Aug 21 18:48:32 CEST 2009


"Ajith Gopinath" <qbits143 at gmail.com> wrote 

> how do we represent left arrow key in a python program?
> Can anybody help?

That depends entirely on your terminal/environment.
If you are using a Unix terminal it will be diffeent to using 
a Mac or a PC.

You can write a program to display the key code of any 
key using curses on *nix or msvcrt on Windows. Here
is an example from my web tutor under the Event Driven 
Programming topic...

import msvcrt

def doKeyEvent(key):
    if key == '\x00' or key == '\xe0': # non ASCII
       key = msvcrt.getch() # fetch second character
    print ord(key),

def doQuitEvent(key):
    raise SystemExit


# First, clear the screen of clutter then warn the user 
# of what to do to quit
lines = 25 # set to number of lines in console
for line in range(lines): print

print "Hit space to end..."
print

# Now mainloop runs "forever"
while True:
   ky = msvcrt.getch()
   length = len(ky)
   if length != 0:
      # send events to event handling functions
      if ky == " ": # check for quit event
         doQuitEvent(ky)
      else: 
         doKeyEvent(ky)
HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list