Is there a way to capture backspace key press on a input[type="text"] in BlackBerry? I have tried with $('input[type="text"]').bind('keydown', function(event) { ... }); and it captures all key press events except the one for the backspace (del). Pressing this key does not fire any key event.
Does anyone know a way to capture the event?
I am developing for OS 6.0 and testing with BlackBerry simulator 9800.
EDITED – the code that I am 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>
I have just come up against this annoyance, and found this question in my search for answers, so here are details of my investigation and solution (well, workaround).
The
keyupandkeydownevents simply will not be triggered oninputortextareaelements in the Blackberry browser when the backspace key is pressed. It will, however, be triggered when the event handler is bound to thedocument:Why this is the case, I have absolutely no idea. The
keyupevent should bubble, and it does, but since it doesn’t even fire when you press the backspace key, that’s not much use.However, there is another event at our disposal. The
inputevent is supported by the Blackberry browser, and correctly fires any time the value of the element changes (including, fortunately for us, when that change is due to a press of the backspace key).Therefore, we can kind of workaround the problem by binding event handlers to both
keydownandinput. Thekeydownevent will fire beforeinput, except if the backspace key is pressed, in which casekeydownwon’t fire. So we can keep track of that quite easily:Some notes:
As far as I can tell this is only an issue in the browser on Blackberry 6. I’ve tested Blackberry 5 (physical device and simulator) and 7 (simulator) and both will fire the
keydownandkeyupevents for the backspace key.This “fix” works in almost every single browser I have tested it in (so you can use it to properly support Blackberry 6 without breaking other browsers) except Opera Mobile (tested in version 12), which for some reason likes to fire the
inputevent twice sometimes.This only allows you to detect backspace presses when there is text in the input to delete (otherwise the
inputevent doesn’t fire). This is probably the biggest downfall of the script.You can find a working example here, but for mobile device testing it’s quicker to load the embedded version.