pythonpyqtkeyboardinterruptpykdeautokey

Handling KeyboardInterrupt in a KDE Python application?


I'm working on a PyKDE4/PyQt4 application, Autokey, and I noticed that when I send the program a CTRL+C, the keyboard interrupt is not processed until I interact with the application, by ie. clicking on a menu item or changing a checkbox.

lfaraone@stone:~$ /usr/bin/autokey
^C^C^C
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/autokey/ui/popupmenu.py", line 113, in on_triggered
    def on_triggered(self):
KeyboardInterrupt
^C^C^C
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/autokey/ui/configwindow.py", line 423, in mousePressEvent
    def mousePressEvent(self, event):
KeyboardInterrupt

This is despite having the following in /usr/bin/autokey:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from autokey.autokey import Application

a = Application()
try:
    a.main()
except KeyboardInterrupt:
    a.shutdown()
sys.exit(0)

Why isn't the KeyboardInterrupt caught:

Running Ubuntu 9.04 with Python 2.6.


Solution

  • Try doing this:

    import signal
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    

    before invoking a.main().

    Update: Remember, Ctrl-C can be used for Copy in GUI applications. It's better to use Ctrl+\ in Qt, which will cause the event loop to terminate and the application to close.