0

I have an Uno and Leonardo that I am trying to have it interact with the GTA V video game. Using the Keyboard library, it works within the OS. But it ocassionally works within the GTA V process. It seems that the transfer speed may be too slow, as the game sometimes registers the key press and does the assigned command. For instance, I can have a pushbutton that when pressed, it activates Keyboard.Write('J'). While in GTA V, I can rapidly press the pushbutton for 3 seconds and the game will only detect one J within that time frame and do the assigned game command. Yet, if I rearrange the code to be momentary to where the pushbutton is active, it'll constantly send Keyboard.Write('J'), this will work for commands that are momentary ingame, like the vehicle horn.
Below is the code I used for my Uno. It works fine within programs.

/*
  Keyboard Message test

  For the Arduino Leonardo and Micro.

  Sends a text string when a button is pressed.

  The circuit:
  - pushbutton attached from pin 4 to +5V
  - 10 kilohm resistor attached from pin 4 to ground

  created 24 Oct 2011
  modified 27 Mar 2012
  by Tom Igoe
  modified 11 Nov 2013
  by Scott Fitzgerald

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/KeyboardMessage
*/

#include "Keyboard.h"

const int buttonPin = 4;          // input pin for pushbutton
int previousButtonState = HIGH;   // for checking the state of a pushButton
int counter = 0;                  // button push counter

void setup() {
  // make the pushButton pin an input:
  pinMode(buttonPin, INPUT);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonPin);
  // if the button state has changed,
  if ((buttonState != previousButtonState)
      // and it's currently pressed:
      && (buttonState == HIGH)) {
    // increment the button counter
    counter++;
    // type out a message
    Keyboard.write('J');
    //Keyboard.print("You pressed the button ");
    //Keyboard.print(counter);
    //Keyboard.println(" times.");
  }
  // save the current button state for comparison next time:
  previousButtonState = buttonState;
}

I also tried this same test using LUFA but the same issues were encountered.
I believe it has to do with the game, not my code. Does anyone have a clue of why this might be?

  • Please post your code that is doing what you don't want it to do. For your game, it's likely that either that the game only wants one J, or that it can detect when J is pushed down, and when J is released. You will have to adjust your Arduino code to do the same. – jose can u c Apr 4 '18 at 20:31
  • Your button is not debounced. It may be registered as pressed several times for just one physical press. – chrisl Apr 4 '18 at 21:18
  • Have you tested not in the game? (e.g. in notepad) Do you intend it to send SHIFT-J (capital J) or do you want just the j key? – Craig Apr 4 '18 at 22:01
  • When I tested in Notepad, It sends one Capital J. And it does work within the game, it's just that I have to rapidly press it (for 3 seconds for example) just for it to register once within the game as a command. – O.J. Apr 4 '18 at 23:04
1

It is quite common for games to be quite slow at reading the keyboard.

The Leonardo's Keyboard.write() command is quite rapid. The OS can pick it up with its interrupt driven buffering, but games tend not to use that buffering so they can get more realtime key control.

The trick here is actually to slow down the Leonardo's sending, by not using Keyboard.write(). Instead you need to press the key, wait a moment, then release the key:

Keyboard.press('j');
delay(1);
Keyboard.release('j');

Increase the delay until it becomes reliable. It's hard to say what the delay should be.

  • Okay, I'll give that a try. – O.J. Apr 5 '18 at 0:36
  • This definitely resolved my issue. Somewhere between 30-40 ms delay is the spot. Now I need to somehow figure if LUFA can achieve this. – O.J. Apr 5 '18 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.