105
70

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VueのEnterキーイベントで日本語入力中のEnterを制御する

Posted at

やりたいこと

  • Enterキーイベントをトリガーに操作をしたい
  • 日本語入力中のEnterキーはトリガーを走らせたくない

どうすればよいか

@keyup.enter だといずれのEnterもkeyCode = 13を返してしまうが、@keydown.enterであれば日本語入力中のEnterはkeyCode = 229を返すので、@keydown.enterで判断すれば良い。

<div id="app">
  <!-- 日本語入力中のEnterでもイベントが発火してしまう -->
  <input @keyup.enter="trigger" placeholder="keyup.enter">
  <!-- 日本語入力中のEnterではイベント発火しない -->
  <input @keydown.enter="trigger" placeholder="keydown.enter">
</div>
<script>
new Vue({
  el: "#app",
  methods: {
    trigger: (event) => {
      // 日本語入力中のEnterキー操作は無効にする
      if (event.keyCode !== 13) return
    
      // 実行したい処理
    }
  }
})
</script>
105
70
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
105
70

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?