Even with the use of !event.repeat, the keyboard event still manages to trigger twice

I've been working on an interactive online piano that takes keyboard input, but I'm encountering a problem where sometimes it registers two events instead of one when a key is pressed.

To address this issue, I attempted to use e.repeat (with e representing the keyboard event) in order to only capture the initial keydown event.

If you'd like to test it out, you can visit the temporary link here:

As someone who is relatively new to JavaScript and web development, any feedback or advice on my code would be greatly appreciated.

if (!e.repeat && isValidInput(e.key)) {
    let pitch = keysMap.get(e.key);
    playNote(pitch);
    document.getElementById(pitch).classList.add("key-bkg-color"); // change color of key
}

Here's the relevant snippet of code:

let notes = document.getElementsByClassName("note");
let soften = document.getElementById("soften");
// other variable assignments and mapping details...
// remainder of the existing JavaScript code...

Answer №1

It appears that there is a significant issue with the JavaScript code. A scenario has been identified where pressing and releasing keys in a certain order can trigger unexpected events.

To address this issue, an array of pressed keys has been implemented to track key releases and ensure that the corresponding actions are executed correctly. Additionally, lowercase logic has been incorporated to account for scenarios where caps lock may interfere with the functionality of the piano.

The code snippet includes elements such as defining key mappings, loading audio files, handling keyboard input events, managing mouse interactions, labeling notes, and implementing a menu open animation.

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

External Submit button malfunctioning when attempting to submit two distinct forms

I'm currently working on a program to add items to the cart, and I need to include product attributes like colors and sizes in the process. However, I seem to be encountering an issue where only one form is submitted when using jQuery's submit(), ...

Optimizing logical expressions in C++

Can I assume in C, C++, JavaScript, or any other modern language that if I have the following code snippet... bool funt1(void) {…} bool funt2(void) {…} if (funt1() && funt2()) {Some code} ...I am guaranteed that both functions will be called, ...

Struggling to locate the text and modify the anchor tag using HTML

I am currently working with the following javascript code inside a div. I am attempting to locate and replace an entire a tag with a new type of a tag. This is the HTML that I have: <div class="info-more"><h3>We're here to help!</h3&g ...

What could be the reason for this Javascript code not functioning as intended, failing to generate a random number between 1 and 3 when I click on any of the buttons

Could someone help me with generating a random number between 1 and 3 each time I click on one of the buttons (rock, paper, scissors)? I am new to programming and not sure what I'm doing wrong. <!doctype html> <html lang="en"> <head& ...

Is it possible to access an external JavaScript file in the main.js by importing it?

In this scenario, I have defined an array of countries in an external Javascript file. const countries = [ "Australia", "Brazil", "China", "Denmark", "Egypt", "France", "Germany", "Hungary", "Italy", "Japan", ...

Ways to refresh a container

How do I update the box below... <div className="container team-member-tasks"> <header className="header-box"> <h1>Team Member Tasks</h1> ...after marking a task as complete using the script below. ...

Issue with Material UI Textfield error functionality in React.js not functioning correctly

Currently, I am working on a functional component in combination with Material UI. Within this setup, I have constructed a form comprising of 2 textfields. My objective is to activate the error property solely under certain conditions being met. However, t ...

Tips for rearranging table columns using drag and drop in react js

I have been experimenting with various libraries to create a drag-and-drop feature for changing table columns order. Here is my current implementation: import React, { useState } from 'react'; import './list_de_tournees.css' const Table ...

Launching the Node.js application on Heroku resulted in encountering an issue: "Application error - There was a problem within the application

When I access the browser using http://localhost:8080/, I can see the message Hello World with Express. I am currently trying to deploy this application on Heroku. I have followed the tutorial provided by Heroku. 1) Create a new app 2) Choose an App name ...

The React Route Component is responsible for managing all routes that start with "/", with the exception of the "/login" route

When accessing the /login route, the Dashboard component is also rendered. However, I need to exclude the Dashboard component while on the Login page. The exact attribute isn't suitable in this case because the Dashboard has nested paths such as /das ...

Creating a formatted JSON string from the data retrieved using a GET request and embedding it into an HTML template to be sent as the

Struggling to send JSON data retrieved from a GET METHOD as an email. The challenge is defining the body of the email using the obtained JSON object. Looking for solutions! Below is a snippet of my code: app.get('/userinfo',(req,res)=>{ ...

What is the best method for transforming a base64 encoded string into a base64 formatted PDF string?

Could someone please help me with a problem I'm facing? I am utilizing an AngularJS .pdf viewer that displays documents in a modal using base64. Everything works smoothly when the base64 is generated from a .pdf file. The backend (Java) generates th ...

Get an Array Using AJAX in PHP and JavaScript

Section 1 I want to retrieve an Array from PHP and use it in JavaScript. I have created a file using the fwrite function in PHP, then included that file in my next .load method inside a Div. The new PHP file contains an "include 'somefile.php';" ...

The MIME type 'text/html' is incompatible with stylesheet MIME type and is not supported

I have exhausted all possible solutions for the issue, from specifying the type for <link rel="stylesheet" href="./style.css" /> to using app.use(express.static('public')), but unfortunately, none of them seem to resolve the problem. index ...

Storing an object with the same value in AngularJS

Having an issue with pushing objects into an array using AngularJS. The problem is that it keeps storing the same value in every object, as it is an associative object. service('setAttribs',function(){ var setMapAttrib = { Processtet : {}, ...

Headers error encountered during sending request

When making a request with this method, an error labeled as "[ERR_HTTP_HEADERS_SENT] Cannot set headers after they are sent to the client" occurs. I recently started learning about express and might have made some progress. Can you please help me identif ...

Working with scrollIntoView in useEffect (Encountering an error trying to access 'scrollIntoView' property of null)

Starting from scratch, I'm working on setting up my webpage and crafting some code. function BasicDetails() { React.useEffect(()=>{ scrollView(); }, []); function scrollView() { document.getElementById('main-root& ...

Handling errors within classes in JavaScript/TypeScript

Imagine having an interface structured as follows: class Something { constructor(things) { if (things) { doSomething(); } else return { errorCode: 1 } } } Does this code appear to be correct? When using TypeScript, I en ...

Utilizing an Entity's Location for triggering an Action in AFrame

Looking to create a custom component that can track the position of a sphere within an AFrame scene and trigger an event when it reaches a specific coordinate (for example, resetting to its default position as shown below): AFRAME.registerComponent("t ...

Having trouble with RethinkDB filter not returning any matches?

Encountering an obstacle with RethinkDB while using NodeJS. Attempting to utilize a basic .filter() function, but for some mysterious reason, it fails to retrieve a result. The current code snippet in question looks like this: const someID = 1234; ...