What do I need to change in my code to sent a F1 keystroke?

I have a QTPY that I have programed to send an ASCII key so only characters. What do I need to change to make it send an F1 keystroke?

#include <Keyboard.h>

// use this option for Windows and Linux, number is ASCII:
  char button = 102;

void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  if (digitalRead(2) == HIGH) {
    // do nothing until pin 2 goes High
 
  Keyboard.press(button);
  }
   else {
   Keyboard.releaseAll();
  }
}

There is no 'ASCII' code for F1. You want the USB button code 58 but since 58 is ALREADY the USB code, you have to sneak it past the Keyboard.press() "ASCII to USB" translation. To do that you add the magic number 136 to it. The number you want is: 194

Looks to me like you shlould use “button = KEY_F1;”

All default supported keys are in Keyboard.h so you can locate that file on your PC and view it with an editor. On my Windows system: C:\Program Files (x86)\Arduino\libraries\Keyboard\src\Keyboard.h

When checking that file, you will see that @johnwasser's solution and @westfw's solution are the same :slight_smile:

1 Like

I am confused do I just replace button = 102; with button = KEYF1; ?

It's "KEY_F1" but, yes.

The library defines KEY_F1 as 194 so you can use:
char button = KEY_F1;
or
char button = 194;

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