Utilizing Vue.js for Keyboard Binding

I'm brand new to coding and Vue, and I've hit a roadblock in my project.

I'm creating a JavaScript calculator that needs to be usable both with a mouse and keyboard. While I've managed to make it work with the mouse, I just can't figure out how to get it working with keyboard input. Here's my code. Can someone please assist me?

I understand that I need to somehow bind the keyboard input to the input field, but I'm struggling to make it function correctly.

<template>
  <v-app >
    <div class="cont" >
      <div class="name">
        <h1>Vue Calculator</h1>
      </div>
      <div class="calc_display">
        <input type="text" v-model="result" @click="keyboardInput" class="calc_result text-sm-center" >
      </div>
      <div class="buttons">
        <v-btn @click="clear" color="red darken-1" class="btn">CE</v-btn>
        <v-btn @click="square" color="light-blue lighten-1" class="btn">&radic;</v-btn>
       ...
      </div>
    </div>
    <v-card-actions class="primary lighte-2 justify-center white--text">
      &copy;2018 — <strong>Vue calculator</strong>
    </v-card-actions>
  </v-app>
</template>

<script>
  export default {
    data() {
      return {
        firstNumber: null,
        result: '',
        operator: null,
        operatorClicked: false,
        sign: '',
      }
    },
    methods: {
      ...
      //THIS IS THE CODE THAT I CAN NOT MAKE IT WORK
      keyboardInput(key) {
       ...
      }
    }
  }

</script>

`

Answer №1

Make sure to utilize the keyboardInput function by incorporating an actual key press event. Connect it properly by following these steps:

<div class="cont" @keyup="onKeyPress">

Then, implement the following code:

methods: {
  onKeyPress(event) {
    this.keyboardInput({ which: event.keyCode })
  }
}

If you're encountering any issues with the current setup of keyboardInput, I recommend revisiting its implementation for better clarity :)

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

Storing and Retrieving Mongoose Data Using Nested Schemas, References, and Promise Functions

I have a question regarding saving and retrieving documents with nested schema references in Mongoose. When I try to save a document with nested schema refs, and then retrieve it later, the required nested field is not included unless I populate it in the ...

"Enhance Your Online Shopping Experience with Woocommerce Ajax Filters and

I have a structure that looks like this: Company Google Apple Microsoft Material Wood Metal Plastic Essentially, Brand & Material are attributes in the Woocommerce system, while the rest are variables. I am currently working on implementing AJAX fi ...

Prepared SQL Statement in NodeJS for MSSQL using WHERE IN clause

I'm using the sql npm package in my Node.js project. Currently, I have an array of product SKUs like this: var skus = ['product1', 'product2', 'product3']; The SQL query stored in a file looks like this: SELECT * FROM ...

Is it possible to apply styles to the body of a document using styled-components?

Is it possible to apply styles from styled-components to a parent element, such as the <body> tag, in a way that resembles the following: const PageWrapper = styled.div` body { font-size: 62.5%; } ` ...

Transform an array containing arrays into an array of individual objects

After spending a considerable amount of time trying various solutions, I am still unable to achieve the desired result. The issue lies in getting an array of objects from service calls and organizing them into a single array for data loading purposes. The ...

Retrieve a file through a POST request with node.js

Hi everyone, I'm trying to create a "export to excel" functionality in Node.js for a page. By using Chrome DevTools, I discovered that the file is downloaded after a post request, so I need to replicate it. var request = require('request' ...

Assigning attributes to each letter in a pangram

I am attempting to assign the appropriate class to a group of elements, representing each letter in the alphabet. The elements have IDs ranging from #alpha_0 to #alpha_25. If a letter appears just once in the input, it should be displayed in green. If a le ...

Guide to establishing multiple relationships using the `belongsTo` method within a Loopback model

In my database, I have three models named "Companies," "Employees," and "Employments." The "Employments" model is set up to have a belongsTo relationship with both a company and an employee. Inversely, every "Employee" should have a hasOne relationship wit ...

Introducing an alternative solution for handling tasks linked to the completion of CSS3 animations

In order to incorporate smooth CSS3 animations into my website, I have implemented the animate.css library created by Dan Eden. One particular scenario involves a loading screen that features a fade-out animation. It is crucial for this animation to comple ...

What is the best way to display an image path and add it to the Ajax success function in a CodeIgniter application?

I am struggling to display my image path correctly using append and a variable to store the value. However, whenever I try, it results in an error. Let me provide you with the code snippet: <script type="text/javascript"> $(document).ready(funct ...

Issues with Bootstrap-slider.js functionality

I'm having trouble getting the bootstrap-slider.js library from to function correctly. All I see are textboxes instead of the slider components. You can view an example at I have verified that the CSS and JS files are pointing to the correct direc ...

In a carousel slider, the height and width of divs are not set to specific dimensions

For a code snippet, you can visit this link: here The html: <html lang="en"> <head> <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700,700i|Merriweather:300,400,700" rel="stylesheet"> <link href="https://ma ...

How can you make a Tempory Drawer wider in Material Components Web?

Increasing the Width of a Temporary Drawer in Material-components-web Greetings! I am currently facing an issue with Material-components-web In the provided demo here, I would like to extend the width of the Temporary drawer to accommodate additional co ...

The FormData() object in Django backend is consistently found to be void of any data

I am currently experimenting with uploading an HTML form through AJAX using pure JavaScript, without jQuery. The form is put together in my template by combining three components: the CSRF token, a ModelForm, and a regular Django form (forms.Form). The vis ...

What is the reason behind appending a timestamp to the URL of a JavaScript resource?

$script.ready('jui',function() { $script('<?php base_path(); ?>js/partnerScripts.js?ts=1315442861','partners'); }); Can anyone explain why there is a fixed ts=timestamp at the end of the partnerScripts.js file name? I ...

Node.js video tag requests minimal data transfer on mobile devices

After creating a node server for streaming mp4 videos from a Mongo database using gridfs, I encountered an issue. The videos stream perfectly to desktop browsers, but when attempting to stream to a mobile device, the video player shows up, but the video do ...

Upon selecting the correct prompt, it fails to respond when I press enter

I attempted to use a multi-search script that I found on a website. I followed the instructions given and made the necessary changes. After saving the file as an HTML document, I opened it in Chrome. However, when I type in a word and hit enter, nothing ha ...

The button should only be visible when the input is selected, but it should vanish when a different button within the form is

Having an issue with displaying a button when an input field is in focus. The "Cancel" button should appear when a user interacts with a search bar. I initially used addEventListener for input click/focus to achieve this, but ran into a problem: on mobile ...

Bootstrap - creating seamless navigation between accordion sections on a single webpage

I am currently attempting to create internal links within an accordion menu on the same page. Here is the code I have been working with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> & ...

How to display an element using an if statement in Next.js

I am attempting to incorporate a parameter into a nextJS component that will only display if a certain condition is met. At the moment, my code looks like this: return ( <div role="main" aria-label={this.props.title} classN ...