Javascript read keypresses on tablet?

2019-04-24 06:56发布

问题:

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.

回答1:

Try something like this:

 $('input,textarea').on("keypress", function (e) {
    //do any action here
 });


回答2:

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.



回答3:

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());
        });