Skip to content

Instantly share code, notes, and snippets.

@pberkes
Created October 24, 2011 22:49
Show Gist options
  • Save pberkes/1310607 to your computer and use it in GitHub Desktop.
Save pberkes/1310607 to your computer and use it in GitHub Desktop.
Show the content of KeyEvents in enable, and preview the new enable behavior
from enable.api import Component, ComponentEditor
from enable.events import KeyEvent
from traits.api import HasTraits, Instance, Str, Bool, Any
from traitsui.api import Item, View, VGroup
import wx
from pyface.qt import QtCore, QtGui
import enable.wx.constants as wx_constants
class KeyEventComponent(Component):
parent = Any
def wx_new_behavior_key_event(self, event, gui_event):
key_code = gui_event.GetKeyCode()
if key_code in wx_constants.KEY_MAP:
character = wx_constants.KEY_MAP[key_code]
elif key_code == wx.WXK_COMMAND:
character = 'Menu'
else:
character = unichr(key_code).lower()
return KeyEvent(event_type=event.event_type,
character=character,
x=event.x, y=event.y,
control_down=gui_event.ControlDown(),
shift_down=gui_event.ShiftDown(),
meta_down=gui_event.MetaDown(),
event=gui_event,
window=event.window)
def qt_new_behavior_key_event(self, event, gui_event):
modifiers = gui_event.modifiers()
character = event.character
import sys
if sys.platform == 'darwin':
# manually switch Meta and Control for Mac OS X
key_code = gui_event.key()
if key_code == QtCore.Qt.Key_Control: character = 'Menu'
elif key_code == QtCore.Qt.Key_Meta: character = 'Control'
control_down = bool(modifiers & QtCore.Qt.MetaModifier)
meta_down = bool(modifiers & QtCore.Qt.ControlModifier)
else:
control_down = bool(modifiers & QtCore.Qt.ControlModifier)
meta_down = bool(modifiers & QtCore.Qt.MetaModifier)
# re-package old event according to new criteria
return KeyEvent(event_type=event.event_type,
character=character,
x=event.x, y=event.y,
alt_down=bool(modifiers & QtCore.Qt.AltModifier),
shift_down=bool(modifiers & QtCore.Qt.ShiftModifier),
control_down=control_down,
meta_down=meta_down,
event=gui_event,
window=event.window)
def new_behavior_key_event(self, event, gui_event):
if isinstance(gui_event, wx._core.KeyEvent):
return self.wx_new_behavior_key_event(event, gui_event)
elif isinstance(gui_event, QtGui.QKeyEvent):
return self.qt_new_behavior_key_event(event, gui_event)
raise ValueError('Unknown key event: %r' % gui_event)
def parse_key_event(self, event):
self.parent.event_repr = repr(event)
self.parent.character = event.character
self.parent.alt_down = event.alt_down
self.parent.control_down = event.control_down
self.parent.shift_down = event.shift_down
self.parent.meta_down = getattr(event, 'meta_down', None)
def normal_key_pressed(self, key_event):
if self.parent.new_behavior:
key_event = self.new_behavior_key_event(key_event, key_event.event)
self.parse_key_event(key_event)
class KeyEventView(HasTraits):
new_behavior = Bool(False)
event_repr = Str
character = Str
alt_down = Bool
control_down = Bool
shift_down = Bool
meta_down = Bool
key_event_component = Instance(KeyEventComponent)
def _key_event_component_default(self):
return KeyEventComponent(parent=self)
traits_view = View(
VGroup(
Item('new_behavior', label='New behavior?'),
Item('key_event_component',
editor=ComponentEditor(),
show_label=False,
width=200,height=-50),
Item('event_repr', style='readonly'),
Item('character', style='readonly'),
Item('alt_down', style='readonly'),
Item('shift_down', style='readonly'),
Item('control_down', style='readonly'),
Item('meta_down', style='readonly'),
),
resizable=True,
title="Key Events"
)
if __name__ == "__main__":
KeyEventView().configure_traits()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment