Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle key press by function in VueJs

in my component I am using VueStrap's modal like this:

<template>
    <modal-window v-model="show" v-on:keyup="keyHandler($event)" @ok="submit()" @cancel="cancel()" @closed="close()" ... >
       ...
    </modal-window>
 ...
</template>
<script> 
    ...
    methods: {
       keyHandler (event) {
           console.log(event);
       }
    },...
</script>

I want handle key press when that modal is opened and ensure submit modal when enter pressed or close modal when esc pressed.

I added custom function keyHandler which is unfortunately never fired. Can you tell me how to fix code to handle key press in that function? Or when is better way how to close and submit vue strap modal I will be grateful for advice. Thank you.

like image 533
Denis Stephanov Avatar asked May 17 '18 15:05

Denis Stephanov


People also ask

What is the best way to use key input in Vue?

Alternatively, you may want to consider using the v-hotkeydirective for key input in Vue (docs, github). This will keep your code relatively clean and simple if you must consider several different key inputs. 1.

How to prevent form submission on Enter key in vuejs?

You can refer to event modifiers in vuejs to prevent form submission on enter key. It is a very common need to call event.preventDefault() or event.stopPropagation() inside event handlers.

What is event handling in Vue?

These functions are known as event handlers. Vue is no different. It has an event handling mechanism that allows you to catch and handle events from within each independent component file. Events can appear either as user input or we can manually create them in the code and emit them when needed.

What is the keycode of the enter event in Vue?

@keyup.enter.native="onEnter". 13 is the keycode of enter. For @ key event, the keycode is 50. So you can use @keyup.50. Is there a site that lists the mapping between characters and numbers (like 50 and @)? I couldn't find it in the Vue documentation.


3 Answers

You can attach your event handler to window, that way you can receive all key events and act accordingly depending on your modal's state:

Vue.component('modal', {
  template: '<div>test modal</div>',
});

new Vue({
  el: "#app",
  created() {
    window.addEventListener('keydown', (e) => {
      if (e.key == 'Escape') {
        this.showModal = !this.showModal;
      }
    });
  },
  data: {
    showModal: true
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
  <modal v-show="showModal"></modal>
</div>
like image 146
Amr Noman Avatar answered Sep 26 '22 06:09

Amr Noman


easiest way

<input v-on:keyup.13="whatkey()" type="text"> <br>

looks for if the enter key is pressed then fires a method called whatkey.

like image 22
roblem Avatar answered Sep 25 '22 06:09

roblem


Alternatively, you may want to consider using the v-hotkey directive for key input in Vue (docs, github). This will keep your code relatively clean and simple if you must consider several different key inputs.

1.  Install it:

npm i --save v-hotkey
  1.  Have Vue 'use' it:

    import VueHotkey from "v-hotkey"; Vue.use(VueHotkey);

3.  Apply it to your Vue components, like so:

<template>
    <modal-window ... v-hotkey="keymap">
       ...
    </modal-window>
</template>
<script>
  ...
  data() {
    return {
      showModal: false
    };
  },
  computed: {
    keymap() {
      return {
        "esc": this.toggleModal
      };
    }
  },
  methods: {
    toggleModal() {
      this.showModal = !this.showModal;
    }
  }
</script>
like image 32
Cato Minor Avatar answered Sep 26 '22 06:09

Cato Minor