How to make run two events simultaneously when clamping?

There are three options, 2 of which scored on keydown, which add width and height, but they both can't work, as after pressing another button without releasing the first, the first function stops
How to implement simultaneous actuation of two buttons and two functions does not matter in what order everything happens.
For example
size function(e) {
if(e.keyCode == 20) {
doc.elem.width++;
}
if(e.keyCode == 21) {
doc.elem.height++;
}
// How to do a third, where 
doc.elem.width++;
doc.elem.height+;
// trigger at the same time, the option 
if(e.keyCode == 20 && e.keyCode ==21) {
....
}
// does not work and there is also a 2nd button interrupts the first and holds the other by stopping the first
}
document.body.addEventListener("keydown", size);
June 27th 19 at 14:57
1 answer
June 27th 19 at 14:59
Solution
var events = {};
var w = 0, h = 0;

// when the button is pressed
document.addEventListener('keydown', function (e) {
 events[e.keyCode] = true; // add the button code to object
 action(); // call the function "action"
});

document.addEventListener('keyup', function (e) {
 // delete the button code from the object only when the key is released
 events[e.keyCode] = false; 
 console.log(w, h);
});

// and here now you can write instructions for any combination
function action () {
 if (events[38]) w++;
 if (events[39]) h++;
 if (events[38] && events[39]) console.log('both buttons');
}
thank you, very interesting option
only when you push one of the buttons, if both are clamped, stops the function, even if we still keep at least one sandwiched, how to be? - Ewell commented on June 27th 19 at 15:02
: Not stopping! Checked again. Everything works - Lulu.Reynolds commented on June 27th 19 at 15:05
And anyway, "stops" isn't the right word. After a keydown event while holding the key generated always new, and therefore each event function action called new. - Lulu.Reynolds commented on June 27th 19 at 15:08
: try to press one button after the second(two sandwiched) and the second to let go.
in this case, I have all the stops ( - Ewell commented on June 27th 19 at 15:11
: https://jsfiddle.net/4qn2Lnht/
here is an example, the buttons W,S,A,D
It is possible to smooth and to make this process not so clumsy that there will be a stop and traffic is rough? - Ewell commented on June 27th 19 at 15:14
:
but with such conditions
function action () {
 if (events[38] && !events[39]) console.log('first');
 if (events[39] && !events[38]) console.log('second');
 if (events[38] && events[39]) console.log('both buttons');
}


here's the log.
81dbc7ace45c44a2bd62cfee6c21263f.jpg

Clamp first, then second, then released first one, then both again, etc..

About W,S,A,D, now try to write) - Lulu.Reynolds commented on June 27th 19 at 15:17
I threw an example in that message, if you hold the first, then second and release the second clamped first nothing makes - Ewell commented on June 27th 19 at 15:20
: Wrote) A VC? - Lulu.Reynolds commented on June 27th 19 at 15:23
:
https://jsfiddle.net/4qn2Lnht/2/
here is an example, the buttons W,S,A,D
If you hold down S, after S+D, and after D to let go, but squeezed S, it stops - Ewell commented on June 27th 19 at 15:26
: https://jsfiddle.net/4qn2Lnht/3/
Here! Now certainly works, and smoothly, without breaks - Lulu.Reynolds commented on June 27th 19 at 15:29
:
cool, thanks, will learn it ) - Ewell commented on June 27th 19 at 15:32

Find more questions by tags JavaScript