Javascript read keypresses on tablet?

泄露秘密 提交于 2019-12-10 04:04:54

问题


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


来源:https://stackoverflow.com/questions/14340469/javascript-read-keypresses-on-tablet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!