简体   繁体   English

在日语输入中输入keyup事件

[英]Enter keyup event in japanese input

I've an input field on which I'm listening to keyup events. 我有一个输入字段,我在上面监听按键事件。

Using the Japanese input method I start typing characters and the event doesn't get triggered; 使用日语输入法,我开始输入字符,并且不会触发该事件。 which is expected as the enter characters are being converted to hiragana and a drop down appears so the user can select the katakana or kanji version of them. 这是预期的,因为输入字符将转换为平假名,并出现一个下拉菜单,以便用户可以选择它们的片假名或汉字版本。 While the user is typing the characters appear underlined and the user can select it's choice (kanas/kanji) by pressing enter. 在用户键入内容时,带下划线的字符会出现,用户可以通过按Enter键选择它的选项(卡纳斯语/汉字)。 After that the text is no longer underlined and is 'commited' to the input text. 之后,该文本将不再带有下划线,并被“提交”到输入文本中。

This behavior is expected as is how the input method is intended to work. 预期此行为以及输入方法的预期工作方式。

However, I wasn't expecting to receive any keyup events until the text has been commited (an even then I would expect a change event no a keyup), since that enter is part of how the input method works. 但是,在提交文本之前,我不希望收到任何键入事件(即使这样,我也希望没有键入事件发生更改事件),因为该输入是输入法工作方式的一部分。

I'm listening to keyup events because I need to trigger an action when the user releases the enter key. 我正在监听按键事件,因为我需要在用户释放Enter键时触发一个动作。

I have analyzed event data when typing an enter in western and japanese input method and didn't find any relevant difference. 在西方和日语输入法中输入Enter时,我已经分析了事件数据,但没有发现任何相关差异。

Which is the proper way to deal with that? 哪个是解决该问题的正确方法?

Regards 问候

You can achieve this with the following technique: 您可以使用以下技术来实现:

  • Use textarea instead of input 使用textarea代替input
  • Use an onInput listener instead of onKeyDown 使用onInput侦听器,而不是onKeyDown
  • Detect newlines in onInput onInput检测换行符

The relevant JavaScript code: 相关的JavaScript代码:

function onInput(evt) {
   let val = evt.target.value;
   const nlAt = val.indexOf('\n');
   if (nlAt >= 0) {
       // A newline was detected in the input!
       // Remove the newline from the field:
       val = val.replace(/\n/g, '');
       evt.target.value = val;
       // And fire our custom onReturnKey handler:
       onReturnKey(val);
   }
}

You can try this CodePen . 您可以尝试使用此CodePen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM