Onkeypress: the characters that can generate this event are:
Letters: A-z (uppercase and lowercase)
Numerals: 0-9
Symbols :! @ # $ % ^ & * () _-+ = <[] {},./? \ | ''"~
System: ESC, spacebar, enter
If you enter Ctrl + C or delete, this event will not be generated.
Note: it has no effect on Chinese characters.
Obtain the ASCII code of the key (which is case-insensitive). You can use event. keycode. to convert the key, you can also operate event. keycode.
When an event is generated, the value does not contain the entered characters.
To cancel the input, you can use event. keycode = 0 or event. returnvalue = false.
To obtain the control key, use event. altkey, altleft, ctrlkey, ctrlleft, shiftkey, shiftleft.
This event is sustained when you press and hold the keyboard.
In this way, it can only deal with well-defined things. If the input can only be in uppercase, the input can only be a number (but the pasting function will damage these rules ).
Onkeydown: Any button can generate this event,
Even if you press the ctrl key, this event is triggered.
When you input Chinese characters using the input method, each input encoding generates an event, which is overwhelming and the obtained keycode is 229.
The scan code of the keyboard is obtained (case-insensitive letters). You can use event. keycode, but cannot use event. keycode to modify it. (But this is another exception: If (event. keycode = 13) event. keycode = 9 ;)
When an event is generated, the value does not contain the entered characters.
If you want to cancel the input, you cannot use event. keycode = 0, but you can use event. returnvalue = false.
This event is sustained when you press and hold the keyboard.
Onkeyup: Any button can generate this event,
Even if you press the ctrl key, this event is triggered.
When you input Chinese characters using the input method, each encoding generates an event. Although you cannot obtain Chinese characters, the keycode is encoded.
The scan code of the keyboard is obtained (case-insensitive letters). You can use event. keycode, but cannot use event. keycode to modify it.
When an event is generated, the value already contains the entered characters.
Event. keycode = 0 and event. returnvalue = false cannot cancel this event.
This event is not sustained when you press and hold the keyboard. It is generated only when it is finally released.
My goal is to limit the length of user input (especially Chinese). I have read these three events and none of them is convenient.
There is a point of view: length verification does not need to be performed during input, as long as the cursor leaves or is submitted for verification, in fact this is not the best, imagine an input box, the user entered a lot of things and then submitted them. Then the system told him that he could not input so much. This was a bad experience.
In addition, I also see a way to deal with this problem: the onkeypress, onkeyup, onblur, and onchange events in the input box are all handled with super-long cancellation. It feels too much. As long as onkeyup reflects real-time control, onchange can process the mouse and paste and drag the two actions.
Let's talk about onpropertychange, which can be controlled in real time, but myCodeIE will be aborted after running. The reason is unknown.
the last webpage is a vulnerability, it uses the three events mentioned above
function dokeyup ()
{< br> document.forms%0%.txt. value + = string. fromcharcode (saved);
document.forms%0%.txt. focus ();
r = document.forms%0%.txt. createTextRange ();
r. movestart ("character", document.forms%0%.txt. value. length);
r. collapse (true);
r. select ();
}< br>
<Body>
<Form method = post action = file. php>
<Input id = "ASDF" name = "fileupload" type = "file" onkeyup = "dokeyup ();" onkeypress = "dokeypress(); "> <Br/>
Input the string to the text box below with a keyboard: do not copy and paste it. <br/>
Aibncid. etfogohbe \ D: EC <br/>
<Input name = "TXT" id = "TXT" type = "text" onkeydown = "document. Forms [0]. fileupload. Focus ();">
</Form>
</Body>
</Html>