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

Web and WebWorks Development

Reply
Developer
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi
Accepted Solution

Simulating Keypress with jQuery or Javascript

Built a game for the Q10 now I'm bringing it to the Z10. I made the bottom of the Z10 screen a controller. 

 

I want to simulate keypress so the Q10 code will work exactly the same on the Z10. E.g. when someone presses the back button on screen on the Z10 it's as if the backbutton was actually pressed.

 

I've read this is possible on a computer and tried a bunch of solutions I found on StackOverFlow. None seem to work but maybe I'm implimenting them wrong.

 

Does anyone know if it's possible to do this?

 

Current code:

 

$('#z10_back').trigger(jQuery.Event('keypress', {which: 8}));

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi

Re: Simulating Keypress with jQuery or Javascript

Hey this is possible here is the Stackoverflow question that helped me figure it out.

http://stackoverflow.com/q/15722096/773263
_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: Simulating Keypress with jQuery or Javascript

Useful Q

 

What you're after is event trapping

 

Specifically you are after the keydown message

 

Look at script/vexed.js [sorry everyone - Phil has the source]

 

Search for preventDefault - those are my handlers for mouse and touch

 

You'll need onKeyPressed (look it up - doing events from memory) then check the key

 

Sorry - my answer is rubbish - gimme a sec to read the dox




Click the like button if you find my posts useful!
Highlighted
Developer
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: Simulating Keypress with jQuery or Javascript

OK - here you go

 

api.jquery.com/keydown/

 

Take especial notice of this bit

 

The event.which means 'What ket did the user press?'

 

The precentDefault causes the bind to be ignored - it never happned

 

$('#target').keydown(function(event) {
if (event.which == 13) {
event.preventDefault();
}

 

Interestingly you can translate one event into another (sorry everyone again - Phil has the code)

 

If you look at touchmove/end I convert it to a mouse event - check my code, search for simulate and / or mouse to see the call then google the call for more info




Click the like button if you find my posts useful!