Show
Ignore:
Timestamp:
07/08/08 12:20:56 (5 months ago)
Author:
rob
Message:

Fixed #480: added DescribeKey? action
* modified keystroke processing to send an action to a callback rather than processing the action
* updated the keystroke definition to use the built-in keystrokes from wx itself rather than a hard-coded list here in the source
* changed some default key bindings to C-h for Help

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/peppy/lib/wxemacskeybindings.py

    r1433 r1471  
    2121import wx 
    2222 
    23 wxkeynames = ( 
    24     "BACK", "TAB", "RETURN", "ESCAPE", "SPACE", "DELETE", "START", 
    25     "LBUTTON", "RBUTTON", "CANCEL", "MBUTTON", "CLEAR", "PAUSE", 
    26     "CAPITAL", "PRIOR", "NEXT", "END", "HOME", "LEFT", "UP", "RIGHT", 
    27     "DOWN", "SELECT", "PRINT", "EXECUTE", "SNAPSHOT", "INSERT", "HELP", 
    28     "NUMPAD0", "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD4", "NUMPAD5", 
    29     "NUMPAD6", "NUMPAD7", "NUMPAD8", "NUMPAD9", "MULTIPLY", "ADD", 
    30     "SEPARATOR", "SUBTRACT", "DECIMAL", "DIVIDE", "F1", "F2", "F3", "F4", 
    31     "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", 
    32     "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", 
    33     "NUMLOCK", "SCROLL", "PAGEUP", "PAGEDOWN", "NUMPAD_SPACE", 
    34     "NUMPAD_TAB", "NUMPAD_ENTER", "NUMPAD_F1", "NUMPAD_F2", "NUMPAD_F3", 
    35     "NUMPAD_F4", "NUMPAD_HOME", "NUMPAD_LEFT", "NUMPAD_UP", 
    36     "NUMPAD_RIGHT", "NUMPAD_DOWN", "NUMPAD_PRIOR", "NUMPAD_PAGEUP", 
    37     "NUMPAD_NEXT", "NUMPAD_PAGEDOWN", "NUMPAD_END", "NUMPAD_BEGIN", 
    38     "NUMPAD_INSERT", "NUMPAD_DELETE", "NUMPAD_EQUAL", "NUMPAD_MULTIPLY", 
    39     "NUMPAD_ADD", "NUMPAD_SEPARATOR", "NUMPAD_SUBTRACT", "NUMPAD_DECIMAL", 
    40     "NUMPAD_DIVIDE", 
    41     ) 
     23# The list of all the wx keynames (without the WXK_ prefix) is needed by both 
     24# KeyMap and KeyProcessor objects 
     25wxkeynames = [i[4:] for i in dir(wx) if i.startswith('WXK_')] 
     26 
    4227 
    4328class DuplicateKeyError(Exception): 
     
    333318    """ 
    334319    debug = False 
    335      
     320 
     321    # Mapping of wx keystroke numbers to keystroke names 
     322    wxkeys = {} 
     323    for i in wxkeynames: 
     324        wxkeys[getattr(wx, "WXK_"+i)] = i 
     325    for i in ("SHIFT", "ALT", "COMMAND", "CONTROL", "MENU"): 
     326        if wx.Platform == '__WXGTK__': 
     327            # unix doesn't create a keystroke when a modifier key 
     328            # is also modified by another modifier key, so we 
     329            # create entries here so that decode() doesn't have to 
     330            # have platform-specific code 
     331            wxkeys[getattr(wx, "WXK_"+i)] = i[0:1]+'-' 
     332        else: 
     333            wxkeys[getattr(wx, "WXK_"+i)] = '' 
     334 
    336335    def __init__(self,status=None): 
    337336        self.keymaps=[] 
     
    367366        self.universalArgument="C-U" 
    368367        self.processingArgument=0 
     368         
     369        # If reportNext is not None, instead of being processed the next action 
     370        # is reported to a caller by using reportNext as a callback. 
     371        self.reportNext = None 
    369372 
    370373        self.hasshown=False 
    371374        self.reset() 
    372  
    373         # Mapping of wx keystroke numbers to keystroke names 
    374         self.wxkeys={} 
    375         # set up the wxkeys{} dict 
    376         self.wxkeymap() 
    377  
    378     def wxkeymap(self): 
    379         for i in wxkeynames: 
    380             self.wxkeys[getattr(wx, "WXK_"+i)] = i 
    381         for i in ("SHIFT", "ALT", "COMMAND", "MENU"): 
    382             if wx.Platform == '__WXGTK__': 
    383                 # unix doesn't create a keystroke when a modifier key 
    384                 # is also modified by another modifier key, so we 
    385                 # create entries here so that decode() doesn't have to 
    386                 # have platform-specific code 
    387                 self.wxkeys[getattr(wx, "WXK_"+i)] = i[0:1]+'-' 
    388             else: 
    389                 self.wxkeys[getattr(wx, "WXK_"+i)] = '' 
    390375 
    391376    def findStickyMeta(self): 
     
    486471                keyname = chr(keycode) 
    487472            else: 
    488                 keyname = "(%s)unknown" % keycode 
     473                keyname = "unknown-%s" % keycode 
    489474        if self.debug: print("modifiers: raw=%d processed='%s' keyname=%s keycode=%s key=%s" % (emods, modifiers, keyname, keycode, modifiers+keyname)) 
    490475        return modifiers + keyname 
     
    514499        """ 
    515500        if self.status: 
     501            if self.reportNext: 
     502                text = "Describe Key: %s" % text 
    516503            self.status.SetStatusText(text) 
    517504            self.hasshown=True 
     
    608595            self.args+=key + ' ' 
    609596            self.processingArgument+=1 
     597     
     598    def setReportNext(self, callback): 
     599        self.reportNext = callback 
     600        self.show('') 
    610601 
    611602    def process(self, evt): 
     
    635626                skip, unknown, function = self.add(key) 
    636627            self.reset() 
    637             self.show("Quit") 
    638             if function: 
    639                 function(evt, printable=False) 
     628            if self.reportNext: 
     629                self.reportNext(function) 
     630                self.reportNext = None 
     631            else: 
     632                self.show("Quit") 
     633                if function: 
     634                    function(evt, printable=False) 
    640635        elif self.metaNext: 
    641636            # OK, the meta sticky key is down, but it's not a quit 
     
    679674                    printable = not self.modifier 
    680675                    self.reset() 
    681                     if save is not None: 
    682                         function(evt,save, printable=printable) 
     676                    if self.reportNext: 
     677                        self.reportNext(function) 
     678                        self.reportNext = None 
    683679                    else: 
    684                         function(evt, printable=printable) 
     680                        if save is not None: 
     681                            function(evt,save, printable=printable) 
     682                        else: 
     683                            function(evt, printable=printable) 
    685684                elif unknown: 
    686685                    # This is an unknown keystroke combo 
    687686                    sf = "%s not defined."%(self.sofar) 
    688687                    self.reset() 
     688                    if self.reportNext: 
     689                        self.reportNext(None) 
     690                        self.reportNext = None 
    689691                    self.show(sf) 
    690692                elif skip: 
     
    693695                    # to get processed elsewhere. 
    694696                    self.reset() 
    695                     evt.Skip() 
     697                    if self.reportNext: 
     698                        self.reportNext(None) 
     699                        self.reportNext = None 
     700                    else: 
     701                        evt.Skip() 
    696702                else: 
    697703                    self.show(self.args+self.sofar)