Changing the keypress

后端 未结 6 1169
北荒
北荒 2020-11-27 06:16

In an input box or contenteditable=true div, how can I modify a keypress for the letter \"a\" to return a keybress for the letter \"b\"? I.e., every time you type the letter

6条回答
  •  面向向阳花
    2020-11-27 06:41

    You can't change the character or key associated with a key event, or fully simulate a key event. However, you can handle the keypress yourself and manually insert the character you want at the current insertion point/caret. I've provided code to do this in a number of places on Stack Overflow. For a contenteditable element:

    • Need to set cursor position to the end of a contentEditable div, issue with selection and range objects

    Here's a jsFiddle example: http://www.jsfiddle.net/Ukkmu/4/

    For an input:

    • Can I conditionally change the character entered into an input on keypress?

    • show different keyboard character from the typed one in google chrome

    Cross-browser jsFiddle example: http://www.jsfiddle.net/EXH2k/6/

    IE >= 9 and non-IE jsFiddle example: http://www.jsfiddle.net/EXH2k/7/

提交回复
热议问题