Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

'Global' shortcut keys, for Page possible?

I am trying to give the customer some 'global' shortcut keys that they can use to quickly escape or complete an input page.  These are key combinations that would not normally be valid in the customer's input.  I have tried a number of combinations, the latest being

 

Shift + Del - close the page

Shift + Enter - process the page

 

Here is some QML like the QML I am using:

 

    shortcuts: [
        Shortcut {
            key: "Shift+Enter"
            onTriggered: {
                console.log("shift-enter pressed");
                if (_app.send(true)) {
                    _navigationPane.pop();
                }
            }
        },
        Shortcut {
            key: "Shift+Delete"
            onTriggered: {
                console.log("shift-del pressed");
                _navigationPane.pop();
            }
        }
    ]

 

The idea is simply to allow the user to type at the keyboard and then cancel or complete the page without leaving the keyboard.

 

Unfortunately this only works where the control the user is focused on does not process this combination.  So for things like check boxes, the one definition at the page level does the job,  But for input Fields, it seems I have to code the definitions for each Field, which I would prefer not to do.

 

I think what I am seeing, is working as designed, so I guess my questions are:

 

a) Does anyone have a better way of achieving this?

b) Does anyone have a way of coding this 'once' in my QML and using it everywhere?

 

I have a number of pages like this, each with separate QML, and each containing a number of custom Fields.  I don't mind having this replicated on every page, but replicated on every Field on every page is a bit much. 

 

BTW, I am not great at QML, if there is a simple thing that you are surprised I have not thought of, please suggest it, I probably haven't thought of it! 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: 'Global' shortcut keys, for Page possible?

That was a bit heavyweight for my poor brain. Can I get a clarification of what you want?

It appears you're basically saying you've got it working for the basic functionality, but default handling of those particular shortcuts on certain controls such as TextField is intercepting the events before they ever reach your handlers. Is that correct?

What you're basically in need of appears to be a way of intercepting those events before they're ever passed down to the controls in question, so you can filter those out and divert them to your own handlers.

The event model still seems to follow the usual "capturing" and "bubbling" phases (see https://developer.blackberry.com/cascades/reference/bb__cascades__propagationphase.html#enum-type), so it occurs to me that you might stick in something to handle key events during "capturing" instead. If that's possible, I think it would need C++ code. My thoughts end there.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: 'Global' shortcut keys, for Page possible?

More on the event handling, though since this is under "Touch propagation" it suggests it may not apply to the key events: https://developer.blackberry.com/cascades/documentation/dev/touch/touch_propagation.html

Those with a better knowledge of Qt event handling may be of more help here.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
DrShavargo
Posts: 133
Registered: ‎05-10-2013
My Device: Blackberry Z10

Re: 'Global' shortcut keys, for Page possible?

Place the shortcut on the highest element in the QML file, like so:

 

Page{
TextArea{
text: ""
}
shortcuts: [
        Shortcut {
            key: "Shift+Enter"
            onTriggered: {
                console.log("shift-enter pressed");
                if (_app.send(true)) {
                    _navigationPane.pop();
                }
            }
        },
        Shortcut {
            key: "Shift+Delete"
            onTriggered: {
                console.log("shift-del pressed");
                _navigationPane.pop();
            }
        }
    ]
}

 The application should hear the call to the shortcut, no matter what field you're working with.

----------------------------------------
Remember to mark the thread as solved at the post that solved your problem, and if you like a post, like it!
Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: 'Global' shortcut keys, for Page possible?

 @peter9477, sorry didn't mean to be heavyweight...

 

I think my response to DrShavargo will clarify the problem.

 

@DrShavargo

 

You solution is exactly what I have tried and it does not work for me.  I have expanded the QML a little below.  In the example provided, if focus is on the checkbox, the Shift and Enter works.  When it is on the Text Area, it doesn't, it just creates a new line.  I would like SHift + Enter to always provide the send.  I can do that by placing the same short cut keys in the TextArea, but then I need it globally for the page, plus in every Control that will process them, which is not ideal.

 

Hopefully that is clearer? 

 

Page{
CheckBox {
 label: "Test"
 onCheckedChanged: {
 }
}
TextArea{
text: ""
}
shortcuts: [
        Shortcut {
            key: "Shift+Enter"
            onTriggered: {
                console.log("shift-enter pressed");
                if (_app.send(true)) {
                    _navigationPane.pop();
                }
            }
        },
        Shortcut {
            key: "Shift+Delete"
            onTriggered: {
                console.log("shift-del pressed");
                _navigationPane.pop();
            }
        }
    ]
}

 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: 'Global' shortcut keys, for Page possible?

peter_strange, so it seems to be as I interpreted it, which means the rest of my comment stands. :smileyhappy:

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: 'Global' shortcut keys, for Page possible?

"it seems to be as I interpreted it,"  It is indeed.  The issue appears to be that shortcut events are always bubbled, at least that is what the documentation suggests.

 

I would rather not have something in C++ processing this since the pages are mainly coded in QML as is.  In fact I would prefer to have the shortcut coding replicated than have to do this in C++.  So I hope that someone can help with a QML solution. 

 

 

Please use plain text.
Developer
jalue
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10

Re: 'Global' shortcut keys, for Page possible?

It's unfortunate that Key Events are not handled the same way as Touch Events pertaining to Event Propagation...

 

According to this it goes from the focused Control outwards to the root...

https://developer.blackberry.com/cascades/documentation/dev/shortcuts/

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: 'Global' shortcut keys, for Page possible?

peter_strange, sounds like the next best thing would be to use your own custom control based on TextField, adding your own shortcut definitions to override the defaults. Same on any other controls where this is needed, and then replace all relevant instances of TextField with your custom class. That saves you having to specify this on each instance, at the cost of having to build the custom control complete with all required alias properties etc.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: 'Global' shortcut keys, for Page possible?

Looks that way to me too.  Still too many places to change for me to think this is the ideal solution...  :smileyfrustrated:

 

If this was in C I could probably use a macro to put the same code in everywhere (not that I really know how to use macros), but I don't think there is the equivalent functionality in QML.  I will leave this for a bit longer and cross my fingers....

Please use plain text.