Skip to content

A convenient library for output HID keyboard and mouse events via /dev/hidX with USB Gadget on Linux

License

Notifications You must be signed in to change notification settings

nate-yocom/Nfw.Linux.Hid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nfw.Linux.Hid

A convenient library for outputting Keyboard and Mouse events via a Linux USB Gadget + OTG + /dev/hidX device.

NuGet

dotnet add package Nfw.Linux.Hid

Sample: Keyboard

using Nfw.Linux.Hid.Keyboard;

SimpleKeyboard keyboard = new SimpleKeyboard();
keyboard.EmitString("test");

You can also use methdos to toggle keys (i.e. 'hold down SHIFT'), send specific keys etc. SendKey or the Events which can be sent via EmitEvent.

Sample: Mouse

using Nfw.Linux.Hid.Mouse;

SimpleMouse mouse = new SimpleMouse();

// Start at center
mouse.Center();

// Move diagonally from top left, to bottom right
for(int x = 0; x < SimpleMouse.MAX_X; x++) {    
    mouse.MoveTo(x,x);
    Thread.Sleep(1);    
}

Note additional methods for clicking, toggling (press-and-hold), etc are also available.

Sample: Joystick

using Nfw.Linux.Hid.Joystick;

string hidDevice = args.Count() > 0 ? args[0] : "/dev/hidg2";

SimpleJoystick joystick = new SimpleJoystick(hidDevice);

// First press them all one by one
for (byte buttonId = 0; buttonId <= SimpleJoystick.MAX_BUTTON_ID; buttonId++) {
    joystick.UpdateButton(buttonId, true);
}

// Now release them all one by one
for (byte buttonId = 0; buttonId <= SimpleJoystick.MAX_BUTTON_ID; buttonId++) {
    joystick.UpdateButton(buttonId, false);
}

// Press all the odd ones at the same time
joystick.UpdateButtons(new byte[] { 1, 3, 5, 7, 9, 11, 13, 15 }, true);
// And release them all at once
joystick.UpdateButtons(new byte[] { 1, 3, 5, 7, 9, 11, 13, 15 }, false);

References

Attribution

Creating the HID devices

Creating the HID devices via USB Gadget is a bit out of scope for this library, but for reference, the method used by the author is included in the repo here. Only tested on rasbian-lite 32bit on a R-Pi4. Requires the dwc2 OTG driver/overlay:

echo "dtoverlay=dwc2" >> /boot/config.txt
echo "dwc2" >> /etc/modules

Then reboot, and run:

sudo ./init-usb-gadget.sh
sudo ./install-usb-gadget.sh

You should end up with /dev/hidg0, hidg1, hidg2 accordingly.

Note that the samples and code in this lib presume the use of the report descriptors from these scripts, i.e. a 8 byte keyboard report (2 status, 6 keys), a 7 byte mouse report, and an 11 byte joystick (16 buttons, 9 axis).

About

A convenient library for output HID keyboard and mouse events via /dev/hidX with USB Gadget on Linux

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published