Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 11:53 Go to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
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 08:18 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
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 15:17 Go to previous messageGo to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
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 15:37 Go to previous messageGo to next message
David  Pérez is currently offline David PérezFriend
Messages: 228
Registered: July 2009
Senior Member
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 04:55 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
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 14:18 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

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: Wed Apr 24 21:31:09 GMT 2024

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

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

Back to the top