简体   繁体   English

Vue - 仅当您连续按两次键时才触发事件

[英]Vue - Trigger event only if you press the key twice in a row

I'm implementing a special field in my application, when the user input the @ (shift + 50) character, a window will pop up so the user can access predefined variables...我在我的应用程序中实现了一个特殊字段,当用户输入@ (shift + 50)字符时,会弹出一个窗口,以便用户可以访问预定义的变量...

<textarea @keyup.shift.50="showWindow"></textarea>

My question is: I do not want the window to appear when the user presses the key only once, I want the window to appear when he presses the keys a twice.我的问题是:当用户只按一次键时,我不希望窗口出现,我希望当他按两次键 a 时出现窗口。

Is it possible?是否可以?

============NEW============================================ ============新====================================== ========

Edit: modified and cleaned up the code for you - this code is more efficient and does not contain any 'bugs' (there are some flaws in the other code)..编辑:为您修改和清理了代码 - 此代码效率更高,并且不包含任何“错误”(其他代码中存在一些缺陷)。

Explanation: I basically collect all 'shift.50' key presses and compare their delta - then overwrite - this means if you press 'shift.50' then dont press it again til 5 minutes later (would be the 2nd click in the old code) you would have to essentially click it 3 times to fire the 'do the needful' event..说明:我基本上收集所有“shift.50”按键并比较它们的增量 - 然后覆盖 - 这意味着如果你按下“shift.50”然后不要再按它直到 5 分钟后(将是旧代码中的第二次点击) 你必须基本上点击它 3 次才能触发“做需要”事件..

https://jsfiddle.net/yL57kbhf/ https://jsfiddle.net/yL57kbhf/

var myapp = new Vue({
  el: '#app',
  data: {
    delta: 1000, // in ms
    keyPress: null,
  },
  methods: {
    keyPressed(key) {
      if(this.keyPress !== null){ 
        let d = key.timeStamp - this.keyPress.timeStamp;
        if(this.delta > d){
            alert('do something here!')
        }
      }
      this.keyPress = key;
    },
  }
})

. .

. .

=============OLD============================================ ==============旧==================================== ========

Explanation: In the following code I keep track of how many times you click the 'shift.50' button using the data property 'pressCount'.说明:在下面的代码中,我使用数据属性“pressCount”跟踪您单击“shift.50”按钮的次数。 On the first press I store the click event in 'keyPress' in an effort to compare the delta (which is a property you can set under 'delta') with the second key press.在第一次按下时,我将点击事件存储在“keyPress”中,以便将 delta(您可以在“delta”下设置的属性)与第二次按键进行比较。 If there are 2 consecutive key presses within N (where N=delta) then you can "do the needful"..如果在 N(其中 N=delta)中有 2 次连续按键,那么您可以“做需要的”..

https://jsfiddle.net/c0tk6pbx/ https://jsfiddle.net/c0tk6pbx/

var myapp = new Vue({
    el: '#app',
  data: {
    delta: 1000, // in ms
    pressCount: 0,
    firstPress: null,
  },
  methods: {
    keyPressed(key) {
        this.pressCount++;
        if(this.pressCount === 1){
        this.firstPress = key;
      } 
      if(this.pressCount === 2){
        let d = key.timeStamp - this.firstPress.timeStamp
        if(this.delta > d){         
            alert("do something here");
        }
        this.pressCount = 0;
      }
    },
  }
})

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

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