In JavaScript, if you press two keys down at the same time and then release one of them, it can affect the listener of the other key

I am currently developing a webGL game with the Three.js library that involves movement in space using keyboard arrows. I am working on allowing users to simultaneously move and rotate by pressing the left-right arrow keys together with the up-down arrow keys. To achieve this, I have created a key map that keeps track of the status of all necessary keys. Here is the code snippet:

var keyMap = [];
document.addEventListener("keydown", onDocumentKeyDown, true); 
document.addEventListener("keyup", onDocumentKeyUp, true);
function onDocumentKeyDown(event){ 
    var keyCode = event.keyCode;
    keyMap[keyCode] = true;
    executeMovement();
}
function onDocumentKeyUp(event){
    var keyCode = event.keyCode;
    keyMap[keyCode] = false;
    executeMovement();
}
function executeMovement(){
    var cameraNeck = cameraEquipment.getNeck();
    if(keyMap[37] == true){
        //rotate left
    }
    if(keyMap[39] == true){
        //rotate right
    }
    if(keyMap[38] == true){
        //move front
    }
    if(keyMap[40] == true){
        //move back
}

This setup allows for simultaneous movement and rotation when two keys are pressed at once. However, there is a notable issue. If a user presses the up arrow first followed by the left arrow (without releasing the up arrow), everything functions as intended and the user rotates to the left. But if the user releases the last pressed button (left arrow), both movement and rotation halt.

It appears that in the sequence of key presses, triggering a 'keyUp' event for one button may also trigger a 'keyUp' event for another button. This behavior has led me to question the overall implementation and debugging strategies employed thus far.

Answer №1

Upon close examination, it was evident that the map responsible for storing key status did not have any errors in its implementation and functioned correctly. The issue lied within the executeMovement() function, which was only triggered once during a 'keydown' or 'keyup' event. Consequently, this caused the user to halt movement with one key still pressed.

To address this issue, the executeMovement() function needed to be invoked from the function executed when the browser refreshed the frame. The revised code snippet is as follows:

var keyMap = [];
document.addEventListener("keydown", onDocumentKeyDown, true); 
document.addEventListener("keyup", onDocumentKeyUp, true);
function onDocumentKeyDown(event){ 
    var keyCode = event.keyCode;
    keyMap[keyCode] = true;
}
function onDocumentKeyUp(event){
    var keyCode = event.keyCode;
    keyMap[keyCode] = false;
}
function executeMovement(){
    \\same code
}

var render = function () {
    requestAnimationFrame( render );
    renderer.render(scene, cameraEquipment.getCamera());

    executeMovement()
};
render();

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

After reloading the data tables, analyze the information and dynamically highlight any rows in red that remain unchanged

I have a table that is refreshed every 5 minutes using ajax.reload(). One of the rows in this table is labeled as CIP. My goal is to highlight the CIP row on each refresh where the value remains unchanged from the previous value (value received in the la ...

Having trouble executing `npm start` command

After running npx create-react-app and then npm start, I encountered an error message https://i.sstatic.net/Uj5EC.png. Despite following the suggested solutions, the error persists. Any idea why this is happening? ...

Vue displaying element tags without proper color rendering

Currently, I am using Vue3 in VS Code and encountering an issue where the colors of the Vue tags appear as white, making it difficult for me to differentiate between the template, script, and style tags. If you would like to take a look at the code sample ...

Learn how to define matching properties in an enum and object in React using Typescript

Here is an enum I have defined: enum INFO_CODES { goToInit = 'goToInit', lockIp = 'lockIp', } I am looking to create an object (or another enum) with the same properties, which are only goToInit and lockIp: const SOME_OBJ = { goToInit ...

"Enhance your website with a dynamic animated background using Bootstrap container

I'm struggling to understand why the Bootstrap container is blocking the particles-js animation behind the text. I want the background surrounding the text to be animated as well... :/ Example Code: .gradient-bg { background: rgba(120, 87, 158, ...

Show PHP results in an Ajax modal box

Hey everyone! I'm new to this and had some code written for me by a programmer. Unfortunately, I can't reach out to them for help anymore. I have a calculator that shows results (generated by calc.php) without refreshing the page. Check out the ...

Unfortunately, the jquery Calendar plugin is malfunctioning

After much consideration, I decided to implement this calendar plugin on my website Plugin Unfortunately, it isn't working as expected. I am encountering the following error: $(...).pignoseCalendar is not a function at HTMLDocument. (Index ...

Confirming Bootstrap - Implement an onConfirm method for element attributes

I am looking to add a JavaScript function to my confirmation button. I have created the elements off-page and fetched them using ajax, so it is crucial that I can set the function in-properties rather than via $('#id').confirmation() So far, I h ...

Discovering the size of content through solely the height and width of a div: Is it possible?

Can someone help me figure out how to calculate the content length based solely on the height and width of a div? For example, if a Div has a height:200px & width:200px, I would like to know that the stored content length is 298. function calculateCo ...

Challenges with the insertedCount in Express and MongoDB

I am facing an issue with using "insertedCount" in MongoDB in a NodeJS project. Upon calling the "insertOne()" method, the const assigned does not allow me to access the insertedCount value as it returns "undefined". Below is my app.js code: const { MongoC ...

The console is not displaying the data from the useForm

I am working on a project to create a Gmail clone. I have implemented the useForm library for data validation. However, when I input data into the form and try to print it to the console, nothing is being displayed. const onSubmit = (data) => { ...

The issue with ng-if not functioning within ng-repeat is that the ng-if directive

My issue is with using ng-if inside an ng-repeat in AngularJS. Despite updating to the latest version on 09/27/2014, I am still unable to make it work properly. The code functions perfectly outside of ng-repeat, and also works fine inside ng-repeat when us ...

Merging two arrays together to create a new array of objects while keeping track of and tally

I'm facing a challenge trying to merge two arrays into an array of objects. For example: arr1 = [a,b,c]; arr2 = [a,a,a,b,b,c,d,d]; The desired combination: combinedArr = [ {name: a, amount: 3}, {name: b, amount: 2}, {name: c, amount ...

What could be causing the buttons in this JavaScript trivia game to consistently show the wrong answer even after selecting the correct one?

My latest project involves creating a trivia game using vanilla JavaScript and Bootstrap. The game fetches questions from the API, displays the question along with four multiple choice answers on separate buttons using Bootstrap. To ensure the buttons are ...

Displaying a dialog or popup in React Native when catching errors

Exploring React Native has been quite a journey for me, especially when trying to implement a popup for error handling. Despite numerous attempts, I haven't been successful in getting it to work as desired. The code snippet below captures my current i ...

Please reset the form fields after the most recent edit

I've created a form that includes multiple select elements. When an option is selected, it activates the next select element and updates it with values using Ajax and PHP. However, I'm facing an issue where changing a previous option only resets ...

Integrate the elements from the <template> section into the designated <slot> area

I am trying to retrieve template content, insert it into a custom element with shadow DOM, and style the span elements inside the template using the ::slotted selector. However, it seems like this functionality is not working as I expected. <!doctype h ...

Adding a nested data structure in a Meteor application

In my mongo collection, I have a document named exam. // meteor:PRIMARY> db.exam.find() { "_id" : "RLvWTcsrbRXJeTqdB", "examschoolid" : "5FF2JRddZdtTHuwkx", "examsubjects" : [ { "subject" : "Z4eLrwGwqG4pw4HKX" }, ...

What is the best way to automatically update a list of posts using vuex?

Currently, I am working on a project that utilizes Laravel and Vuejs to function as a Single Page Application (SPA). Within the admin page where I manage posts, I display a list of posts from the database using Vuex and Axios requests. There is also a butt ...

Error: Unable to locate module: 'material-ui/Toolbar'

Encountering Error While Using Material UI Recently, I attempted to utilize the ToolBar and AppBar components of React Material UI. Unfortunately, I encountered an error message stating: "Module not found: Can't resolve 'material-ui/core/Toolbar ...