javascript Android 浏览器中某些键没有按键事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9302986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No keypress events for certain keys in Android browser
提问by Zut
Below code works perfect in Chrome, Firefox, on IPhone, and even in third-party browsers on Android. However, when run within the native browser key events for special characters like ?, ?, and ? on my Swedish keyboard are simply not triggered.
下面的代码在 Chrome、Firefox、iPhone 甚至 Android 上的第三方浏览器中都可以完美运行。但是,当在本机浏览器中针对特殊字符(如 ?、? 和 ? 在我的瑞典语键盘上根本没有被触发。
The example should only allow the user to enter a single character at a time. Works like a charm, unless I in android press keys like ?, ? or ? for which I can enter any number of characters.
该示例应该只允许用户一次输入一个字符。就像一个魅力,除非我在 android 中按键像 ?, ? 或者 ?我可以输入任意数量的字符。
Here's a jsFiddle for any who wish to try it out: http://jsfiddle.net/x7H6f/. If u don't have special keys like my Swedish ones printed on your keyboard, characters like é (hold E) should do the "trick".
这是任何想要尝试的人的 jsFiddle:http: //jsfiddle.net/x7H6f/。如果你没有像我的瑞典语那样的特殊键印在你的键盘上,像 é(按住 E)这样的字符应该可以“把戏”。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Key Event test</title>
</head>
<body>
<input type="text" id="a" name="test" />
<script>
document.getElementById("a").onkeypress = function(e) {
e = e || window.event;
var k = e.keyCode || e.which;
this.value = String.fromCharCode(k);
return false;
}
</script>
</body>
</html>
And no, keydown and keyup don't work either. Did I miss something, or is this a bug? It's horribly annoying when developing Swedish Apps in PhoneGap!
不,keydown 和 keyup 也不起作用。我错过了什么,还是这是一个错误?在 PhoneGap 中开发瑞典应用程序时非常烦人!
Thanks!
谢谢!
EDIT:As Manor says in his answer, the input
event can be used. Here's a fiddle which demonstrates the differences between the keypress
, input
, and change
events: http://jsfiddle.net/Qxd76/(use http://jsfiddle.net/Qxd76/showto view the result on a smartphone).
编辑:正如 Manor 在他的回答中所说,input
可以使用该事件。下面是这表明了之间的差异的小提琴keypress
,input
和change
事件:http://jsfiddle.net/Qxd76/(使用http://jsfiddle.net/Qxd76/show查看在智能手机上的结果)。
采纳答案by g.e.manor
i just spent allot of time about this problem. i'm develop to hebrew and all hebrew charters isn't firing event for me.
我只是花了很多时间来解决这个问题。我正在开发希伯来语,所有希伯来语宪章都没有为我开火。
i found the solution and i'll give for it short explain before
我找到了解决方案,我会在此之前对其进行简短的解释
when use all key events the browser search for the key you pressing for. cause some reason all charters in other language from english in android native browser is unknown and this is why isn't fire any event when you fill in input charter.
当使用所有按键事件时,浏览器会搜索您按下的按键。出于某种原因,Android 原生浏览器中所有其他语言的英语宪章都是未知的,这就是为什么在您填写输入宪章时不会触发任何事件的原因。
the solution is to search for event in the input and not on the key. for example if we have in text input .change() method is will be great for us.
解决方案是在输入中而不是在键上搜索事件。例如,如果我们在文本输入中有 .change() 方法对我们来说会很棒。
i'm using in addEventListener() method and is working like a charm.
我在 addEventListener() 方法中使用,并且工作得很好。
this is the code
这是代码
var exmpleInput = document.getElementById('input-id');
if(exmpleInput) {
exmpleInput.addEventListener("input", function() {
exampleFunction(this)
}, false);
}