It is difficult, but not impossible. What you can do is pretty hard and a bit of a hack that will work in Firefox 2+, IE 5.5+ and the latest WebKit browsers like Safari 4 or Chrome (unverified in older versions). Recent versions of TinyMCE and CKEditor use this technique in their iframe-based editors:
- Detect ctrl-v / shift-ins event with a keystroke event handler
- In this handler, save the current user selection, add the textarea element off the screen (say, left -1000px) to the document, include contentEditable and call focus () in the text field, thus moving the caret and effectively redirecting the paste
- Set a very short timer (say, 1 millisecond) in the event handler to call another function that stores the textarea value, removes the text field from the document, turns on contentEditable again, restores the user's selection and inserts the text.
Please note that this will only work for keyboard insert events, and not from pastes in the context menu or in the edit menu. The paste event would be better, but by the time it fires, itβs too late to redirect the cursor to the text box (at least in some browsers).
source share