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
New Developer
Posts: 6
Registered: ‎05-26-2009
My Device: Torch 9810/Playbook
My Carrier: Rogers

capturing keyboard events on BB in javascript

Hi,

 

I am using a BB8900 with OS 4.6.  I was wondering if it's possible to capture keyboard events from the blackberry browser via javascript on a webpage.  Please note that I only care about the browser that comes with OS 4.6 for now as all of the blackberry units accessing the site will be running that version.

 

I've created a listener:

function processKeyPress() { alert('good!'); }

 

document.addEventListener('keypress', processKeyPress, false);

 

The processKeyPress() function never gets called.  If I view the same page in firefox, the keypress events are handled accordingly and the function is called. 

 

The BB javascript documentation claims that the BB browser is supposed to be able to handle keyboard events but they never seem to be handled.  It seems as though these keypresses are instead being intercepted by the browser for use as browser keyboard shortcuts.  For example, hitting 'd' will cause the browser to minimize.  Is there a way to have these keypresses processed by the page instead?  Any tricks?

 

NOTE:  The same thing occurs if I try:

<body onkeypress="processKeyPress();">

 

So I guess the key here is whether or not it's possible for these keypresses to get to the page instead of the browser intercepting them.

 

TIA.

Developer
Posts: 107
Registered: ‎09-25-2008
My Device: Not Specified

Re: capturing keyboard events on BB in javascript

Have you tried listening for the keydown or keyup events?
New Developer
Posts: 6
Registered: ‎05-26-2009
My Device: Torch 9810/Playbook
My Carrier: Rogers

Re: capturing keyboard events on BB in javascript


staktrace wrote:
Have you tried listening for the keydown or keyup events?

Thanks for the reply staktrace.  That's a good idea to try.  I hadn't tried it earlier but just tried it now.  Unfortunately without success.  With that being said, if a textbox is focussed, then the events will work (including onkeypress). 

 

I found a workaround based on the knowing that they'll process keyboard events when a textbox is focussed.  That is, I can create a hidden textbox and set focus to it in onload.  Then the keyboard events work even if the user interacts with the page.

 

It'd be better if BB gave us an option to disable the BB browser shortcuts so that we could always handle keyboard events but I guess this workaround will do for now.  Smiley Wink

New Contributor
Posts: 2
Registered: ‎08-10-2010
My Device: Not Specified

Re: capturing keyboard events on BB in javascript

How are you hidding your textbox?

 

textbox.style.display = "none";

does not seem to keep focus.

 

Also, you have to keep setting focus back to the text box if the user clicks other places on the page correct?

 

 

Also.. the onkeypress has to be in the body tag not the text box tag right?

 

ftt
Contributor
Posts: 10
Registered: ‎02-24-2012
My Device: none
My Carrier: none

Re: capturing keyboard events on BB in javascript

Try 

window.addEventListener(…)