Simulated Keyboard from Teensy Not Being Recognised In MAME Emulator

I figured this might be a long shot but I have no idea where to get any help for this.

I've been programming a Teensy to simulate key-presses so I can make an arcade gamepad which otherwise works fine in other programs, however in the MAME Emulator the keystrokes are not recognised and I'm unsure what to do.

Both of my keyboards work fine with the emulator, as does a cheap USB gamepad I used to play with.
The Teensy board isn't being recognised however unlike any other device, does the Teensy communicate as a true HID device? Because it certainly isn't recognised as one when using the emulator.

The chances of anybody knowing what to do on here are very minimal and I realise that but I am very desperate to get this working.
Does anybody have a clue how I might be able to get my Teensy keyboard input working with this program?
I have even tried looking at the source of the MAME version I compiled (AdvanceMAME) and have looked at 'input.c' to see if there is anyway I could inject keystrokes through the software but as I have literally no knowledge in C I am struggling to find a way to pursue that root, all I know is that the input is in someway handled by the SDL library.

Being new to programming micro-controllers etc I'm not sure whether I am just being stupid or if it's going to be a difficult problem to solve. Any help or guidance would be greatly appreciated because I'm always trying to learn xD.

Perhaps there is a mistake in the code you did not show us.

johnwasser:
Perhaps there is a mistake in the code you did not show us.

Here is the test code I used, it literally just types these 2 numbers so there can't be anything wrong with the code can there? It works on every other program (eg: notepad) but my MAME Emulator never picks it up.

void setup() {
  // put your setup code here, to run once:
  Keyboard.begin();
  delay(10000);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  Keyboard.write('5');
  delay(1000);
  Keyboard.write('1');
}

Any suggestions?

What do the digits '5' and '1' mean in MAME? I don't see them on the list of default keyboard controls:
https://docs.mamedev.org/usingmame/defaultkeys.html

johnwasser:
What do the digits '5' and '1' mean in MAME? I don't see them on the list of default keyboard controls:
Default Keyboard Controls — MAME Documentation 0.227 documentation

Those keys are for the user interface and not the gameplay.
The 5 is for insert credit and the 1 is for player 1 start. http://members.optusnet.com.au/spacetaxi64/MAME-Arcade/MAME-Keys.html
The 5 and 1 key work in the game when I use an actual keyboard but it doesn't recognise the 5 and 1 from the Teensy even though it recognises in the start menu search, notepad, etc. Any ideas?

It is possible that the MAME software is "debouncing" the keyboard and ignoring very short key presses. The Arduino Keyboard library .write() does a .press() immediately followed by .release(). Perhaps calling them separately and adding a short delay between would work. I'd start with a 500 millisecond delay (WAY too long) and if that works, try shorter delays.

void setup() {
  // put your setup code here, to run once:
  Keyboard.begin();
  delay(10000);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  Keyboard.press('5');
  delay(500);
  Keyboard.release('5');
  delay(1000);
  Keyboard.press('1');
  delay(500);
  Keyboard.release('1');
}

johnwasser:
It is possible that the MAME software is "debouncing" the keyboard and ignoring very short key presses. The Arduino Keyboard library .write() does a .press() immediately followed by .release(). Perhaps calling them separately and adding a short delay between would work. I'd start with a 500 millisecond delay (WAY too long) and if that works, try shorter delays.

void setup() {

// put your setup code here, to run once:
  Keyboard.begin();
  delay(10000);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  Keyboard.press('5');
  delay(500);
  Keyboard.release('5');
  delay(1000);
  Keyboard.press('1');
  delay(500);
  Keyboard.release('1');
}

Thank you so much! The emulator now receives the keys :slight_smile:

johnwasser:
It is possible that the MAME software is "debouncing" the keyboard and ignoring very short key presses. The Arduino Keyboard library .write() does a .press() immediately followed by .release(). Perhaps calling them separately and adding a short delay between would work. I'd start with a 500 millisecond delay (WAY too long) and if that works, try shorter delays.

void setup() {

// put your setup code here, to run once:
  Keyboard.begin();
  delay(10000);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  Keyboard.press('5');
  delay(500);
  Keyboard.release('5');
  delay(1000);
  Keyboard.press('1');
  delay(500);
  Keyboard.release('1');
}

Over further inspection the code you have gave me works in MAME but not the version (ADVMAME) I am using. I need to use this version because it is a lot more efficient on ARM platforms. So it works in one version but not another, I feel like it's getting so close so is there any last things I can try and do?

Late here, but I was having the same problem and hope the solution might help someone. I ended up using an algorithm like this in loop:

state = digitalRead(PIN);

if (state)
   Keyboard.press('a');
else
   Keyboard.release('a');

I have some debouncing too, but I don’t know if I really need it. I used a Arduino Pro Micro.