The premise

Project to develop the editor, the need is to set some shortcut keys to the editor

demand

Alt + Alt + ~1
alt+2
alt+3.Copy the code

implementation

We normally use keyCode to do this

document.addEventListener('keydown'.function(e){
    / / the main keyboard
    if(e.altKey && e.keyCode == 192) { // Corresponding to Alt + ~. }if(e.altKey && e.keyCode == 49) { / / Alt + 1. }if(e.altKey && e.keyCode == 50) { / / Alt + 2. }.../ / keypad
    if(e.altKey && e.keyCode == 97) { / / Alt + 1. }if(e.altKey && e.keyCode == 98) { / / Alt + 2. }... })Copy the code

Pit point

  1. KeyCode 229 problem
1.  Read the virtual key code from the operating system's event information, if such information is available. 2. If an Input Method Editor is processing key input and the event is keydown, return 229. 3. If input key when pressed without modifiers would insert a numerical character (0-9), return the ASCII code of that numerical character. 4. If input key when pressed without modifiers would insert a a lower  case character in the a-z alphabetical range, return the ASCII code of the upper case equivalent. 5. If the implementation supports a key code conversion table for the operating system and platform, look up the value. If the conversion table specifies an alternate virtual key value for the given input, return the specified value. 6. If the key's function.as determined in an implementation-specific way.corresponds to one of the keys in the [table of fixed virtual key codes] (https://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html#fixed-virtual-key-codes), return the corresponding key code.
7.  Return the virtual key code from the operating system.
8.  If no key code was found, return 0.
Copy the code

reference

Lists.w3.org/Archives/Pu…