[Tutor] How to capture 'Ctrl+c' in Python

Chris Hengge pyro9219 at gmail.com
Fri Nov 3 08:25:40 CET 2006


I'm not sure where I got this from, but I think its from your site..

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)


On 11/2/06, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
> "Chris Hengge" <pyro9219 at gmail.com> wrote
> > Do you by chance know of a way to capture special keys like "Print
> > Screen"?
>
> Try the key capture code in my Event Driven topic.
>
> So far as I know it works for all keys including the special ones.
> It also points out that those keys have a two part code...
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061102/59e4eb11/attachment.htm 


More information about the Tutor mailing list