Arduino pro micro not sending the correct keystrokes with keyboard.h

Hi everyone, I'm currently working on a 2-key keypad to play rhythm games using a Pro Micro with the keyboard.h library.
I finished yesterday the coding, but I have an issue: one key should send the "x" keystroke an the other one the "z" one, but instead the x one sends LShift + "/" keystrokes and the other one sends "+" keystroke.
How do I solve this?
Here is the code:

#include <Keyboard.h>
#include <KeyboardLayout.h>
#include <Keyboard_da_DK.h>
#include <Keyboard_de_DE.h>
#include <Keyboard_es_ES.h>
#include <Keyboard_fr_FR.h>
#include <Keyboard_it_IT.h>
#include <Keyboard_sv_SE.h>


//Código propio
#define botonZ 16
#define botonX 14
#define ledZ 10
#define ledX 6
#define ledG 7

int pressX = 0;
int pressY = 0;
int pressG = 0;

void setup() {
  //Setup propio
  pinMode(ledZ, OUTPUT);
  pinMode(ledX, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(botonZ, INPUT);
  pinMode(botonX, INPUT); 
  Keyboard.begin();
}


void loop() {
  int z = digitalRead(botonZ);
  int x = digitalRead(botonX);
  
//Configuración del botón/switch que emulará el keystroke "z"

  if(z==1){
    digitalWrite(ledZ, HIGH);
    Keyboard.press("z");
  }
  else{
    digitalWrite(ledZ, LOW);
    Keyboard.release("z");
  }

//Configuración del botón/switch que emulará el keystroke "x"
  if(x==1){
    digitalWrite(ledX, HIGH);
    Keyboard.press("x");
  }
  else{
    digitalWrite(ledX, LOW);
    Keyboard.release("x");
  }

}

Does it actually send two keystrokes for the "x" or does it send the keystroke for the character you get if you push shift and "/" on your keyboard?
What keyboard layout are you using? The default is the US keyboard layout. If you're using another one you have to specify that.

it is sending two keystrokes at the same time, as shown in this image:

Also, when i try using the spanish layout, it gets even worse; it happens the same but with "=" instead of "/" and it presses shift about 15 times per press

this is what the input looks like when using the spanish layout:

How do you know that? Do you sniff the USB bus?

For that you use the Spanish layout on which side? The Arduino or the PC side?

thats on the arduino side

damn.
I just use the app shown in the images and sends a lot of times that input

We have no clue what app that is. Where is the link to it? Is that app capable of debugging USB stuff? Have you tried WireShark?

I

I don't think so

I'll try it, how does it work?

Then you don't know if the Arduino is the problem.

There is a ton of documentation on that site. Basically let it sniff on the USB interface you connect the Micro to and it will show you what the Arduino sends. I would expect you to see 0x1b and 0x1d scan codes. If these codes are transferred but you see something different on you PC you have to fix your PC software.

Ok, so I have installed wireshark and USBPcap.
I opened the app and entered in the USBPcap1, which is the one where my pro micro is connected, and it looks like this

You get that output when you hit one of the two buttons on your board?

Save the pcap file and post it once you done above so we can investigate.

  1. Keyboard.press() and .release() take a character, not a string. Change "x" and "z" to 'x' and 'z'.

  2. Remove useless includes, <Keyboard.h> is all you need.

  3. The sketch spams keypresses/releases as fast as it can. Slow down!

1 Like

Just changing the double apostrophe to a single apostrophe worked.
Thank you so much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.