Joystick WSAD - add keystroke mid loop

Hi all,

I’m new to Arduino and I’m building a DIY gamepad using the code found here: https://forum.arduino.cc/index.php?topic=531531.15 (thanks rontonomo).

I’ve modified the code slightly for IDE version 1.8.12. I’m using the Leonardo and an analog stick. I’ve got everything working as intended using the code below however I need a shift keystroke right after pressing either w,s,a and d (to make my ingame character run).

Here’s the code:

#include <Keyboard.h>

int currStateY1;
int prevStateY1;
int currStateY2;
int prevStateY2;
int currStateX1;
int prevStateX1;
int currStateX2;
int prevStateX2;

void setup()
{
Keyboard.begin();
}

void loop()
{
int sensorValueY = analogRead(A0);
int sensorValueX = analogRead(A1);

{ // Y+
currStateY1 = sensorValueY > 404;
if (currStateY1 != prevStateY1)
{
if (currStateY1 == (sensorValueY < 50))
{
Keyboard.press(‘s’);
}
else
{
Keyboard.release(‘s’);
}
}
prevStateY1 = currStateY1;
}

{ //Y-
currStateY2 = sensorValueY < 594;
if (currStateY2 != prevStateY2)
{
if (currStateY2 == (sensorValueY > 973))
{
Keyboard.press(‘w’);
}
else
{
Keyboard.release(‘w’);
}
}
prevStateY2 = currStateY2;
}

{ //X
currStateX1 = sensorValueX > 408;
if (currStateX1 != prevStateX1)
{
if (currStateX1 == (sensorValueX < 50))
{
Keyboard.press(‘a’);
}
else
{
Keyboard.release(‘a’);
}
}
prevStateX1 = currStateX1;
}

{ //X-
currStateX2 = sensorValueX < 598;
if (currStateX2 != prevStateX2)
{
if (currStateX2 == (sensorValueX > 973))
{
Keyboard.press(‘d’);
}
else
{
Keyboard.release(‘d’);
}
}
prevStateX2 = currStateX2;
}
}

I’ve tried inserting shift as a keypress and as an if statement in various places in the code but I can’t figure out what I’m doing wrong (my coding skills are not great). Can anyone point me in the right direction?

Thanks and have a nice day.

First: Edit your post and insert code tags!

I cannot find any code that sends a shift key press, post the code that should send that keystroke!

I've tried inserting shift as a keypress and as an if statement in various places in the code but I can't figure out what I'm doing wrong (my coding skills are not great). Can anyone point me in the right direction?

there is no such thing as a shift key press in the same sense that there aren't separate keys for an 'a' and 'A'. pressing the 'w' key on a keyboard produces 'w', but pressing both the shift and 'w' keys produces a 'W (capital).

do you simply need to send 'W', 'S', A', or 'D'?

there is no such thing as a shift key press

Wrong, there is such a thing. If you send a key press event with just the modifiers changed you have exactly that thing. That's why KEY_LEFT_SHIFT and KEY_RIGHT_SHIFT is defined to be used because the two shift keys can be distinguished.