Features

Download

Online

Gallery

Blog

  Index  | Recent Threads  | List Attachments  | Help  | Search
 Welcome Guest  |  Register  |  Login
Login Name  Password
 

Sweet Home 3D Forum



No member browsing this thread
Thread Status: Active
Total posts in this thread: 3
[ Jump to Last Post ]
Post new Thread
Author
Previous Thread This topic has been viewed 5305 times and has 2 replies Next Thread
jaffguo
Newbie




Joined: Sep 30, 2014
Post Count: 3
Status: Offline
Reply to this Post  Reply with Quote 
How to add a shortcut key to my plugin menu item?

Hi,

I'm wondering if there would be a way to add a shortcut key to my customized plugin menu item.

thanks,

jaff
[Oct 28, 2016 10:06:01 PM] Show Printable Version of Post    View Member Profile    Send Private Message    Hidden to Guest [Link] Report threatening or abusive post: please login first  Go to top 
Puybaret
Expert
Member's Avatar

France
Joined: Nov 7, 2005
Post Count: 5837
Status: Offline
Reply to this Post  Reply with Quote 
Re: How to add a shortcut key to my plugin menu item?

You can add shortcut by binding an instance of KeyStroke with an action that will execute your plugin. The good place to program this should be in the method getActions of your plug-in :

public PluginAction [] getActions() {
final PluginAction pluginAction = new PluginActionClass(...);

// Create shortcut once Sweet Home 3D window is created
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// KeyStroke for shortcut Ctrl+Alt+Shift+C (cmd+alt+shift+C under Mac OS X)
KeyStroke cmdOrCtrlAltShiftC = KeyStroke.getKeyStroke(KeyEvent.VK_C,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
| KeyEvent.ALT_DOWN_MASK
| KeyEvent.SHIFT_DOWN_MASK);
// Bind shortcut with the key "MY_KEY"
JComponent homePane = (JComponent)getHomeController().getView();
String actionKey = "MY_KEY";
homePane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(cmdOrCtrlAltShiftC, actionKey);
// Bind the key "MY_KEY" with an action that will execute the plug-in
homePane.getActionMap().put(actionKey, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
pluginAction.execute();
}
});

}
});

return new PluginAction [] {pluginAction};
}


If you want that the shortcut appears beside the menu item of the plug-in, you'll have to find the menu item in the menu bar returned by
((JFrame)SwingUtilities.getWindowAncestor((JComponent)getHomeController().getView())).
getJMenuBar();
with getMenu then getMenuComponent methods. Once you found the right menu item, just call its setAccelerator method.
----------------------------------------
Emmanuel Puybaret, Sweet Home 3D developer
[Nov 2, 2016 3:33:35 PM] Show Printable Version of Post    View Member Profile    Send Private Message    Hidden to Guest    http://www.eteks.com [Link] Report threatening or abusive post: please login first  Go to top 
jaffguo
Newbie




Joined: Sep 30, 2014
Post Count: 3
Status: Offline
Reply to this Post  Reply with Quote 
smile Re: How to add a shortcut key to my plugin menu item?

I very much appreciate your help, Ennanuel. It works well. Thanks a lot.
Jaff
[Nov 3, 2016 12:20:12 PM] Show Printable Version of Post    View Member Profile    Send Private Message    Hidden to Guest [Link] Report threatening or abusive post: please login first  Go to top 
[ Jump to Last Post ]
Show Printable Version of Thread  Post new Thread

    Get Sweet Home 3D at SourceForge.net. Fast, secure and Free Open Source software downloads
   
© Copyright 2006-2017 eTeks - All rights reserved