Leonardo keypresses not working with OBS

I am using an Arduino Leonardo to have hotkeys for OBS but they don't work when the OBS Studio window is not focused.

I have the Leonardo press Ctrl + Alt + Shift + F7 when I press a button connected to the Leonardo. On OBS I have Ctrl + Alt + Shift + F7 switch to a slide. When I have the OBS window focused and I press the button it works fine. But when I have another application focused it does not work. The strange thing is, is that when I press (on my keyboard) Ctrl + Alt + Shift + F7, it will switch to the slide, but when I press the button it does not work.

I am running OBS as an Administrator btw.

My code for the Leonardo is below

int pinA = 8;

void setup() {
    pinMode(pinA, INPUT);
}

void loop(){
digitalWrite(pinA, HIGH);
readPinA = digitalRead(pinA);
if (readPinA == 0){
    Serial.print("A\n");
    Keyboard.press(KEY_RIGHT_CTRL);
    Keyboard.press(KEY_RIGHT_ALT);
    Keyboard.press(KEY_RIGHT_SHIFT);
    Keyboard.press(KEY_F7);
    Keyboard.releaseAll();
    delay(250);
    }
}

I can’t help you.

But I find it strange that the normal keyboad actually can change the focus to a specific program using .

I have been using Leonardo in many of my projects and like this controller because of the capability to "act" like keyboard or mouse. But sometimes it is "tricky" to get the results you need. :slight_smile:

  1. The code you posted does not compile because of missing (at least):
#include "Keyboard.h"

at the top of the sketch
and

Keyboard.begin();

in setup.

  1. Depending on the operating system (and other things) you may get different results using left or right modifier keys.
    For example on my current computer (Windows 10) the following line does not work (ALT key will not be recognized):
Keyboard.press(KEY_RIGHT_ALT);

But this line will work (ALT key will be recognized):

Keyboard.press(KEY_LEFT_ALT);

(I am not sure if this behavior has to do with the fact that on my "real" keyboard there is no right ALT key at all but an ALT GR key. I did not check if this behavior has to do with the specific software currently running).

  1. My experience is that some machines don't "like" too short key presses, witch may be produced by using
    Keyboard.press(....) and Keyboard.release()
    without any delay between.
    See also this topic: OBS - Solved: Global Hotkeys

On my current computer there is no OBS (but I know this software), so I can not test the code below with OBS. But with all said above a modification of your code could look like this (the push button is connected to pin 8 and GND):

#include "Keyboard.h"
const int pinA = 8;

void setup() {
  Keyboard.begin();
  Serial.begin(9600);
  pinMode(pinA, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(pinA) == LOW) {
    Serial.println("A");
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_LEFT_SHIFT);
    Keyboard.press(KEY_F7);
    delay(100);
    Keyboard.releaseAll();
    delay(150);
  }
}

sterretje:
But I find it strange that the normal keyboad actually can change the focus to a specific program using .

Global Hotkeys are shortcuts you can use even if an application does not have the focus.
Commonly used for example with well known “player applications” like VLC or AIMP.

uxomm:
Global Hotkeys are shortcuts you can use even if an application does not have the focus.
Commonly used for example with well known "player applications" like VLC or AIMP.

Never heard of them; thanks for pointing it out. karma added.