Detecting IME input before entering in Javascript

I'm not even sure that this is possible, so I apologize if this is a stupid question.

I set the keyup through jQuery to run the function when the user types in the input field. It works great in English.

However, when entering text in Japanese / Korean / Chinese, the function is not called until the user confirms his text.

Is it possible to discover that they have started to print, and to access their text not yet completed?

I think maybe this is an OS level thing, so Javascript doesn't have access to it.

Edit: I only realized that this works in Chrome and Safari, but not in Firefox (he still had no way to try it on Windows). So Chrome calls keyup, and it's possible to get the text. But I still have a problem with Firefox.

+6
source share
2 answers

This is a known issue in Firefox, and which browsers should not be clear .

A possible method to solve this problem is where the text field is polled to change the text (instead of relying on events).

+5
source

compositionstart , compositionupdate and compositionend events can be useful. They help you detect when IME input is being used.

For example, consider using IME to type か (ka) in Japanese.
The following events (in that order) will be fired:

  • k (IME visible) keydown, composition , composition , composition , input
  • a (IME visible), composition , composition , composition , input
  • enter (closed IME), input

Note that compositon events are fired only when the IME is visible (before you press enter ). Also note that the keystroke event does not fire. This is done only for input without IME.

To access the incomplete text of the user, you can use the data property for the event.

 $('#input').on('compositionupdate', function(e) { console.log(e.data); }); 

For more information, see MDN: compositionstart , compositionupdate , compositionend , InputEvent .

+11
source

Source: https://habr.com/ru/post/896690/


All Articles