If the USB device itself presents a HID keyboard interface, then from the point of view of the computer, it is (or "contains") a keyboard. The corresponding HID driver is loaded. The device communicate through that interface using the corresponding HID protocol.
USB devices can have many interfaces. The device itself sends information to the operating system describing what it is, in USB descriptors. You can see that information in the verbose output of lsusb (
lsusb -v). The relevant values are
Here is one of the interfaces of my mouse for instance, presenting itself as a keyboard. When buttons assigned keys or macros are clicked the mouse "type" here :
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 1 Boot Interface Subclass
bInterfaceProtocol 1 Keyboard
And it is listed by
xinput just like an actual second keyboard.
This also has the potential to be used maliciously, as something doesn't have to look like a keyboard at all to type things for you (or presents itself as any other device).