最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i detect multiple key press (updown) on Vue.js - Stack Overflow

programmeradmin2浏览0评论

I am trying to implement mon chat app on Vue.js.

window.onload = function () {
  new Vue({
    el: '#vue-chat',
    data: {
      body: ''
    },
    methods: {
      fooMethod: function () {
        alert('foo');
      },
      barMethod: function () {
        alert('bar');
      }  
    }
  })
}
<script src=".0.3/vue.js"></script>
<div id="vue-chat">
  <ul class="ments">
    <li></li>
  </ul>

  <input type="text" v-model="body" @keyup.enter="fooMethod">
</div>

I am trying to implement mon chat app on Vue.js.

window.onload = function () {
  new Vue({
    el: '#vue-chat',
    data: {
      body: ''
    },
    methods: {
      fooMethod: function () {
        alert('foo');
      },
      barMethod: function () {
        alert('bar');
      }  
    }
  })
}
<script src="https://cdnjs.cloudflare./ajax/libs/vue/1.0.3/vue.js"></script>
<div id="vue-chat">
  <ul class="ments">
    <li></li>
  </ul>

  <input type="text" v-model="body" @keyup.enter="fooMethod">
</div>

and i want to call barMethod when users press enter key and shift key at the same time.

I read docs however I could not find the way. Thank you for reading!

Share Improve this question asked Sep 30, 2016 at 3:27 asayamakkasayamakk 1631 gold badge3 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

With the shift key and other modifier keys you can see if they were pressed through the event object.

I'd use a single method with @keyup.enter and then decide to which method to call based on the event's shiftKey value.

new Vue({
  el: '#app',
  data: {
    message: 'Hi',
  },
  methods: {
    action(event) {
      if (event.shiftKey) {
        this.shiftKeyPressed()
      } else {
        this.shiftKeyNotPressed()
      }
    },
    shiftKeyPressed() {
      console.log('Shift key was pressed.')
    },
    shiftKeyNotPressed() {
      console.log('Shift key was NOT pressed.')
    },
  }
})

Here's a quick demo: https://jsfiddle/bj75cyd3/

There is no trivial way to do what you want.

You can't reach your goal through modifiers; you have to drop the .enter modifier and deal with the keyup event, as well as the keydown event.

  <input type="text" v-model="body" @keyup="keyUp" @keydown="keyDown">

There are a short answer and a long answer suggesting how to track multiple keys pressed at once in JavaScript.

Based on the answers linked above, we can build the basis of our Vue solution:

data: {
    shiftPressed: false
},
methods: {
    keyDown: function (event) {
        if (event.keyCode === 16) {
            this.shiftPressed = true
        }
    },
    keyUp: function(event) {
        if (event.keyCode === 16) {
            this.shiftPressed = false
        }
        if (this.shiftPressed && (event.keyCode === 13)) {
            this.shiftPressed = false   // avoid double trigger
            this.fooMethod()
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论