How to send numpad keys using USBkeyboard.h?

Hi All,

I am currently using USBkeyboard.h to try to send keyboard signals from the nano 33 BLE sense board. What I need is the keys on the numpad, but I still can’t send them after many attempts.

I refer to the functions of mbed about USBkeyboard as follows and can't find any available information.
https://os.mbed.com/docs/mbed-os/v6.8/apis/usbkeyboard.html

I would like to know if anyone has made a similar attempt and succeeded.

Thanks and BRs,

Rean

Welcome to the forum.

I just tested the example on a Windows 10 PC and even connected to a Raspberry Pi with the official Pi OS. After replacing the ThisThread::sleep with delay() the sketch does exactly what it should.

Could you please post your code (use code tags) and describe what does not work and what exactly do you want to type? You just mentioned keys on numpad. They have multiple assignments.

Hi Klaus_K,

Thanks for your reply.

The numpad keys I mentioned are on the right side of the keyboard, including keys such as 0~9 and + - * / ...etc.

Using USBKeyboard.h to send ASCII key is OK, but it doesn't seem to be able to send numpad's key codes, I saw similar post as follows.
https://forum.arduino.cc/index.php?topic=266688.0

But I can't apply the solution to nano 33 ble board, here is my test code:

#include "PluggableUSBHID.h"// Library that needs to be added
#include <USBKeyboard.h>

USBKeyboard key;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Started keyboard test...");
}

void loop() {

  key.key_code(222, 0); // try Number-pad key -
  delay(1000);
  key._putc(222);
  delay(1000);

  key.key_code(223, 0); // try Number-pad key +
  delay(1000);
  key._putc(223);
  delay(1000);

  key.key_code(231, 0); // try Number-pad key 7
  delay(1000);
  key._putc(231);
  delay(1000);

  key.key_code(232, 0); // try Number-pad key 8
  delay(1000);
  key._putc(232);
  delay(1000);

  while(1); 
}

Thanks

Rean

Can you confirm that you can get your sketch to enumerate as a keyboard and print characters? Your sketch does not work on my system and as far as I know should not work.
Once your Arduino works as a keyboard you cannot use Serial anymore and therefore your sketch should not be able to run beyond while(!Serial);
Also, I believe for some reason (maybe USB initialization) you cannot use setup() and loop(). My sketch only works with main() like the original example from the mbedOS documentation.

What OS are you using on your PC?

Do you care about numpad key codes (if there is such a thing) or do you just want to use the functions like arrow down?

I added some code to my sketch and successfully used Arrow, CTRL and SHIFT. e.g.

keyboard.key_code( 'c', KEY_CTRL ); // CTRL + C copy

keyboard.key_code( KEY_HOME, KEY_SHIFT ); // mark text from current cursor to beginning of line

keyboard.key_code( RIGHT_ARROW );

You can find more information in the USBKeyboard .h and .cpp source files.

Hi Klaus_K,

When I send other key code (ex: "a") it works fine, my sketch can run on my env.(nano 33 BLE sense), include serial function. My OS on PC is win10.

I was refer the topic below as my template, it also use setup(), loop() and works well.
https://forum.arduino.cc/index.php?topic=718218.0

I need to send numpad keys because it's for customer's application.

Today I use USBMouse.h as an alternative and found it work for the app, I will update this post if I get the solution of keyboard, thank for your help. :slight_smile:

BRs,
Rean

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