Detect whether an arrow key is being pressed

How do I…

The arrow keys (NORTH, SOUTH, EAST, WEST) in my simulation are supposed to cause a zero-gravity rigid body to self-rotate and move according to the Math.sin(r) and Math.cos(r) functions.

Right now I am finishing the self-rotating part, but the arrow keys are not being checked. How do I check that an arrow is being pressed in the event chart?

What is the expected result

Pressing the up arrow for example should set a player’s variable to zero

What is the actual result

The player does not rotate because the arrow keys are not checked for in the GDevelop engine

Related screenshots

Welcome hydroperfox. It looks like your conditions and actions are all in the same event. Events are processed as a whole from top to bottom. In this case, all of the arrow keys would have to be pressed at the same time and then it would trigger all of the actions.

Each condition and action pair in this scenerio need to be placed into separate events so they can be processed separately.

By default all of the conditions in an event need to be true unless the conditions are within an OR condition. You can use any number of conditions and actions but all of the conditions would have to be true.

1 Like

Thanks @Keith_1357! I see… I’ve done that, divided the conditions into separate events. I wanted to use a logical OR, but since I have setup the conditions like that, I just separated the press and release conditions as well.

Now I do only have two problems:

  • the turn logic change abruptly for some cases
  • the player may release with a (NORTH|SOUTH)+(WEST|EAST) combination, like SOUTHEAST; when all keys are released, it should only be one of (NORTH, SOUTH, WEST, EAST).

This is the turn logic (where I also want to put the movement logic together):

const NORTH_ROTATION = 0;
const NORTHWEST_ROTATION = 315;
const NORTHEAST_ROTATION = 45;
const SOUTH_ROTATION = 180;
const SOUTHWEST_ROTATION = 225;
const SOUTHEAST_ROTATION = 135;
const WEST_ROTATION = 270;
const EAST_ROTATION = 90;
const TURN_SPEED = 5;

const player = objects[0];
const player_vars = player.getVariables();

let a = lock360Degrees(player.getAngle());
let b = player_vars.get("TargetRotation").getAsNumber();

if (a != b) {
    // Rotate either clockwise or counterclockwise for the
    // shortest route.

    let ab = a - b; /* counterclockwise */
    let ba = b - a; /* clockwise */

    // For the lower delta, add 360.
    if (Math.min(ab, ba) == ab) {
        ab += 360;
    } else {
        ba += 360;
    }

    // Rotate
    if (ab < ba) {
        // counterclockwise
        a -= TURN_SPEED;
        a = lock360Degrees(a);
        a = a < b ? b : a;
    } else {
        // clockwise
        a += TURN_SPEED;
        a = a > b ? b : a;
    }

    player.setAngle(a);
}

// Lock degrees to be between 0 and 360.
function lock360Degrees(a) {
    a = a < 0 ? 360 - (-a % 360) : a;
    return a % 360;
}

const movingNorth = player_vars.get("MovingNorth").getAsBoolean();
const movingSouth = player_vars.get("MovingSouth").getAsBoolean();
const movingWest = player_vars.get("MovingWest").getAsBoolean();
const movingEast = player_vars.get("MovingEast").getAsBoolean();

// Set target rotation
if (movingNorth) {
    player_vars.get("TargetRotation").setNumber(movingWest ? NORTHWEST_ROTATION : movingEast ? NORTHEAST_ROTATION : NORTH_ROTATION);
} else if (movingSouth) {
    player_vars.get("TargetRotation").setNumber(movingWest ? SOUTHWEST_ROTATION : movingEast ? SOUTHEAST_ROTATION : SOUTH_ROTATION);
} else if (movingWest) {
    player_vars.get("TargetRotation").setNumber(WEST_ROTATION);
} else if (movingEast) {
    player_vars.get("TargetRotation").setNumber(EAST_ROTATION);
}

Are you making it all in Javascript or are you converting it from Javascript? Or a combination?
I’m a bit confused.

I have asked some questions past year about all this and used some of the logic:

I found it easier to do it in JavaScript since I’m not very used to visual programming. (My first visual programming was in Construct 2, but that’s long ago…)

About the inertia to prevent things like SOUTHEAST, I solved it with:

// Inertia
if (!(movingNorth || movingSouth || movingWest || movingEast)) {
    if ([NORTH_ROTATION, SOUTH_ROTATION, WEST_ROTATION, EAST_ROTATION].indexOf(b) == -1  && b != 0) {
        b -= 45;
        player_vars.get("TargetRotation").setNumber(b);
    }
} 

Do you need help with the Gdevelop events or the Javascript? I’m still unsure. I’m not as proficient with Javascript although I can understood the basics of what’s happening.

I’m getting the impression that it makes the player slowly turn towards the new direction instead of immediately.

Alright, I should open a new question then