Skip to content
Arduino library making it easier to use HID buttons
Branch: master
Clone or download
Latest commit ef8618e Feb 24, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
extras Add HID button type flag to documentation Feb 24, 2019
.gitattributes Initial commit Feb 8, 2019

HID Buttons Library

arduino-library-badge Build Status

This small Arduino library makes it easier to set up programs that use Keyboard, Mouse, or Joystick buttons by storing the associated key / button ID and output state for comparison. This makes your code cleaner, simpler, and prevents spamming USB reports with duplicated keypresses.


	if (stickY < -300) {
		if (aPressed == 0){
			aPressed = 1;'a');
	else {
		if (aPressed == 1) {
			aPressed = 0;


	KeyboardButton ButtonA('a');
	ButtonA.set(stickY < -300);

All HID button types share a common base class and are treated identically, allowing you to share logic between buttons on composite devices (e.g. Keyboard + Mouse).

Button 'Set' Functions

void press();
void release();

void toggle();
void write();

void set(boolean state);

static void releaseAll();

Press / Release

The traditional press() and release() functions are present and accounted for! These pass through the class's state filter so repeated calls will avoid spamming USB messages.


This is press() and release() wrapped up into one convenient function. Pass a boolean to set() the state of the button. True is 'pressed', false is 'released'.


Flip it! Calling toggle() changes a press to a release and a release to a press.


Don't need to hold it down? Call write() to press and release a button right away.


Release all of the buttons! Can be called either on the base class to release everything:


Or on a specific type to release only buttons of that type:

KeyboardButton::releaseAll();  // release Keyboard buttons only
MouseButton::releaseAll();  // release Mouse buttons only

Button 'Get' Functions

boolean isPressed() const;
virtual uint16_t getButton() const;
virtual HID_Button_Type getType() const;


Calling getPressed() will return the internal button state, where 'true' is pressed and 'false' is released.


Calling getButton() will return the key or button identifier for that object. For a KeyboardButton this is the key, for a MouseButton or JoystickButton this is the button number.


The getType() function will return the HID type for that specific button object using the library's classed enumeration. If you're passing objects using the base class (HID_Button) this will tell you what HID type that button is using.

Supported HID Types

The library currently supports three HID button types:

All three of these HID types are also supported on the Teensy using its native libraries.

To add support for a new HID type, please see the documentation in the 'extras' folder.


Released under the terms of the permissive MIT license. See the LICENSE file for more information.

You can’t perform that action at this time.