javascript 击键时的 Extjs 侦听器检查是否有任何字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5547785/
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
Extjs listener on keystroke checking if its ANY character
提问by neolaser
I cant seem to find this anywhere. I want to find the best way to check whether a keystroke is actually a character.
我似乎无法在任何地方找到这个。我想找到检查击键是否实际上是字符的最佳方法。
listeners:{
'keyup':function(f, e){
//Is key a letter/character
}
}
EDIT
编辑
Thanks to everyone who has answered, I DO know how to detect the actual key that was pressed, I want to know how to detect whether the key is actually a character (not arrows, backspace, enter etc)
感谢所有回答的人,我知道如何检测实际按下的键,我想知道如何检测键是否实际上是一个字符(不是箭头、退格、回车等)
回答by Stefan Gehrig
To capture keyboard events you must set the enableKeyEvents
property on the component to true
.
要捕获键盘事件,您必须将enableKeyEvents
组件上的属性设置为true
。
...,
enableKeyEvents: true,
listeners:{
'keyup': function(f, e){
var charCode = e.getCharCode();
var key = e.getKey();
}
},
...
See "Detecting keystrokes" for a description of the difference between keyCodeand charCode.
有关keyCode和charCode之间差异的说明,请参阅“检测击键” 。
回答by Tommi
You need to capture the parameters that are sent to the event. Then evaluate the event.getKey()
:
您需要捕获发送到事件的参数。然后评估event.getKey()
:
listeners:{
'keyup':function(field, event){
if(event.getKey() >= 65 && event.getKey() <= 90) {
//the key was A-Z
}
if(event.getKey() >= 97 && event.getKey() <= 122) {
//the key was a-z
}
}
}
回答by Abdel Raoof
You need to have the correct parameters for your event.
您需要为您的活动设置正确的参数。
'keyup': {
fn: function(field,event) {
//use event.getKey() to get the key
}
}
The first parameter is the field (form element) and second is the event object. You can make use of the event object to get your key press details. Use the getKey()
method or getCharCode()
as per your requirement.
第一个参数是字段(表单元素),第二个参数是事件对象。您可以使用事件对象来获取您的按键详细信息。使用该getKey()
方法或getCharCode()
根据您的要求。
getKey
method returns the numeric code. You will have to compare the range (a=65 & z=90) to check if the keypress was character. Ext JS provides some static variables for checking special keys.
getKey
方法返回数字代码。您必须比较范围 (a=65 & z=90) 以检查按键是否为字符。Ext JS 提供了一些用于检查特殊键的静态变量。
回答by Tim Down
You cannot tell from the keyup
or keydown
events whether the keystroke produced a character or not. The only event that can do this is keypress
. Even then, it's quite tricky to detect in some browsers because of inconsistent behaviour. See section "3.2. Values Returned on Character Events" on the following page: http://unixpapa.com/js/key.html
您无法从keyup
或keydown
事件中判断击键是否产生了字符。唯一可以做到这一点的事件是keypress
. 即便如此,由于行为不一致,在某些浏览器中进行检测也非常棘手。请参阅以下页面上的“3.2. 字符事件返回的值”部分:http: //unixpapa.com/js/key.html