Subscribe on changes!

Vue.withModifiers() does not accept key modifiers

avatar
Jan 22nd 2023

Vue version

3.2.45

Link to minimal reproduction

https://sfc.vuejs.org/#eNpVkMFugzAMhl/F8g4FiYY7o5V2nvYE8w4duCVV40RJKJoQ7z5DtU275bed3/7+GV9CMPeRscE2ddGGDInzGI4k1gUfM8zQRT5l1sEKhgomm4c339uz5ZhggXP0DnZqsSMh6bykDKcQ4PD3r5hJACJLz7EoYVOrzmMUGApCK2HMhJUu8/LKX72fpPm/qSi4hMMR1gX+xubmL1qp4J2QJXMk/ChhKZ9X76UiWUrj/ChZ3Z/0HMK11dYPRqVTkdmFm16oCqDt7R1sfyDcpo9trQXttPXvGFb4yGTvTsFckxdNbYNRgq2RCJsfPELNZNWEQ84hNXWdzt2a9TUZHy+1vkzUC61jw8ntP6OfEkc1JtwoFAKXbwx8jl0=

Steps to reproduce

  1. Follow the link to the minimal reproduction. It creates an element with an event with an "enter" key modifier:

    Vue.h("input", { onKeydown: Vue.withModifiers((e) => console.log(e), ["enter"]) })
    
  2. Open the JavaScript console.

  3. Focus the input element and press some keys.

What is expected?

Only when pressing the "enter" key, there should be a message in the console log.

What is actually happening?

Every key leads to output in the console log.

System Info

System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 10.14 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 19.4.0 - /opt/homebrew/bin/node
    npm: 9.2.0 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 109.0.5414.87
    Firefox: 107.0.1
    Safari: 16.0

Any additional comments?

I'm aware of the difference between event modifiers and key modifiers. But the documentation clearly says: "For other event and key modifiers, the withModifiers helper can be used." So I would expect that I can pass key modifiers to withModifiers as well.

avatar
Jan 22nd 2023

What the docs really mean here is that you can use it for modifier keys, i.e. Shift etc. Replace your "enter" with "shift" and it works (only logs when shift key is being held down while typing).

A PR in the docs repo to word this better would surely be apprechiated by the docs team.

Yes, I'm aware that in templates you can do@keyup.enter and it filters for the enter key. However that's a different kind of filter, which in temlates is only being added when the compiler finds it being used on keyup/keydown events (v-on:input.enter doesn't do anything). It's currently not a public API.