In this example, we will be utilizing the USBHIDBootKbdAndMouse example from the USB_Host_Shield_2.0 Arduino library. This example can be found in the File dropdown menu (i.e. (1) File > Examples > USB Host Shield Library 2.0 > HID > USBHIDBootKbdAndMouse). Once the example has been opened, users should see the USBHIDBootKbdAndMouse.ino example sketch.
#include<hidboot.h>#include<usbhub.h>// Satisfy IDE, which only needs to see the include statment in the ino.#ifdef dobogusinclude#include<spi4teensy3.h>#endif#include<SPI.h>classMouseRptParser:publicMouseReportParser{protected:voidOnMouseMove(MOUSEINFO*mi);voidOnLeftButtonUp(MOUSEINFO*mi);voidOnLeftButtonDown(MOUSEINFO*mi);voidOnRightButtonUp(MOUSEINFO*mi);voidOnRightButtonDown(MOUSEINFO*mi);voidOnMiddleButtonUp(MOUSEINFO*mi);voidOnMiddleButtonDown(MOUSEINFO*mi);};voidMouseRptParser::OnMouseMove(MOUSEINFO*mi){Serial.print("dx=");Serial.print(mi->dX,DEC);Serial.print(" dy=");Serial.println(mi->dY,DEC);};voidMouseRptParser::OnLeftButtonUp(MOUSEINFO*mi){Serial.println("L Butt Up");};voidMouseRptParser::OnLeftButtonDown(MOUSEINFO*mi){Serial.println("L Butt Dn");};voidMouseRptParser::OnRightButtonUp(MOUSEINFO*mi){Serial.println("R Butt Up");};voidMouseRptParser::OnRightButtonDown(MOUSEINFO*mi){Serial.println("R Butt Dn");};voidMouseRptParser::OnMiddleButtonUp(MOUSEINFO*mi){Serial.println("M Butt Up");};voidMouseRptParser::OnMiddleButtonDown(MOUSEINFO*mi){Serial.println("M Butt Dn");};classKbdRptParser:publicKeyboardReportParser{voidPrintKey(uint8_tmod,uint8_tkey);protected:voidOnControlKeysChanged(uint8_tbefore,uint8_tafter);voidOnKeyDown(uint8_tmod,uint8_tkey);voidOnKeyUp(uint8_tmod,uint8_tkey);voidOnKeyPressed(uint8_tkey);};voidKbdRptParser::PrintKey(uint8_tm,uint8_tkey){MODIFIERKEYSmod;*((uint8_t*)&mod)=m;Serial.print((mod.bmLeftCtrl==1)?"C":" ");Serial.print((mod.bmLeftShift==1)?"S":" ");Serial.print((mod.bmLeftAlt==1)?"A":" ");Serial.print((mod.bmLeftGUI==1)?"G":" ");Serial.print(" >");PrintHex<uint8_t>(key,0x80);Serial.print("< ");Serial.print((mod.bmRightCtrl==1)?"C":" ");Serial.print((mod.bmRightShift==1)?"S":" ");Serial.print((mod.bmRightAlt==1)?"A":" ");Serial.println((mod.bmRightGUI==1)?"G":" ");};voidKbdRptParser::OnKeyDown(uint8_tmod,uint8_tkey){Serial.print("DN ");PrintKey(mod,key);uint8_tc=OemToAscii(mod,key);if(c)OnKeyPressed(c);}voidKbdRptParser::OnControlKeysChanged(uint8_tbefore,uint8_tafter){MODIFIERKEYSbeforeMod;*((uint8_t*)&beforeMod)=before;MODIFIERKEYSafterMod;*((uint8_t*)&afterMod)=after;if(beforeMod.bmLeftCtrl!=afterMod.bmLeftCtrl){Serial.println("LeftCtrl changed");}if(beforeMod.bmLeftShift!=afterMod.bmLeftShift){Serial.println("LeftShift changed");}if(beforeMod.bmLeftAlt!=afterMod.bmLeftAlt){Serial.println("LeftAlt changed");}if(beforeMod.bmLeftGUI!=afterMod.bmLeftGUI){Serial.println("LeftGUI changed");}if(beforeMod.bmRightCtrl!=afterMod.bmRightCtrl){Serial.println("RightCtrl changed");}if(beforeMod.bmRightShift!=afterMod.bmRightShift){Serial.println("RightShift changed");}if(beforeMod.bmRightAlt!=afterMod.bmRightAlt){Serial.println("RightAlt changed");}if(beforeMod.bmRightGUI!=afterMod.bmRightGUI){Serial.println("RightGUI changed");}}voidKbdRptParser::OnKeyUp(uint8_tmod,uint8_tkey){Serial.print("UP ");PrintKey(mod,key);}voidKbdRptParser::OnKeyPressed(uint8_tkey){Serial.print("ASCII: ");Serial.println((char)key);};USBUsb;USBHubHub(&Usb);HIDBoot<USB_HID_PROTOCOL_KEYBOARD|USB_HID_PROTOCOL_MOUSE>HidComposite(&Usb);HIDBoot<USB_HID_PROTOCOL_KEYBOARD>HidKeyboard(&Usb);HIDBoot<USB_HID_PROTOCOL_MOUSE>HidMouse(&Usb);KbdRptParserKbdPrs;MouseRptParserMousePrs;voidsetup(){Serial.begin(115200);#if !defined(__MIPSEL__)while(!Serial);// Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection#endifSerial.println("Start");if(Usb.Init()==-1)Serial.println("OSC did not start.");delay(200);HidComposite.SetReportParser(0,&KbdPrs);HidComposite.SetReportParser(1,&MousePrs);HidKeyboard.SetReportParser(0,&KbdPrs);HidMouse.SetReportParser(0,&MousePrs);}voidloop(){Usb.Task();}
Users will need to connect an HID device (keyboard and/or mouse) to the USB-C host shield with a USB cable, before running the example. After the example begins, users should see an output in the Serial Monitor with print out based on the user's interaction with their HID device.