Capture Backspace Key Press in BlackBerry with jQueryMobile

Is there a way to capture the backspace key pressed on input[type="text"] in BlackBerry? I tried with $('input[type="text"]').bind('keydown', function(event) { ... }); , and it captures all keypress events except one for backspace (del). Pressing this key does not trigger any key events.

Does anyone know a way to capture an event?

I am developing for OS 6.0 and testing using the BlackBerry 9800 emulator.

EDITED is the code I'm testing

 <div id="myPage" data-role="page" data-theme="b"> <div data-role="content"> <input type="text" id="ddd" /> </div> <script type="text/javascript"> $('input[type="text"]').bind('keydown', function(e){ if(e.keyCode == 8) alert('backspace trapped') }); </script> </div> 
+8
javascript-events jquery-mobile jquery-events blackberry keyevent key-events blackberry-simulator
source share
4 answers

I just opposed this annoyance and found this question in the search for answers, so here are the details of my research and solution (well, a workaround).

The keyup and keydown simply will not fire on the input or textarea elements in the Blackberry browser when the backspace key is pressed. This will, however, fire when the event handler is bound to document :

 $("#myInput").keydown(someFn); //Will not fire for backspace $(document).keyup(someFn); //Will fire for backspace 

Why this is so, I have no idea. The keyup event should bubble up, and this happens, but since it doesn't even fire when you press the backspace key, that is not much.

However, we have another event at our disposal. The input event is supported by the Blackberry browser and correctly fires at any time when the value of an element changes (including, fortunately for us, when this change occurs due to pressing the backspace key).

Thus, we can solve the problem by binding event handlers to keydown and input . The keydown event will fire before input , unless the backspace key is pressed, in which case keydown will not fire. Therefore, we can easily track this:

 function handler(e) { if (e.keyCode === 8) { alert("Backspace!"); //Backspace was pressed :) } } var elem = document.getElementById("example"); elem.addEventListener("keydown", function (e) { //Bind to keydown event this.keydownFired = true; //Remember that keydown fired in expando property handler.call(this, e); //Call the event handler }, false) elem.addEventListener("input", function (e) { //Bind to input event if (!this.keydownFired) { //Keydown didn't fire, must have pressed backspace e.keyCode = 8; //Fix the event object handler.call(this, e); //Call the event handler } delete this.keydownFired; //Clean up so we can handle next key press }, false); 

Some notes:

  • As far as I can tell, this is only a problem in the browser on Blackberry 6. I tested Blackberry 5 (physical device and simulator) and 7 (simulator), and both will trigger keydown and keyup events for the backspace keyword.

  • This “fix” works in almost every browser in which I tested it (so you can use it to properly support Blackberry 6 without breaking other browsers), except Opera Mobile (tested in version 12), which for some people like to run twice input event.

  • This allows you to detect a backpressure when text is entered at a delete (otherwise, the input event does not fire). This is probably the biggest fall script.

  • You can find a working example here, but to test a mobile device, it loads the embedded version faster.

+6
source share

The following code works fine. you can see it on jsfiddle . tested it on chrome.

 $(document).ready(function() { $('input[type="text"]').bind('keydown', function(e){ if(e.keyCode == 8) alert('backspace trapped') }); });​ 

for using blackberry

 function captureBackButton() { blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK, function() { alert('Backspace Pressed') }); } 

see details

+4
source share

You can use http://jsbin.com/ezucen/13/ to find out which code code you are returning. On the BlackBerry 9900 7.1, I get keyCode 8.

+1
source share

In a general browser there is no way to accomplish this.

The only way to track back key events using JavaScript is to use the Widget / WebWorks app using the KEY_BACK API.

+1
source share

All Articles