Assigning hotkeys to DigitalMicrograph functions and scripts?

117 views Asked by At

In GMS2, there was an area to easily assign hotkeys to certain built-in functions (eg. Save As..., Rotate). It seems that was removed in GMS3. Is it possible to script a hotkey for those items and any installed scripts?

1

There are 1 answers

0
BmyGuest On

I think you want to look at this section of the F1 help documentation for detail: F1 help


Behavior may depend on the GMS version you are using. I found a script that states that it works for GMS 1, GMS 2 and from GMS 3.4 and onward, but not for GMS 3.0 to GMS 3.3


Pre-installed menu items (such as File/Save as...) cannot get accelerator keys assigned or changed. They are provided by the system.


In general, having too many accelerators or hot-key is dangerous, as there is no global management and one can easily over-use combinations. Also, some in-build functionality uses hot-keys while certain images are front-most etc. Proceed with care.


The example below installs a custom menu with a key accelerator.

// //////////////////////////////////////////////////// //
// Example of installing a menu with a key accelerator.
// //////////////////////////////////////////////////// //
Class CMyMenuAction
{
    CMyMenuAction(object self){result("\n Created object: "+ self.ScriptObjectGetClassName() + " - " + self.ScriptObjectGetID());}
    ~CMyMenuAction(object self){result("\n Killed object: "+ self.ScriptObjectGetClassName() + " - " +self.ScriptObjectGetID());}
    
    void Action( object self ) { OKDialog("Action!"); }
}

Class CMyMenuInstaller
{
    CMyMenuInstaller(object self){result("\n Created object: "+ self.ScriptObjectGetClassName() + " - " + self.ScriptObjectGetID());}
    ~CMyMenuInstaller(object self){result("\n Killed object: "+ self.ScriptObjectGetClassName() + " - " +self.ScriptObjectGetID());}


    object UnInstallMenu( object self , string menuName){
    
        object menuBar = GetMenuBar()
        object customMenu = menuBar.FindMenuItemByName(menuName)
        if ( customMenu.ScriptObjectIsValid() )
        {
            customMenu.ClearMenuItems()
            menuBar.RemoveMenuItem( customMenu )
        }
        return self
    }
    
    object InstallMenu( object self, string menuName ){
    
        object menuBar = GetMenuBar()
        
        // Find or add test-menu
        object customMenu = menuBar.FindMenuItemByName(menuName)
        if ( !customMenu.ScriptObjectIsValid() )    {
            customMenu  = NewMenu( menuName )
            menuBar.AddMenu( customMenu )
        }
        
        // Create command
        object menuActions = Alloc(CMyMenuAction)
        
        object item1 = alloc( menuItem ).init( "Do something" )
        item1.SetAction( menuActions, "Action" )
        item1.SetIsEnabled(1)
        item1.SetAccelerator( MakeKeyStroke( "alt-shift-x" ) )
    
        // Building the menu
        customMenu.ClearMenuItems()         // Reset/Clear existing items
        customMenu.AddMenuItem( item1 )
        
        
        return self
    }
}

ClearResults()
Alloc(CMyMenuInstaller).InstallMenu("Test Menu")
//Alloc(CMyMenuInstaller).UnInstallMenu("Test Menu")```


----------
You can also use *global key accelerators* to tie script functionality to a key-stroke.

```c
// NB: This works only in GMS 1 & GMS 2 and from GSM 3.4 onwards
//      Since GMS 3 the accelerator can no longer be replaced and will
//      remain registered until application shutdown. 
//      Alternatively, one can use keyboard-accellerators

class Custom_Action{
    void DoAction( Object self ){Beep();Result("beep.");}
};

void do(){
object handler = alloc( Custom_Action )
object action = NewMenuAction( handler, "DoAction" )
object accelerator = NewMenuAccelerator( MakeKeyStroke( "f7" ), action )
accelerator.SetIsEnabled(1)
object menuBar = GetMenuBar()
menuBar.AddAccelerator( accelerator )
}
do()