How do I detect the " * " key in a keyEvent?

    if(keyEvent.keyCode == 8){
        $scope.erase();
    }

    else if(keyEvent.keyCode === 107){
    console.log("+");
    $scope.inputToCal('+')
    }

    else if(keyEvent.keyCode === 109){
    console.log("-");
    $scope.inputToCal('-')
    }

    else if(keyEvent.keyCode === 16){
    console.log("*");
    $scope.inputToCal('*')
}
    }

    else if(keyEvent.keyCode === 111){
    console.log("/");
    $scope.inputToCal('/')
    }

I attempted to create a calculator that captures keydown events.

I am able to capture the keys for addition, subtraction, and division.

The key codes for these operations are "107," "109," and "111" respectively.

Despite this success, there is one issue I encountered.

The key "*" cannot be captured.

Is there a solution to this problem?

Alternatively, do I need to create a new array to capture the keys "Shift" (keyCode:16) and "8" (keyCode:56)?

I apologize for my lack of expertise in handling keyEvents. I eagerly await a solution... Please assist me!

Answer №1

Make sure to handle the shift key press separately in your code.

function detectShiftKey(e) {
 var isShiftPressed = 0;
 var event = (e == null ? event : e);
 isShiftPressed = event.shiftKey;
 if ((isShiftPressed) && (event.keyCode == 56)) 
   alert("You pressed the * key");
 return true;
}

Check out this demo for reference: http://jsfiddle.net/soxj0v54/1/

Incorporate the following code snippet into your existing code-

else if(keyEvent.keyCode === 56 && keyEvent.shiftKey){
    console.log("*");
    $scope.inputToCal('*')
}

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

Using JSON objects effectively in MVC4, including parsing them seamlessly!

I am struggling with understanding how to effectively utilize JSON objects within MVC and the correct way to pass them from Controller, to View, to Jscript. I am also unsure if I am correctly parsing the JSON objects at the appropriate places... Within m ...

Utilizing the CSS 'overflow: hidden' property and jQuery to restrict users from scrolling during a loading page

OBJECTIVE I aim to restrict the user from scrolling while the page is loading. ISSUE The snippet of code provided successfully prevents the user from scrolling during the additional 2 seconds of the loader animation: $('body').toggleClass(&ap ...

Is there a way to create a dropdown selection for font families with vue js?

Here is a select element with font families that I want to apply to my texts: <select v-model="focused_font"> <option value="" disabled selected>Font</option> <option v-for="font in available_fonts" ...

Angular2 - Transforming SVG elements with dynamic styles using ng-style

I'm trying to create SVG lines using ng-repeat and need to adjust the translation of each line. However, I'm having trouble getting the style to apply using ng-attr-style. my-component.js: import {Component} from 'angular2/core'; @Co ...

What is preventing me from accessing a dialog form modal through a button using JavaScript with jQuery?

There is jQuery and Bootstrap code on the same page, let's call it index.html This is my unique content <button id="btncreate" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Create</button> I am having trouble acces ...

"React Component - Common Mistakes in Utilizing Input Fields in Forms Without Effectively Implementing Use

import { useEffect } from 'react'; import AddRecordButton from './AddRecordButton'; const AddRecordForm=()=>{ const [content, setContent]=useState([]); const [artist_name, setartist_name] = useState(''); const [album_name, ...

Is there a way to retrieve files stored on my hard drive within a webpage?

I have a large number of files stored on my hard drive, all following the same format, 2014.C1.012.012 - full name of the file. They each have unique numbers and names. I want to create a local webpage where I can organize these files into a table w ...

Challenges arise when adjusting frame size for mobile and desktop devices

I am trying to achieve an auto-resize feature for my frame's height when the screen width is smaller than a certain value. I want it to behave like Sketchfab, as demonstrated in this video: http://www.youtube.com/watch?v=_y5ckVFGHKU&feature=youtu. ...

What are some ways to escape an ng-repeat loop?

Instructions for breaking out of a specific item in an ng-repeat loop <div ng-init="myarr=['abc','xyz','pqr','mno','rst']"> <div ng-repeat="item in myarr"> {{item}} ...

Save the status of checkboxes in a JSON file using boolean values of true or false

When a user submits a form, I am retrieving the values of checkboxes and storing them as an array. The simplified form structure is as follows: <!-- gym_create.html - other input fields are omitted for brevity --> <form class="create-gym" role=" ...

Learn the process of zipping a folder in a Node.js application and initiating the download of the zip file afterwards

After encountering issues with the latest version of the npm package adm-zip 0.4.7, I reverted to an older version, adm-zip 0.4.4. However, despite working on Windows, this version does not function correctly on Mac and Linux operating systems. Additionall ...

If a div element includes a specific text, then update that portion of the text without altering the value of any variables

Need help removing text without affecting variables Attempting to translate a specific line with JQuery Looking to change text only, leaving the content within the strong tag intact $('.content-box__row div:nth-child(3)').text('') ...

Object displaying no visible illumination

Recently, I've been experimenting with this project, and after some trial and error, I've managed to get things working to some extent. As a novice in JavaScript, I'm unsure if the issue I'm facing has a simple solution. The problem is ...

ngOptions not updating the selected item in the select dropdown

My Angular code is not reflecting the selected option in the select input. Even though I have specified Hospital One to be selected, it's not happening. Can anyone figure out what might be causing this issue? I've also shared the same code snipp ...

Is it possible to transform a .csv document into a JavaScript array with dictionaries? Each dictionary's keys would correspond to the column headers in the .csv file, and the values would be the

Let's imagine a scenario where I have a .csv file with the column headers listed in the first row, and their respective values are provided in the subsequent rows as shown below: index,id,description,component,service 0,5,lorem ipsum,7326985,Field Ser ...

What is the reason for object destructuring not functioning properly in styles?

Why is it that the HTMLelement is considered an object, while the style is also an object, yet this code fails to work as expected? When I remove destructuring, everything functions properly, so I am curious to understand the underlying reason behind thi ...

Link clicking does not trigger URL routing properly

I've been tasked with updating our web application, and I'm facing a challenge that I need help with. My boss wants users to be able to click on a link in an email and have the internal company web app directly navigate to a specific page indicat ...

An advanced password checker that notifies the user of any spaces in their password

I need help fixing my JavaScript code. The program should prompt the user for a password using a dialogue box, and then validate that the input has no spaces. If a space is detected, the program should stop and display an alert saying "Invalid, contains a ...

Troubleshooting: Vue.js not triggering method after watching object

I am in need of watching a prop that is an object, so here is my script: <script> export default { watch:{ filter: { handler:(newval)=> { console.log("I have new data",newval) //this works thi ...

Troubleshooting Firebase AppCheck v8 in React: Encountering a 400 error message stating "App ID is Invalid: 'undefined'"

I've been attempting to integrate appCheck into my Firebase project. Despite following the instructions in the Firebase documentation and consulting several StackOverflow posts, I'm encountering difficulties getting it to function correctly. When ...