Hello,
I’m trying to figure out how to create input system without being linked to a specific keyboard layout.
Let’s say that this is my main script to move inside an update:
// calculate force based on pressed keys
if (this.app.keyboard.isPressed(pc.KEY_A)) {
moceX = -this.speed;
}
if (this.app.keyboard.isPressed(pc.KEY_D)) {
moveX += this.speed;
}
if (this.app.keyboard.isPressed(pc.KEY_W)) {
moveY = -this.speed;
}
if (this.app.keyboard.isPressed(pc.KEY_S)) {
moveY += this.speed;
}
This is only working on a QWERTY layout keyboard. If used with an AZERTY, it won’t work.
I didn’t find any posts, tutorials, documentation about this.
If based on simple way, i would do it with this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
And inside the function to use a switch on event.code.
But with this, the movement is really not fluid and laggy.
Is there just some other way around to do this ?
I think the best way to do this would be to have the player manually select which keyboard they are using which would either change a variable check with different keys in the same script or just use a different script entirely
Or are you saying that the code literally just doesnt work at all on other keyboards
The code work on other keyboard layout.
Just that on Azerty/qwerty, the letter W and Z are reverted. Same for Q and A. Which makes it harder to control.
This code is expecting letters WASD.
What I want to do is not expecting letters. But expecting location of the key on the keyboard. This way all kind of keyboard layout will work.
Giving the player the choice to choose is not good.
In pure JS i will be able to do it easily.
Other than just letting the player bind whatever keys they want to im not sure how to help
I would honestly do this in pure JS and create your own global input manager. If you look at the keyboard module for PlayCanvas, it’s pretty trivial in how it checks for presses etc
It just listens for the browser events and stores the state in a JS object.