Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chinese characters and the onkeypress event

Using Internet Explorer on Windows I have an onkeypress event detecting text entered into a text box. However when entering Chinese characters this event doesn't fire. Has anyone encountered this or have suggestions on working around this?

like image 876
dlanod Avatar asked Aug 17 '11 04:08

dlanod


3 Answers

Google Suggest (autocomplete) polls the input for changes as the events are completely unreliable for Unicode. Browsers used to support mid-IME input events which was very nice for Japanese, but support quickly broke or was dropped.

Last time I check was 2006 though, so retesting is required. Here are my previous notes:

http://web.archive.org/web/20060220125639/http://fnjordy.cus.org.uk/auto/

Including non-resolved bug in Firefox:

https://bugzilla.mozilla.org/show_bug.cgi?id=286842

like image 70
Steve-o Avatar answered Nov 13 '22 22:11

Steve-o


Based on this JSFiddle, it seems the keydown event does not fire as one might expect. I would use the keyup event (though, you'll still get partially entered characters).

From my experiment, I got:

  • An event for "ni" instead of 你
  • An event '你ha'
  • An event for 你哈 (since the alert killed my input to complete 'hao')

Basically, Windows and IE are not playing nicely with character input. I would suggest using the change event if it's possible in your framework.

like image 31
ghayes Avatar answered Nov 13 '22 23:11

ghayes


Depending on the keyboard settings, writing a single Chinese character usually requires typing several keys. For example, 豈 can be written using a 4-key sequence (山一口廿), as you can see here: http://en.wiktionary.org/wiki/%E8%B1%88

You need to use an onchange event in this case (and also for Korean and Japanese!)

like image 1
liopic Avatar answered Nov 13 '22 21:11

liopic