Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Execute command Phase Listener for KeyBindings(how to execute a command via key sequences in RAP)
Execute command Phase Listener for KeyBindings [message #519563] Tue, 09 March 2010 10:39 Go to next message
Thomas Jodes is currently offline Thomas JodesFriend
Messages: 15
Registered: July 2009
Junior Member
Hi rap community,

I'd like to implement a mechanism that allows to execute a Command via a key sequence.

The following skeleton code base i have to implement this:

* Hook in "preStartup()" of a WorkbenchAdvisor to send initial jsCode for key events:

    public void preStartup() {
        final Display display = Display.getCurrent();
        RWT.getLifeCycle().addPhaseListener( new PhaseListener() {

            public void afterPhase( final PhaseEvent event ) {
                if( Display.getCurrent() == display ) {
                    String jsCode = "var doc = qx.ui.core.ClientDocument.getInstance();\n"
                            + "doc.addEventListener( \"keypress\", function( evt ) {\n"
                            + "  var req = org.eclipse.swt.Request.getInstance();\n"
                            + "  req.addParameter( \"myCommandParameter\", evt.getKeyIdentifier() );\n"
                            + "  req.send();\n" + "} );\n";
                    IServiceStateInfo stateInfo = ContextProvider.getStateInfo();
                    HtmlResponseWriter writer = stateInfo.getResponseWriter();
                    writer.append( jsCode );
                    RWT.getLifeCycle().removePhaseListener( this ); // only initial, remove yourself
                }
            }

            public PhaseId getPhaseId() {
                return PhaseId.RENDER;
            }
        });
        executeCommandListener = new ExecuteCommandPhaseListener(
                "your.compandy.myCommand", "myCommandParameter" );
        RWT.getLifeCycle().addPhaseListener( executeCommandListener );
    }


* Register an "ExecuteCommandPhaseListener", that reads the input keys send with org.eclipse.swt.Request (last lines above) and triggers the command using IHandlerService#executeCommand(java.langString)

---

- The Problem I face here, how can I react on meaningful key sequences? I.e. "CRTL+Q" or "CRTL+1"; with the proposal above I can only react on a single key press. Further more pressing CTRL, then "1" fires 'Control' only (with evt.getKeyIdentifier(), see above).

- Is there any possiblity to implement any meaningful keypress sequences like "CRTL+1" ?

Any help or hint is greatly appreciated, Thanks

Thomas

Re: Execute command Phase Listener for KeyBindings [message #519570 is a reply to message #519563] Tue, 09 March 2010 10:58 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

Hi Thomas,

you can filter certain key sequences by adding code like the following
to your Javascript event listener:

if( evt.isCtrlPressed && evt.getKeyIdentifier() == "1" ) {
var req = ...

For the KeyEvent API, see [1].

Hope this helps,
Ralf

[1] http://demo.qooxdoo.org/0.7.4/apiviewer/#qx.event.type.KeyEv ent

evt.getKeyIdentifier()
Thomas Jodes wrote:
> Hi rap community,
>
> I'd like to implement a mechanism that allows to execute a Command via a
> key sequence.
>
> The following skeleton code base i have to implement this:
>
> * Hook in "preStartup()" of a WorkbenchAdvisor to send initial jsCode
> for key events:
>
>
> public void preStartup() {
> final Display display = Display.getCurrent();
> RWT.getLifeCycle().addPhaseListener( new PhaseListener() {
>
> public void afterPhase( final PhaseEvent event ) {
> if( Display.getCurrent() == display ) {
> String jsCode = "var doc =
> qx.ui.core.ClientDocument.getInstance();\n"
> + "doc.addEventListener( \"keypress\",
> function( evt ) {\n"
> + " var req =
> org.eclipse.swt.Request.getInstance();\n"
> + " req.addParameter(
> \"myCommandParameter\", evt.getKeyIdentifier() );\n"
> + " req.send();\n" + "} );\n";
> IServiceStateInfo stateInfo =
> ContextProvider.getStateInfo();
> HtmlResponseWriter writer =
> stateInfo.getResponseWriter();
> writer.append( jsCode );
> RWT.getLifeCycle().removePhaseListener( this ); //
> only initial, remove yourself
> }
> }
>
> public PhaseId getPhaseId() {
> return PhaseId.RENDER;
> }
> });
> executeCommandListener = new ExecuteCommandPhaseListener(
> "your.compandy.myCommand", "myCommandParameter" );
> RWT.getLifeCycle().addPhaseListener( executeCommandListener );
> }
>
>
> * Register an "ExecuteCommandPhaseListener", that reads the input keys
> send with org.eclipse.swt.Request (last lines above) and triggers the
> command using IHandlerService#executeCommand(java.langString)
>
> ---
>
> - The Problem I face here, how can I react on meaningful key sequences?
> I.e. "CRTL+Q" or "CRTL+1"; with the proposal above I can only react on a
> single key press. Further more pressing CTRL, then "1" fires 'Control'
> only (with evt.getKeyIdentifier(), see above).
>
> - Is there any possiblity to implement any meaningful keypress sequences
> like "CRTL+1" ?
>
> Any help or hint is greatly appreciated, Thanks
>
> Thomas
>
>
Re: Execute command Phase Listener for KeyBindings [message #519596 is a reply to message #519563] Tue, 09 March 2010 12:11 Go to previous messageGo to next message
Thomas Jodes is currently offline Thomas JodesFriend
Messages: 15
Registered: July 2009
Junior Member
Hi Ralf!

I missed to press the inherit - button in that qooxdoo api viewer website!

Thanks a lot - because KeyEvent inherits that isCtrlPressed() - method from qx.event.type.DomEvent

I am blushing Embarrassed

What I added in that jsCode is:

+ " evt.stopPropagation(evt);"
+ " evt.preventDefault(evt);"

to avoid gecko browsers for showing the first tab (key is also CTRL+1) - if multiple tabs are open in current browser;

An "evt.keyCode = 505; return false;" should do it in M$ - IE.
(*edit* and it truely does; fork via qx.core.Client#isMshtml() ... )

Again, thanks and best wishes to Karlsruhe

[Updated on: Tue, 09 March 2010 12:32]

Report message to a moderator

Re: Execute command Phase Listener for KeyBindings [message #519604 is a reply to message #519563] Tue, 09 March 2010 12:45 Go to previous messageGo to next message
Thomas Jodes is currently offline Thomas JodesFriend
Messages: 15
Registered: July 2009
Junior Member
Ups, sorry to write back; one fallback Ralf:

evt.isCtrlPressed() && evt.getKeyIdentifier() == "1"

is not usable because evt is already fired solely from the ctrl key...
even if crtl is pressed, holded and then "1" pressed, the press for "1" is ignored ...

---

*edit*: It's a typical mshtml story:

- In gecko, everything works fine: the combination of CTRL plus 1 results in evt.isCtrlPressed() == true and evt.getKeyIdentifier() == "1".

- In mshtml, any additional key is ignored, as far as the user presses CTRL. Thus, we have evt.isCtrlPressed == true and evt.getKeyIdentifer() == 'Control'

That is, you cannot react on key combinations, i.e. CTRL+1 of mshtml

[Updated on: Tue, 09 March 2010 13:15]

Report message to a moderator

Re: Execute command Phase Listener for KeyBindings [message #519625 is a reply to message #519604] Tue, 09 March 2010 14:20 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

Thomas,

this works as expected for me. Sure you added the listener to
"keypress", not "keydown" or "keyup"?

If you have Firebug installed (which you should when you're coding
Javascript), you can paste the following code into your Firebug console
on a qooxdoo site (you can try it on the qooxdoo 0.7.4 API site):

var doc = qx.ui.core.ClientDocument.getInstance();
doc.addEventListener( "keypress", function( evt ) { console.log( evt )
} );

Focus some widget inside the browser window (to get the focus out of
Firebug) and hit some key combinations. You will see that the event is
only being fired when hitting Ctrl AND another key.

Good luck, Ralf

Thomas Jodes wrote:
> Ups, sorry to write back; one fallback Ralf:
>
> evt.isCtrlPressed() && evt.getKeyIdentifier() == "1"
>
> is not usable because evt is already fired solely from the ctrl key...
> even if crtl is pressed, holded and then "1" pressed, the press for "1"
> is ignored ...
Re: Execute command Phase Listener for KeyBindings [message #519638 is a reply to message #519625] Tue, 09 March 2010 14:57 Go to previous messageGo to next message
Thomas Jodes is currently offline Thomas JodesFriend
Messages: 15
Registered: July 2009
Junior Member
Ralf,

thx for looking into this.

Yes, all right in "our loved Mozilla (with firebug)".

I've edited the last post again and meant the Internet Explorer (8).
Please have a look to the second half of my edited post...

Greetings, Thomas

[Updated on: Tue, 09 March 2010 14:58]

Report message to a moderator

Re: Execute command Phase Listener for KeyBindings [message #519746 is a reply to message #519638] Tue, 09 March 2010 20:00 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

Hi Thomas,

I can confirm the problem you describe. I'm currently not sure whether
this is a bug in qooxdoo or a problem with RAP. It even seems to differ
between IE7 and IE8. As far as I can see, the KeyListeners are not
affected by this issue, so it's not directly a bug. We're going to look
into this anyway, but won't have the time to do so before next week -
sorry for that.

Best regards, Ralf

Thomas Jodes wrote:
> Ralf,
>
> thx for looking into this.
>
> Yes, all right in "our loved Mozilla (with firebug)".
>
> I've edited the last post again and meant the Internet Explorer (8).
> Please have a look to the second half of my edited post...
>
> Greetings, Thomas
Re: Execute command Phase Listener for KeyBindings [message #520784 is a reply to message #519604] Mon, 15 March 2010 10:30 Go to previous message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

Hi Thomas,

it seems that the problem is the use of the "keypress" event instead of
"keyup" or "keydown". In IE, if you hold down a key, you get one
keydown, many keypresses and one keyup. The following code seems to work
as expected in IE (tested IE7 and IE8):

doc.addEventListener( "keydown", function( evt ) {
this.debug( "Ctrl: " + evt.isCtrlPressed() + ", Identifier: " +
evt.getKeyIdentifier() );
if( evt.isCtrlPressed() && evt.getKeyIdentifier() == "K" ) {
this.debug( "Ctrl+K detected" );
}
} );

Could you give it another try?

Best regards, Ralf

Thomas Jodes wrote:
> Ups, sorry to write back; one fallback Ralf:
>
> evt.isCtrlPressed() && evt.getKeyIdentifier() == "1"
>
> is not usable because evt is already fired solely from the ctrl key...
> even if crtl is pressed, holded and then "1" pressed, the press for "1"
> is ignored ...
Previous Topic:Broken image in RAP 1.3.0M5
Next Topic:Icons initially not visible in perspective
Goto Forum:
  


Current Time: Sat Oct 05 16:00:43 GMT 2024

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

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

Back to the top