jQuery 按下美元键时如何找出在 keyup 或 keypress 事件上按下的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15158061/
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
How to find out the key pressed on keyup or keypress event when dollar key is pressed
提问by Anoop
The below method returns any key from A-Z and 0-9 and when I pressed shift key + 4 its returns the same code 52.
下面的方法返回 AZ 和 0-9 中的任何键,当我按下 shift 键 + 4 时,它返回相同的代码 52。
function keyUpEvent(e)//onkeyup event
{
var chr = String.fromCharCode(e.keyCode);//converts the keycode into a character
alert(chr);//alerts the character
}
But I need to show the $ or %when the respective key with combination of shift key is pressed.
但是当按下带有 shift 键组合的相应键时,我需要显示$ 或 %。
回答by Imperative
回答by Konga Raju
Key codes relate only to keys. $
is a character, achieved through two keys, Shift and 4. There is no key code explicitly for $
when using onkeydown
密钥代码仅与密钥相关。$
是一个字符,通过两个键 Shift 和 4 实现。使用时没有明确的键码$
onkeydown
Edit: It was pointed out by Edward that onkeypress
uses key combinations, and does have keycode's for combinations. Learn something new every day :)
编辑:爱德华指出onkeypress
使用组合键,并且确实有组合键代码。每天学些新东西 :)
Here's some code, edited from the onkeydown
example provided by the MDN, to detect keypress keycodes.
这是从onkeydown
MDN 提供的示例中编辑的一些代码,用于检测按键键码。
Here's the fiddleupdated to be Firefox-friendly, using help from this S.O. post. The JQuery solution works too, if you swing that way.
这是使用此 SO post 中的帮助更新为对 Firefox 友好的小提琴。JQuery 解决方案也适用,如果你这样摆动。
$ is equal to 36
.
$ 等于36
。
JavaScript Event KeyCode Test Page
Here is same S.O thread
这是相同的SO 线程
回答by nefarianblack
You cannot check for two key events at once. You can try e.shiftKey which tells you whether the shift key was being pressed when the event occurred. Try to check:
您不能一次检查两个关键事件。您可以尝试 e.shiftKey,它会告诉您在事件发生时是否按下了 shift 键。尝试检查:
if(e.keyCode === 53 && e.shiftKey){}
also if you have a text field, than you can attach event on key enter and check for '%'.
此外,如果您有文本字段,则可以在按键输入时附加事件并检查“%”。
https://developer.mozilla.org/en-US/docs/DOM/event.shiftKey
https://developer.mozilla.org/en-US/docs/DOM/event.shiftKey
thanks to Marcel Korpel
感谢马塞尔·科佩尔
回答by KULKING
In KeyDown
event of TextBox
, use:
在 的情况KeyDown
下TextBox
,使用:
if (e.Shift && e.KeyCode == Keys.4) {
//Your code here
}