SOLVED Programmatically change menu shortcuts



  • Although there is a way to change shortcuts using a script, they don't update immediately. For example, the following code changes a glyph view shortcut instantly and I don't need to restart RF:

    from mojo.UI import exportPreferences, importPreferences
    
    pref = exportPreferences()
    pref['glyphViewQuickPreviewKey'] = ' '
    importPreferences(pref)
    

    But although the below code also works (it's just an example), it only takes effect after restarting the app. Is there any way to make the changes to menuShortCuts instantly reflected in the UI?

    from mojo.UI import exportPreferences, importPreferences
    
    pref = exportPreferences()
    keys = {"File > Save": [1048576, 'B']}
    pref['menuShortCuts'] = keys
    importPreferences(pref)
    


  • mojo.UI includes some additional functions for dealing with shortcut keys. (these are currently not listed in the docs, looking into it)

    use this API to modify the menuShortCuts preference AND update the application menu.

    from mojo.UI import getMenuShortCuts, setMenuShortCuts
    
    # get menu shortcuts
    print(getMenuShortCuts().keys())
    print()
    
    # set menu shortcuts
    shortcuts = {
        ('File', 'Save') : (1048576, 'b'),
        ('RoboFont', 'Check for Updates') : (1048576, "u"),
        ('RoboFont', 'License') : (1048576, "l"),
    }
    setMenuShortCuts(shortcuts)
    print(getMenuShortCuts().keys())
    


  • Alright, I will consider this for my extension until it gets fixed. These are awaited features for me and very much appreciated.



  • ps. there’a also removeMenuShortCut, for ex:

    removeMenuShortCut(('RoboFont > Check for Updates'))
    

    …but it is not removing the shortcut from the menu. (I’ve filed a bug report)

    in the meantime, you can clear a shortcut by setting it to an empty string.



  • Gustavo that's very nice! Yes, I also think they should be included in the Docs. Thank you again!



  • mojo.UI includes some additional functions for dealing with shortcut keys. (these are currently not listed in the docs, looking into it)

    use this API to modify the menuShortCuts preference AND update the application menu.

    from mojo.UI import getMenuShortCuts, setMenuShortCuts
    
    # get menu shortcuts
    print(getMenuShortCuts().keys())
    print()
    
    # set menu shortcuts
    shortcuts = {
        ('File', 'Save') : (1048576, 'b'),
        ('RoboFont', 'Check for Updates') : (1048576, "u"),
        ('RoboFont', 'License') : (1048576, "l"),
    }
    setMenuShortCuts(shortcuts)
    print(getMenuShortCuts().keys())
    


  • hello @bahman,

    I haven’t yet found out how to make the main menu update. just a comment about the preferences API for now:

    you can read/write a single preference setting using getDefault and setDefault, and send a notification with preferencesChanged:

    from mojo.UI import setDefault, getDefault, preferencesChanged
    
    print('old settings:')
    print(getDefault("glyphViewGridx"), getDefault("glyphViewGridy"))
    
    gridsize = 70
    setDefault("glyphViewGridx", gridsize)
    setDefault("glyphViewGridy", gridsize)
    preferencesChanged()
    
    print('new settings:')
    print(getDefault("glyphViewGridx"), getDefault("glyphViewGridy"))