Direction keys not recognized in Qt4 backend

I'm running the Qt4 backend, and I noticed that I'd frequently get error messages saying I was pressing unrecognized keys. It turns out that the direction keys aren't recognized in the qt4 backend. (I'm using direction keys to switch between spaces in OSX, so this error gets triggered quite frequently)

One possible fix is to just add the direction keys to the list of valid keys (see patch below). Alternatively, the key event code could just ignore unrecognized keys (i.e. `key == None`). This change could be made in FigureCanvasQT.keyPressEvent (in backends.backend_qt4.py) or, more generally, in FigureCanvasBase.key_press_event (in backend_bases.py).

-Tony

%---Diff

Index: lib/matplotlib/backends/backend_qt4.py

···

===================================================================
--- lib/matplotlib/backends/backend_qt4.py (revision 8315)
+++ lib/matplotlib/backends/backend_qt4.py (working copy)
@@ -129,6 +129,10 @@
     keyvald = { QtCore.Qt.Key_Control : 'control',
                 QtCore.Qt.Key_Shift : 'shift',
                 QtCore.Qt.Key_Alt : 'alt',
+ QtCore.Qt.Key_Up : 'up',
+ QtCore.Qt.Key_Right : 'right',
+ QtCore.Qt.Key_Down : 'down',
+ QtCore.Qt.Key_Left : 'left',
                }
     # left 1, middle 2, right 3
     buttond = {1:1, 2:3, 4:2}

%---Full traceback

Traceback (most recent call last):
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_qt4.py", line 198, in keyPressEvent
    FigureCanvasBase.key_press_event( self, key )
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 1459, in key_press_event
    self.callbacks.process(s, event)
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/cbook.py", line 169, in process
    func(*args, **kwargs)
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 2079, in key_press
    if event.key in fullscreen_keys:
TypeError: 'in <string>' requires string as left operand, not NoneType

I can commit the change to backend_qt4, but I need to run the change
to backendbases by the other devs. Is there any reason not to do the
following?:

···

On Fri, May 14, 2010 at 2:39 PM, Tony S Yu <tonyyu@...608...> wrote:

I'm running the Qt4 backend, and I noticed that I'd frequently get error messages saying I was pressing unrecognized keys. It turns out that the direction keys aren't recognized in the qt4 backend. (I'm using direction keys to switch between spaces in OSX, so this error gets triggered quite frequently)

One possible fix is to just add the direction keys to the list of valid keys (see patch below). Alternatively, the key event code could just ignore unrecognized keys (i.e. `key == None`). This change could be made in FigureCanvasQT.keyPressEvent (in backends.backend_qt4.py) or, more generally, in FigureCanvasBase.key_press_event (in backend_bases.py).

-Tony

%---Diff

Index: lib/matplotlib/backends/backend_qt4.py

--- lib/matplotlib/backends/backend_qt4.py (revision 8315)
+++ lib/matplotlib/backends/backend_qt4.py (working copy)
@@ -129,6 +129,10 @@
keyvald = { QtCore.Qt.Key_Control : 'control',
QtCore.Qt.Key_Shift : 'shift',
QtCore.Qt.Key_Alt : 'alt',
+ QtCore.Qt.Key_Up : 'up',
+ QtCore.Qt.Key_Right : 'right',
+ QtCore.Qt.Key_Down : 'down',
+ QtCore.Qt.Key_Left : 'left',
}
# left 1, middle 2, right 3
buttond = {1:1, 2:3, 4:2}

%---Full traceback

Traceback (most recent call last):
File "/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_qt4.py", line 198, in keyPressEvent
FigureCanvasBase.key_press_event( self, key )
File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 1459, in key_press_event
self.callbacks.process(s, event)
File "/Users/Tony/python/devel/mpl/lib/matplotlib/cbook.py", line 169, in process
func(*args, **kwargs)
File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 2079, in key_press
if event.key in fullscreen_keys:
TypeError: 'in <string>' requires string as left operand, not NoneType

===================================================================
--- lib/matplotlib/backend_bases.py (revision 8306)
+++ lib/matplotlib/backend_bases.py (working copy)
@@ -1,4 +1,3 @@
-
"""
Abstract base classes define the primitives that renderers and
graphics contexts must implement to serve as a matplotlib backend
@@ -1450,9 +1449,11 @@

     def key_press_event(self, key, guiEvent=None):
         """
- This method will be call all functions connected to the
+ This method will call all functions connected to the
         'key_press_event' with a :class:`KeyEvent`
         """
+ if key is None:
+ return
         self._key = key
         s = 'key_press_event'
         event = KeyEvent(s, self, key, self._lastx, self._lasty,
guiEvent=guiEvent)