I would like to have an input element (type=text) or textarea element that validates dynamically by triggering specific keystrokes. This will be used for Chinese pinyin input, so for example:
The user types "ma2" into an input element. The keydown event triggers for every keystroke, and the 2 never appears. Instead, when the user presses "2" the "a" will receive a tone mark, like this: "á". In the end, the user will have typed: "má".
This can be accomplished by reading and changing the entire input value using $(element).val() however, when an input element has focus and it's value is set by calling .val("something"), the cursor moves to the end of the text. This works fine in most situations because a user just continues typing at the end of the field, but I want this to work in all situations.
...another solution to this problem would be to get/set the location of a cursor within an input or textarea element. I don't think this is possible in javascript, however.
So, Remy was on the right track for sure. Triggering keypresses wouldn't allow me to input special characters without a lot of hassle anyways. Instead, I catch the keydown event and set the value of the input/textarea, and then move the caret.
I haven't found anything good for caret getting/setting in jQuery, but the following really broke that problem down for me. I'll post a link to my final pinyin input code when it's stable. It's pretty close now.
http://blog.vishalon.net/Post/57.aspx http://demo.vishalon.net/getset.htm