2012年6月14日 星期四

Python wxwidgets bind key 'Ctrl + C'

處理原生 EVT_KEY_DOWN 事件
    self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)

    def on_key_down(self, event):
        """Key down event handler."""
        key = event.KeyCode()
       
        controlDown = event.ControlDown()
        altDown = event.AltDown()
        shiftDown = event.ShiftDown()

        # Cut to the clipboard.
        if (controlDown and key in (ord('C'), ord('c'))):
            copy()
        # Insert the next command from the history buffer.
        else:
            event.Skip()

Reference :
CharacterCodesAndKeyboards - wxPyWiki


使用 wx_tools class
import wx_tools
class Frame(wx.Frame):
    def __init__(self, *args, **keywords):
       self.__init_key_handlers()

    def __init_key_handlers(self):
        '''Initialize key shortcuts.'''
  
        self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
 
        def copy():
            print 'copy'

        self.key_handlers = {
            Key(ord('C'), cmd=True): copy,
         }

    def on_key_down(self, event):
        '''wx.EVT_KEY_DOWN handler.'''
        key = Key.get_from_key_event(event)
        handler = self.key_handlers.get(key, None)
        if handler:
            handler()
        else:
            event.Skip()

Reference :
Nullege: A Search Engine for Python source code
garlicsim_wx.widgets.workspace_widgets.crunching_controls.step_profiles_controls.step_profiles_list.step_profiles_list :: garlicsim_wx 0.6.3 : PyDoc.net

沒有留言:

張貼留言