Hello,
I am trying to integrate bluetooth hardware barcode scanner into my Ionic app. The scanner emulates a keyboard device according to the Human Interface Device standards. The scanner’s output can be directed into any input field where keyboard entry can occur.
But this is not the case I’m trying to solve. I need to acquire the virtual keystrokes generated by the scanner when there is no active input focus - no HTML5 input. I created a document level keypress event handler that works perfectly when running the app within Safari on Mac OS X. In my UI I have no inputs that take keyboard entry. When I scan a barcode my document-level keypress handler gets all the events. Perfect.
However, when running the same Ionic app on iOS, I get no events. After some experimentation, I determined that the document receives keypress events only if focus is on an input field. This is not what I want. An input field, even if off screen, causes the soft keyboard to raise. It also can easily lose focus if the user taps somewhere.
I can’t figure why the document-level keypress handler that works perfectly for the Ionic app running in Safari on OS X does not receive keypress events from the external keyboard on iOS. I tested a vanilla Cordova app as well. I don’t think it has anything to do with Ionic. I’m really stuck here. Any ideas?
Here’s the top level keypress-events directive that is attached to the body element. Try it out. You’ll see you can bang away at the keyboard within an Ionic UI that has no inputs in a browser and it works just fine – gets all the events. Run it on iOS and it’s deaf.
....directive (
'keypressEvents', function ($document, $rootScope) {
return {
restrict: 'A',
link: function () {
$document.bind (
'keypress', function (e) {
console.debug ("keypress:" + e.which, "'"
+ String.fromCharCode (e.which) +"'");
$rootScope.$broadcast ('keypress', e, String.fromCharCode (e.which));
});
}
}
})
Thanks
Jason