8

I have this interesting problem - I would like to read keypresses using javascript, but not directed into a text field.

This is straightforward on computers and I have that working (As long as the window has focus, the javascript can detect keypresses). However, on tablets running Android and iOS, the operating system ignores the keypresses. The javascript code only receives the keypress data if I am focused in a text field.

Is there some way around this? I would like to read the keypresses on tablets without having to be focused in a text field.

gotohales
  • 3,665
  • 1
  • 20
  • 34
user1873476
  • 141
  • 1
  • 1
  • 3
  • Try a hidden text field? – Diodeus - James MacFarlane Jan 15 '13 at 15:02
  • I think I want to stay away from text fields - my goal is to be typing and be able to click buttons at the same time. The hidden text field would still lose focus after I click a button. Like, a racing game that uses WASD to control but also there are buttons to do things - I would like to continue to use WASD to drive before/during/after I press the buttons. – user1873476 Jan 15 '13 at 15:08
  • if your trying to create a graphical program, are you using canvas? if you make your entire view area a canvas field, you should be able to detect all keypresses – Ryan Jan 15 '13 at 15:11
  • Also have you tried using a `onkeypress` event on your container div? If your entire page has a script to capture keypresses then that should register even on a tablet. – Ryan Jan 15 '13 at 15:15
  • Thanks for the suggestions - my sample code was a bare-bones just to test if it worked, so I will work on what you mentioned and see if they fix the problem – user1873476 Jan 15 '13 at 15:20
  • It's not looking good - first I tried a canvas example from here, a javascript game http://joncom.be/experiments/thrust/play/ Doesn't work on the tablet. Next for the onkeypress, I used http://www.java2s.com/Code/JavaScriptReference/Event-Handlers-Reference/onKeyPressExample.htm to test but a no-go on the tablet. – user1873476 Jan 15 '13 at 15:39
  • After more digging I found similar problems, no good solution yet though http://stackoverflow.com/questions/3588899/how-can-i-add-a-javascript-listener-to-capture-input-from-bluetooth-barcode-scan http://stackoverflow.com/questions/11418061/listen-to-keypress-in-html-javascript-on-ios-safari-with-bluetooth-scanner-keybo – user1873476 Jan 15 '13 at 15:55
  • The onkeypress event works on Android devices, however no luck yet on iOS – user1873476 Jan 15 '13 at 15:59
  • Have you tried the other key events? `keydown` or `keyup`? – Jordan Kasper Jan 15 '13 at 17:27

3 Answers3

2

Try something like this:

 $('input,textarea').on("keypress", function (e) {
    //do any action here
 });
Praveen N
  • 137
  • 1
  • 7
1

From what I've seen using an iPad for over 2 years, and writing my own web based games, I've come to the conclusion that the iPad will activate its keyboard only when a form input field is focused. That means that a web page user cannot enter any keyboard input unless a form input field has been focused.

Which means that what you are asking is a physical impossibility on these devices.

0

Instead of key event you can just use input. Worked for me.

 var userList = new List("MyListList", options);

 $(".main-search").on("input", function () {
            userList.search($(".main-search").val());
        });
Juan
  • 147
  • 1
  • 1
  • 10