Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » From a command handler how can I know if it is invoked?
From a command handler how can I know if it is invoked? [message #660927] Tue, 22 March 2011 07:53 Go to next message
Eclipse UserFriend
I need to know from a command handler if it is invoked from the keyboard or other means (menu item, mouse, ....).

Why?

Because if it is invoked from the keyboard, which control has the focus matters.

Example:

I have a view with a table and a text field for searching inside the table.

Suppose that Ctrl+C is the shorcut for the command that copies the contents of the current row in the table to the clipboard.

If I select this command from a toolbar, focus doesn't matter.
But if I press Ctrl+C, I would like the behaviour to be adjusted:
- If the text field has the focus
- Or some other control like the table has the focus

I've checked HandlerUtil but haven't seen anything useful.

Any idea?

David
Re: From a command handler how can I know if it is invoked? [message #661103 is a reply to message #660927] Wed, 23 March 2011 04:18 Go to previous messageGo to next message
Eclipse UserFriend
On 22/03/11 5:23 PM, David Pérez wrote:
> I need to know from a command handler if it is invoked from the keyboard
> or other means (menu item, mouse, ....).

The ExecutionEvent passed to the handler has the getTrigger() method.
You can examine it to get the details you want.

--
- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: From a command handler how can I know if it is invoked? [message #661206 is a reply to message #661103] Wed, 23 March 2011 11:17 Go to previous messageGo to next message
Eclipse UserFriend
Thanks Prakash.
Commands are powerful.
Without your help, it would have been hard to discover this by myself, as it isn't very documented.

David

Prakash G.R. wrote on Wed, 23 March 2011 04:18
On 22/03/11 5:23 PM, David Pérez wrote:
> I need to know from a command handler if it is invoked from the keyboard
> or other means (menu item, mouse, ....).

The ExecutionEvent passed to the handler has the getTrigger() method.
You can examine it to get the details you want.

--
- Prakash
Platform UI Team, IBM

www.eclipse-tips.com

Re: From a command handler how can I know if it is invoked? [message #661424 is a reply to message #661103] Thu, 24 March 2011 11:37 Go to previous messageGo to next message
Eclipse UserFriend
Hi Prakash,

I would consider adding this method in HanderUtil:

    public static boolean isFromKeyboard(ExecutionEve nt e) {
        Object trig = e.getTrigger();
        if (trig instanceof Event) {
            Event ev = (Event) trig;
            return ev.type == SWT.KeyDown ||  ev.type == SWT.KeyUp || ev.type == SWT.HardKeyDown  || ev.type == SWT.HardKeyUp;
        }
        return false;
    }

What do you think?

BTW, your tips from your blog are really very valuable. Razz

Prakash G.R. wrote on Wed, 23 March 2011 04:18
On 22/03/11 5:23 PM, David Pérez wrote:
> I need to know from a command handler if it is invoked from the keyboard
> or other means (menu item, mouse, ....).

The ExecutionEvent passed to the handler has the getTrigger() method.
You can examine it to get the details you want.


Re: From a command handler how can I know if it is invoked? [message #661521 is a reply to message #661424] Fri, 25 March 2011 00:55 Go to previous messageGo to next message
Eclipse UserFriend
On 24/03/11 9:07 PM, David Pérez wrote:
> Hi Prakash,
>
> I would consider adding this method in HanderUtil:
>
> public static boolean isFromKeyboard(ExecutionEve nt e) {
> Object trig = e.getTrigger();
> if (trig instanceof Event) {
> Event ev = (Event) trig;
> return ev.type == SWT.KeyDown ||
> ev.type == SWT.KeyUp || ev.type == SWT.HardKeyDown
> || ev.type == SWT.HardKeyUp;
> }
> return false;
> }
>
> What do you think?

Please raise a bug. We can discuss there: http://bit.ly/NewBugInPlatformUI

--
- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: From a command handler how can I know if it is invoked? [message #661923 is a reply to message #661424] Mon, 28 March 2011 10:18 Go to previous message
Eclipse UserFriend
If you have a view and your handling of a command (like copy) changes
depending on which control has focus, use the IFocusService. That
allows you to specify a different handler when your text widget has
focus (as opposed to using your view's normal handler when anything else
has focus, for example).

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm
Previous Topic:CommonNavigator with resources + own navigatorContent
Next Topic:How to handle new project location
Goto Forum:
  


Current Time: Mon Jul 07 23:26:47 EDT 2025

Powered by FUDForum. Page generated in 0.04510 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top