Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguishing between "Enter" keypress for Japanese kanji suggestion and other "Enter" events

My question is somewhat related to this question. However, I do not have the luxury to listen for the submit event so the answer to that question will not work for me.

My question: Is it possible in Javascript to detect when the Japanese/Chinese suggestion menu is open? Or is there another way to distinguish an Enter keypress that is to accept the Japanese/Chinese character suggestion from any other Enter keypress?

The reason the solution from the other question will not work for my situation is that I am currently overriding the textarea functionality with JavaScript so that an Enter keypress submits the form instead of making a newline.

Therefore I want to be able to distinguish between an Enter keypress that is to accept a character suggestion for Japanese or Chinese and for any other Enter keypress I want to submit the form.

like image 597
diasks2 Avatar asked Oct 16 '25 04:10

diasks2


2 Answers

I use the following check to determine whether any keyboard event is happening inside that window (IME)

event.isComposing || event.keyCode === 229

isComposing alone is not enough, as Safari has a bug

keyCode is deprecated

But it's better than nothing, hope it helps!

like image 194
Andrey Piskunov Avatar answered Oct 18 '25 18:10

Andrey Piskunov


Using the keydown event instead of the keyup event (discovered in this answer) seems to work in the latest versions of Chrome, Firefox and Safari using the Mac IME. I haven't been able to test it in Explorer or using any other IMEs. If anyone can share results under other browsers or IMEs it would be great to know.

like image 31
diasks2 Avatar answered Oct 18 '25 17:10

diasks2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!