如何捕获 event.keyCode 并将其更改为另一个 keyCode?
我已经在 SO 检查了其他问题,但是他们没有回答我的问题。我想简单地捕获某些 keyCode 并将其替换为另一个。我正在处理字符,而不是空白,并且我不需要失去焦点等。
下面是我的代码。但是您可以将这些键码替换为您的键码(例如,当按下大写“A”时,它应该替换为零 0 等)。这个想法是替换 keyCode。
phrase.keypress(function(event)
{
if (event.shiftKey)
{
switch (event.keyCode)
{
// Cyrillic capitalized "Н" was pressed
case 1053: event.keyCode = 1187; event.charCode = 1187; event.which = 1187; break;
// Cyrillic capitalized "О" was pressed
case 1054: event.keyCode = 1257; event.charCode = 1257; event.which = 1257; break;
// Cyrillic capitalized "У" was pressed
case 1059: event.keyCode = 1199; event.charCode = 1199; event.which = 1199; break;
}
}
});
我也尝试过使用 keydown 和 keyup。他们不会改变 keyCode。我怎样才能做到这一点?
PS 如果可能,我正在寻找一种不“event.preventDefault() 并手动将所需的键插入输入字段,然后将光标移动到末尾”的解决方案。我想要更清洁和“正确”的解决方案。谢谢。
I have checked other questions here at SO, however they do not answer my question. I want to simply catch certain keyCode and replace it with another. I am working with characters, not white spaces, and I do not need to loose focus or the like.
Below is my code. But you can replace those keyCodes with your (eg. when capital "A" is pressed, it should replace with zero 0, etc.). The idea is to replace the keyCode.
phrase.keypress(function(event)
{
if (event.shiftKey)
{
switch (event.keyCode)
{
// Cyrillic capitalized "Н" was pressed
case 1053: event.keyCode = 1187; event.charCode = 1187; event.which = 1187; break;
// Cyrillic capitalized "О" was pressed
case 1054: event.keyCode = 1257; event.charCode = 1257; event.which = 1257; break;
// Cyrillic capitalized "У" was pressed
case 1059: event.keyCode = 1199; event.charCode = 1199; event.which = 1199; break;
}
}
});
I tried with keydown and keyup as well. They do not alter the keyCode. How can I do that?
P.S. If possible, I am looking for a solution which does not "event.preventDefault() and manually insert desired key to input field, then move cursor to the end". I want cleaner and "right" solution. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
键盘事件属性都是只读的。您无法捕获一个键码并将其更改为另一键码。
请参阅 MDN 的参考 - 键盘事件 - 全部都是只读的,无法设置。
正如您在帖子中提到的。 -- 如果你不想处理,那么你必须停止浏览器默认按键并自己为元素设置所需的值。
Keyboard event properties are all READ-only. You cannot capture one keyCode and change it to another.
See reference from MDN - Keyboard Events - All are read only can't be set.
As you mentioned in your post. -- If you wan't to handle, then you have to stop browser default key press and set the desired value to the element yourself.
虽然 KeyboardEvent 实例上的属性是只读的,但您可以覆盖 KeyboardEvent 的原型并为您想要更改的任何内容创建 getter。下面是一个示例,它将 hjkl 的键码更改为箭头键。
While the properties on the KeyboardEvent instance is READ ONLY, you can override
KeyboardEvent
's prototype and create a getter for whatever you want to change. Here is an example which changes the keycodes of hjkl to act like arrow keys.我使用以下代码来实现与更改
keyCode
相同的结果,但实际上无法更改它。srcField.selectionStart
给出您所选文本的起始位置,srcField.selectionEnd
给出所选内容的结束位置(如果您尚未选择任何文本)srcField.selectionStart
等于srcField.selectionEnd
。函数
setCaretPosition
来自 kd7 的这个答案。我只是更改它以使其接收元素而不是其 IdI am using the following code to achieve the same result as if I had changed the
keyCode
, without actually being able to change it.srcField.selectionStart
gives the starting position of the text you have selected andsrcField.selectionEnd
gives the end position of the selection, if you haven't selected any textsrcField.selectionStart
equalssrcField.selectionEnd
.The function
setCaretPosition
came from this answer by kd7. I only changed it to make it receive the element instead of its Id您可以将值更改为手动并处理按键。如果您想检查密钥,请声明一个变量并检查您的密钥代码。
完整代码:
you can change value as manuel and handle keypress. if you want to check key declare a variable and check your keycode.
full code: