javascript textarea: what was typed/pasted, and where?
<p>How do you find out what the user typed or pasted into a textarea? For example, if they typed in <code>B</code> (or <code>β</code>) at index 1, or if they pasted <code>hello world\n</code> at index 23. I've searched a fair bit but can't find anything. Here's what I have so far, uses jQuery:</p>

<pre><code>$("textarea").keydown(function(evt){
    var index= getCaret(this); // this.selectionStart doesn't work in IE
    var key = evt.keyCode; // Wrong.
}
</code></pre>

<p>See <a href="http://stackoverflow.com/questions/263743/how-to-get-cursor-position-in-textarea">this question</a> for <code>getCaret</code>. The problem with <code>event.keyCode</code> is non-english keyboard layouts. I'm entirely stuck as far pasting is concerned.</p>

<p>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.</p>

<p>(Deletion is easier - detect <code>keyCode==8</code> for backspace and <code>keyCode==46</code> for del. If <code>selection.length &gt; 0</code>, 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.)</p>

<p>Anything that gets any part of this closer to working in any browser is greatly appreciated. That is, how do you know:</p>

<ol>
<li>the unicode string value of the typed character?</li>
<li>when a paste occurred?</li>
<li>what the text content of the paste is?</li>
</ol>
