Nick
Nick

Reputation: 932

How can I hold down multiple keys with `keyboard-controls` component in A-frame

I am using the keyboard-controls component in a-frame and I notice that I am unable to hold down multiple keys at the same time.

Please see below a stripped down example using the I and J keys.

Using the following scripts

 <script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script>
 <script src="https://cdn.rawgit.com/donmccurdy/aframe-keyboard-controls/a9c513fc/dist/aframe-keyboard-controls.js"></script>

The component;

AFRAME.registerComponent('keytest', {
    dependencies: ['keyboard-controls'],

    init: function () {

        //I Keydown
        this.el.addEventListener('keydown:KeyI', (e) => { 
            console.log("I Key down")
        });

        //K Jeydown
        this.el.addEventListener('keydown:KeyJ', (e) => { 
            console.log("J Key down")
        });
    },
});

And then attached to the scene

<a-scene keytest> 
</a-scene>

And here is a glitch, open the console and try holding down I and J simultaneously.

The issue is that for example, while holding down I if I then also hold down J it stops hearing the I keydown and vice versa. Is this not possible? or am I doing something wrong? I know this would be possible with a normal keydown event..

To give some context if that helps, I am making a vehicle and using the keybaord controls to power it. As it is, pressing forward/backwards accelerates the car and left/right will turn the car, but as it is, as soon as I start turning in either direction, the accelerator is stopped as it no longer hears the forward keypress.

Any advice much appreciated.

Upvotes: 0

Views: 243

Answers (1)

beefster
beefster

Reputation: 44

This is not a bug, but a slight misunderstanding of the keydown event on your part... The keydown event behavior can best be described as what you would expect to see when typing in a word processor, or notepad. if you hold down the I key you would see an I typed and then after a small delay, rapid repetition of I. Then additionally holding down J during that would stop the Is from being typed altogether, type a J, then after another small delay, rapid repetition of J.

What you should do instead, is once a single keydown event is being fired, set an internal variable to true, maybe call it something like throttle, steeringLeft, etc... then when a keyup event is fired for that key, set it back to false. have the car maneuver based on the true/false states of those variables.

Upvotes: 1

Related Questions