Arduino Micro keyboard input not as expected

Hi

I've got a very simple sketch that sends a character to my PC.

The code is very simple -

#include "Keyboard.h"

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

void loop() {
delay(10000);
Keyboard.print("1");
}

I can see it appear in Notepad as expected.

However, I'd like it to work with a game so that I can control one action with a foot switch. It doesn't do what I want it to do, with occasional random inputs being seen in the game.

I still need to use the keyboard and mouse as well.

Any ideas where I'm going wrong ?

Thanks

However, I'd like it to work with a game so that I can control one action with a foot switch. It doesn't do what I want it to do, with occasional random inputs being seen in the game.

Did you use the same code to test the game? Does react the game to a "1" input?

Any ideas where I'm going wrong ?

No, the information you supplied isn't sufficient.

There's not a lot of information to go on but I suspect the the game will need the control to be held down, and Keyboard.print() doesn't do that - It sends a 'press' event and then immediately follows that with a 'release'

Have a look at using Keyboard.press() and Keyboard.release() instead.

Pseudo-code might be something like:

loop() {
   Check the physical state of the control (pressed/released)
   If the state now is different to the last time around then send press or release as required
   Record the new state
}

e2a: To test it you could do this:

#include "Keyboard.h"

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

void loop() {
  Keyboard.press("1");
  delay(5000);
  Keyboard.release("1");
  delay(5000);
}

That will repeatedly hold down and then release the key for five seconds (each).

pylon:
Did you use the same code to test the game? Does react the game to a "1" input?

No, the information you supplied isn't sufficient.

Yes, exactly the same code.

Yes, exactly the same code.

So the problem is in your computer and not in the Arduino. It acts as a USB keyboard and sends a keycode for the 1 key (on standard keyboards, note not the 1 from the numeric block!) to the computer. If your game doesn't react to that it probably expects something else. We don't know what it expects.

I had similar problems with my own project. The timing of the keypress seems to be important - not too short or too long.
Otherwise the character can get ignored or misinterpreted.
Also try adding a delay after Keyboard.begin();