Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to capture key press event over multiple views
How to capture key press event over multiple views [message #676503] Sat, 04 June 2011 22:32 Go to next message
Chaitannya  is currently offline Chaitannya Friend
Messages: 14
Registered: May 2010
Junior Member
Hello Everyone,

I am having an application where I have to detect key press event over multiple views.
There are 3 views in a perspective and out of that there is a main view. The main view has to be assigned certain short cut keys. Example there is a button named "Pain" in the main view and when the user presses "P" the event should trigger the pain functionality. In this case the user's focus might be on any of the views, i.e. the user might be scrolling through the details in view no 2 and he presses "P", appropriate action should trigger.

Regards,
Chaitannya
Re: How to capture key press event over multiple views [message #676774 is a reply to message #676503] Mon, 06 June 2011 13:47 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You can create a command with a handler, and then assign them to key shortcuts using the org.eclipse.ui.contexts.window context.

In your handler, you will be able to get the active workbench window from org.eclipse.ui.handlers.HandlerUtil.getActiveWorkbenchWindow(ExecutionEvent) and then find the main view and tell it to execute your functionality. For more information on commands in general, see the links in my sig.

--
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


Re: How to capture key press event over multiple views [message #676802 is a reply to message #676774] Mon, 06 June 2011 15:05 Go to previous messageGo to next message
Michael Gerzabek is currently offline Michael GerzabekFriend
Messages: 16
Registered: June 2011
Junior Member
I have a similar problem (and am also new to RCP). I followed the steps and declared
- several keybindings,
- several commands
- one handler. the handler is the default handler for all commands. i do discriminiation via the command name.
I also declared a context and activate this context in createPartControl( Composite parent ) of my view. [1] and [2] have been helpful resources on the web for me in the process of figuring out how to do this. (I have to post more than 5 messages until I'm allowed to include links. So the protocol is missing.)

Now. When I define keybindings as a combination of keys, i.e. Alt+B, everything works fine. But when I define keybindings just via ordinary letters, i.e. B, my handler doesn't get called. Is there a way to consume just letters without modifier keys? What do I miss here?

Regards,
Michael

PS: I worked 10 years on the server side doing webapps and enterprise integration. I'm very used to Eclipse, especially the JDT, WST, etc. This is my first dive into Eclipse as a plattform for applications. And, man, THIS IS AN AWESOME PLATFORM! I'm so impressed! Thank you very much all out there!!!

[1] www.vogella.de/articles/EclipseCommandsKeybindings/article.html
[2] beautifuldiscovery.wordpress.com/2010/04/26/eclipse-tip-using-icontextactivation-in-a-viewpart-or-editorpart/
Re: How to capture key press event over multiple views [message #676814 is a reply to message #676802] Mon, 06 June 2011 15:45 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Ah, and sorry this applies to the original question as well.

First, keybindings only works with a modifier. Eclipse by default doesn't provide a way to eat the normal, none modified keys. That's because it breaks UI guidelines. Once you eat, say, the B key, it's gone for good.

That being said you can do this per view by adding a SWT.KeyDown listener to the controls in your view. This helps limit your change to only your view. Once you get your key, you can act on it, even using org.eclipse.ui.handlers.IHandlerService.executeCommand(String, Event) to execute a command (which is what the standard keybinding framework is doing anyway).

In the case of the original question, you can add your keydown listener to all 3 views, and if you get the key you want use something like:

MyMainView part = (MyMainView) getSite().getPage().findView(MyMainView.ID);
part.doPain();


In this case it's OK to use findView(String) because you're working with your main view. In the general case, you would use findViewReference(String).

Later,
PW


Re: How to capture key press event over multiple views [message #676871 is a reply to message #676814] Mon, 06 June 2011 19:42 Go to previous messageGo to next message
Michael Gerzabek is currently offline Michael GerzabekFriend
Messages: 16
Registered: June 2011
Junior Member
Thank you Paul for the quick response.

Guidelines 7.2 says "Use a view to navigate a hierarchy of information, open an editor, or display the properties of an object." (wiki.eclipse.org/User_Interface_Guidelines#Views)

The use-case. I'm working on a view that displays a video. I want to control playback totally by keys. the simpler the better - that's the reason why I would prefer B to Ctrl+B. One Certain key stops playback and immediately opens an editor window to enter text. the text together with a timestamp is stored.

Is a view the right choice here?

Anyway. I did try this
@Override
	public void createPartControl( Composite parent ) {
   
	        parent.setLayout( new FillLayout() );		
                videoPanel = new Composite( parent, SWT.NONE );
		videoPanel.setLayout( new FillLayout() );

		videoComposite = new Composite( videoPanel, SWT.EMBEDDED | SWT.NO_BACKGROUND );
		videoComposite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
		videoFrame = SWT_AWT.new_Frame( videoComposite );
		videoSurfaceCanvas = new Canvas();
		videoSurfaceCanvas.setBackground( java.awt.Color.black );
		videoFrame.add( videoSurfaceCanvas );

		mediaPlayerFactory = new MediaPlayerFactory();
		mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
		mediaPlayer.setVideoSurface( mediaPlayerFactory.newVideoSurface( videoSurfaceCanvas ) );
		mediaPlayer.setRepeat( true );
		

		parent.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent keyEvent) {

				System.out.println(keyEvent.keyCode);
			}
		});

		
		videoPanel.setVisible( true );
		mediaPlayer.playMedia( "./test.mp4" );
	}


When I started the plugin the first time on a new runtime workspace, everything went as supposed. The event got fired and on console i could see the key that i pressed. But when I started the application again, nothing happened. the console keeps empty.

What am I missing? Where do I leave the right track?

Thank's for your help!
Regards,
Michael
FIXED Re: How to capture key press event over multiple views [message #676946 is a reply to message #676871] Tue, 07 June 2011 04:56 Go to previous messageGo to next message
Michael Gerzabek is currently offline Michael GerzabekFriend
Messages: 16
Registered: June 2011
Junior Member
I found the problem. Instead of parent.addKeyListener I need to videoPanel.addKeyListener. Then everything works like a charm.

Now I lose the ability to let the user define key bindings. Is there a way to read all defined key bindings for a particular schema? If I could get my hands on them I could evaluate an emulate them within my custom handler.

Regards,
Michael
Re: FIXED Re: How to capture key press event over multiple views [message #677050 is a reply to message #676946] Tue, 07 June 2011 12:15 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Within an RCP app, there are a couple of options.

If you want to override the default keybinding handling completely, you would disable framework keybindings:

IBindingService srv = (IBindingService) PlatformUI.getWorkbench().getService(IBindingService.class);
srv.setKeyFilterEnabled(false);


And then add an SWT.KeyDown filter to Display. See org.eclipse.ui.internal.keys.WorkbenchKeyboard, it analyses key sequences and asks the IBindingService for matches ... and then uses the IHandlerService to execute those matches.

But ... look at WorkbenchKeyboard Smile There's some complex logic in there to try and get it correct Smile

In theory the keybinding framework gets first crack at the keys, long before your SWT widgets do. So keybindings should still work, and you can catch your B or P key from your video panel.

Does that meet your requirements?

PW


[Updated on: Tue, 07 June 2011 12:15]

Report message to a moderator

Re: FIXED Re: How to capture key press event over multiple views [message #681947 is a reply to message #677050] Fri, 10 June 2011 06:02 Go to previous messageGo to next message
Michael Gerzabek is currently offline Michael GerzabekFriend
Messages: 16
Registered: June 2011
Junior Member
Thank you Paul for the valuable insights. I'm very confident now using the standard stuff, since declaration via plugin.xml and the possibility for users to override factory supplied key-bindings seems to me a must have.

On the other front I got the problem that on one computer the SWT keys get delivered to my KeyAdapter on another computer they don't. Both machines run Eclipse 3.6.2 on winXP. I have no clue where to look. Any ideas?

Regards,
Michael
Re: FIXED Re: How to capture key press event over multiple views [message #682127 is a reply to message #681947] Fri, 10 June 2011 13:24 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Try providing a debug.txt file to eclipse when starting up, with keybinding tracing turned on. Then you can see what the Filter sees on both machines.

See http://wiki.eclipse.org/Platform_Command_Framework#Tracing_Option

PW


Previous Topic:EditorPart MenuContribution
Next Topic:how to add the external jar to the rcp application
Goto Forum:
  


Current Time: Thu Apr 18 04:34:34 GMT 2024

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

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

Back to the top