How do you find out what the user typed or pasted into a textarea? For example, if they typed in B (or β) at index 1, or if they pasted hello world\n at index 23. I've searched a fair bit but can't find anything. Here's what I have so far, uses jQuery:
$("textarea").keydown(function(evt){
var index= getCaret(this); // this.selectionStart doesn't work in IE
var key = evt.keyCode; // Wrong.
}
See this question for getCaret. The problem with event.keyCode is non-english keyboard layouts. I'm entirely stuck as far pasting is concerned.
If possible, I'd like to know the values before the textarea is changed - that is, from within the keydown event. I'm not sure if this can be done for pasting.
(Deletion is easier - detect keyCode==8 for backspace and keyCode==46 for del. If selection.length > 0, then the selection is removed; otherwise the character before or after is removed. In theory. There's also the case of pasting on top of a selection.)
Anything that gets any part of this closer to working in any browser is greatly appreciated. That is, how do you know:
- the unicode string value of the typed character?
- when a paste occurred?
- what the text content of the paste is?