React to the Vue: Only activate the event if the key is pressed twice consecutively

In my application, I am creating a unique feature where users can trigger a window to appear by inputting the symbol @ (shift + 50). This will allow them to access predefined variables...

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

My query is: I want the window to only pop up when the user presses the keys twice in a row, not just once. Is this achievable?

Answer №1

============REVAMPED========================================

Edit: I have significantly improved and optimized the code for better performance. The modified code does not contain any 'bugs' and operates more efficiently compared to the previous version.

Explanation: The updated code now tracks all 'shift.50' key presses and compares their time intervals. It ensures that if you press 'shift.50', you need to wait at least 5 minutes before pressing it again to trigger the 'do the needful' event. This modification requires three consecutive clicks of 'shift.50' to activate the event.

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;
    },
  }
})

.

.

=============ENHANCED=========================================

Explanation: The original code stored the number of times 'shift.50' button was clicked in the property 'pressCount'. By comparing the time interval between two key presses with the specified 'delta' value, it determined whether the action should be triggered. However, the revised code provides a more streamlined approach to achieve the same functionality with improved efficiency.

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;
      }
    },
  }
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps to displaying a genuine Docx file within a Material CardMedia

Currently, I am facing an issue with positioning a docx file in my app. Interestingly, jpg and mp4 files are displaying correctly, but the docx file is not positioned as expected. If you want to try it out, simply open a doxc file. In the FileContentRend ...

Implementing a Tab on Firefox Extension Upon Window Load

I have a requirement to add a tab whenever a new Firefox window is loaded for my bootstrap extension. Below is the code snippet I am using: var WindowListener = { setupBrowserUI: function(window) { window.gBrowser.selectedTab=window.gBrowser.a ...

Divide the string by spaces

One of the challenges I am facing involves a textarea where users can input messages. The goal is to split the message into an array of words after detecting an '@' symbol, and then search for specific words in that array such as @person1 and @pe ...

Algorithm for File Naming

Given an array of desired file names in the order of their creation, where two files cannot have the same name. If a file has a duplicate name, it will be appended with (k), where k is the smallest positive integer that creates a unique name. Output an ar ...

Vue components not being highlighted within template section of Visual Studio Code

I previously had all my imported components within the displayed in a special color compared to other traditional HTML components. However, this feature no longer seems to be functioning. I have already attempted uninstalling and reinstalling all extensi ...

I would appreciate your assistance with the hide button

Is there a way to hide a button after clicking on it? I would greatly appreciate your help! ...

It appears as though the promise will never come to fruition

I am currently developing an application that is designed to search for subdomains related to a specific domain and store them in a database. The application retrieves data from crt.sh and threatcrowd. One of the functions within the application parses th ...

Sharing a Twitter thread using Twit library with Node.js

I have been using Node.js along with the npm Twit module to post tweets on Twitter, and while it works for a single tweet, I am facing issues when trying to post multiple tweets as a thread. When attempting to post a series of tweets together, they do not ...

Determining the browser width's pixel value to enhance responsiveness in design

Lately, I've been delving into the world of responsive design and trying to grasp the most effective strategies. From what I've gathered, focusing on content-driven breakpoints rather than device-specific ones is key. One thing that would greatl ...

How to retrieve values from HTML class names using Javascript for loops but encountering issues

foreach($products as $row){ <input type="hidden" class="prodId" name="id" value="<?php echo $row['id']; ?>"> <input type="hidden" class="prodUnique" name="unique" value="<?php echo $unique; ?>"> <button id="added" ...

Include an item in a JSON structure

I have a settings.json file that contains the following data (where 123456789 represents a unique user id): { "123456789": {"button_mode":true} } My goal is to add a similar id: {button_mode: value} object to this JSON file if there is no existing en ...

Validating date inputs with ng-change in AngularJS

I am currently utilizing AngularJS along with AngularJS bootstrap within my webpage. One of the components I have is a date picker directive that is structured like this: <div class="form-group {{dateStatus.class}}"> <p class="input-g ...

Unable to access object key data from JSON-SERVER

I am currently attempting to send a GET request to json-server in order to retrieve data from a nested object. However, the response I am receiving is empty instead of containing the desired data key. After thoroughly reviewing the documentation, I could ...

Using Ajax/jQuery in combination with Mongodb

My experience with Ajax/jQuery is fairly new. I am currently working on creating a sample HTML page using Ajax/jQuery to retrieve all customers and search for a customer by ID. Each customer has three variables: ID, firstName, and lastName. I am looking t ...

Is there a way to modify Style Properties in JavaScript by targeting elements with a specific class name using document.getElementsByClassName("Class_Name")?

I am seeking a solution to change the background color of multiple div boxes with the class name "flex-items" using JavaScript. Below is my current code: function changeColor(){ document.getElementsByClassName("flex-items").style.backgroundColor = "bl ...

Error: express is missing a closing parenthesis for the argument list

When running this code in the VS Code terminal, be sure to verify any errors that may occur. var express = require('express'); var app = express(); app.get('/', function(request, response) { response.send("hello world"); }); app.li ...

What is the most effective method for retrieving a key and value from an Axios response object?

I currently have a Mongoose schema set up to store key:value pairs in a mixed type array, represented like this: Mongoose const budgetSchema = new Schema({ earnings: Number, expenses: [mongoose.Schema.Types.Mixed] }); budget:{ earning:1000, exp ...

While using jQuery, it was discovered that the name attribute could be changed within the .each() function when working in console.log. However

I'm attempting to modify the name attribute of a cloned table row with the following code: var ipCount = 2; $("#input_form").on("click", "#add_input_param", function() { $('#input_param tr').eq(1).clone().find('input').val ...

Having an issue with button onClick functionality not working in Javascript

Simple enough. I am struggling to get my button to generate the necessary input upon clicking. The fields should populate where the "household" class is located. I am limited to editing only Javascript and not HTML. Any suggestions? HTML: <ol ...

Issues with using a personalized font in a Stenciljs project

Looking for guidance on implementing a custom font in my Stenciljs app. I have the otf file, unsure if an npm package is necessary. Here's my code: filestructure: -src --components --assets ---Anurti-Regular.tiff ---Anurti-Regular.ttf friends-l ...