How To Simulate USB Keyboard From Machine?
Introduction
Simulating a USB keyboard from another machine can be a useful technique in various scenarios, such as testing keyboard input, automating tasks, or even creating a virtual keyboard for accessibility purposes. In this article, we will explore how to simulate a USB keyboard from one machine to another, with a focus on using Ubuntu Linux 14 and above as the operating system.
Understanding USB Keyboard Simulation
Before we dive into the implementation details, it's essential to understand the basics of USB keyboard simulation. A USB keyboard is a human interface device (HID) that sends keyboard input to the operating system. To simulate a USB keyboard, we need to create a virtual HID device that mimics the behavior of a real keyboard.
Requirements
To simulate a USB keyboard from one machine to another, you will need:
- Two machines with Ubuntu Linux 14 and above installed
- A network connection between the two machines
- A tool to simulate the USB keyboard (we will use
hidraw
andxinput
in this example)
Step 1: Install Required Packages
On the machine that will act as the virtual keyboard, install the following packages:
sudo apt-get update
sudo apt-get install libusb-dev libudev-dev
Step 2: Create a Virtual HID Device
We will use hidraw
to create a virtual HID device that mimics the behavior of a real keyboard. Create a new file called keyboard.py
with the following content:
import hid
import time

interface = hid.HidDevice()
layout = [
[0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x07, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x08, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x09, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x0a, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x0b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x0c, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x0d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x0e, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x0f, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
]
Q: What is the purpose of simulating a USB keyboard from another machine?
A: Simulating a USB keyboard from another machine can be useful in various scenarios, such as testing keyboard input, automating tasks, or even creating a virtual keyboard for accessibility purposes.
Q: What are the requirements for simulating a USB keyboard from another machine?
A: To simulate a USB keyboard from one machine to another, you will need:
- Two machines with Ubuntu Linux 14 and above installed
- A network connection between the two machines
- A tool to simulate the USB keyboard (we used
hidraw
and xinput
in this example)
Q: How do I create a virtual HID device to simulate a USB keyboard?
A: To create a virtual HID device, you can use the hidraw
library. You will need to define the HID device interface, the keyboard layout, and the keyboard report descriptor. The keyboard report descriptor is a binary data structure that defines the layout of the keyboard.
Q: What is the keyboard report descriptor, and how do I define it?
A: The keyboard report descriptor is a binary data structure that defines the layout of the keyboard. It consists of a series of bytes that describe the keyboard's capabilities, such as the number of keys, the type of keys, and the layout of the keys. You can define the keyboard report descriptor using a tool like hid-cfg
.
Q: How do I send keyboard input from the virtual HID device to the target machine?
A: To send keyboard input from the virtual HID device to the target machine, you can use the hidraw
library to send a keyboard report to the target machine. The keyboard report is a binary data structure that contains the keyboard input.
Q: How do I receive keyboard input on the target machine?
A: To receive keyboard input on the target machine, you can use the xinput
tool to listen for keyboard events. The xinput
tool can be used to simulate keyboard input, as well as to listen for keyboard events.
Q: Can I use this method to simulate a USB keyboard from one machine to another over a network?
A: Yes, you can use this method to simulate a USB keyboard from one machine to another over a network. You will need to use a network protocol like TCP or UDP to send the keyboard input from the virtual HID device to the target machine.
Q: Are there any security concerns when simulating a USB keyboard from another machine?
A: Yes, there are security concerns when simulating a USB keyboard from another machine. If the virtual HID device is not properly secured, it could be used to inject malicious keyboard input into the target machine.
Q: Can I use this method to simulate a USB keyboard from one machine to another on a Windows machine?
A: Yes, you can use this method to simulate a USB keyboard from one machine to another on a Windows machine. However, you will need to use a different tool, such as hidapi
, to simulate the USB keyboard.
Q: Can I use this method to simulate a USB keyboard from one machine to another on a Mac machine?
A: Yes, you can use this method to simulate a USB keyboard from one machine to another on a Mac machine. However, you will need to use a different tool, such as hidapi
, to simulate the USB keyboard.
Conclusion
Simulating a USB keyboard from another machine can be a useful technique in various scenarios, such as testing keyboard input, automating tasks, or even creating a virtual keyboard for accessibility purposes. By using the hidraw
and xinput
tools, you can create a virtual HID device that simulates a USB keyboard and sends keyboard input to the target machine. However, you should be aware of the security concerns and use proper security measures to prevent malicious keyboard input from being injected into the target machine.