ESP32 with USB Keyboard

I want to make a project with ESP32 in which I am using an ESP32 with a USB Keyboard. I require the ESP32 to read the input keystrokes from the keyboard which would be connected to it.

So does a USB Keyboard start to stream its data immediately when powered on, or does it verify that if a computer is listening to it?

If it doesn't check if there is a computer, then I can simply use some logic to identify the data from the stream. But if it does tries to check if there is a computer, then is there a way to fool the Keyboard into thinking that ESP32 is a computer? Or do I need a USB host controller?

I just want to get the live keystrokes data of the Keyboard on the ESP32 for some reason.

Thank You.

USB uses a quite complex protocol. Without proper device discovery and setup there is no data flow. I think you need a USB host controller (or an ESP32 variant with USB OTG or USB host functionallity).

BTW: Ben Eater put some videos about USB on youtube.

1 Like

Actually Ben Eater tells that a Keyboard needs a computer connected to it in order to output the keystrokes. I am asking about how to fool the keyboard into thinking that the ESP32 is a computer.

I second that.

I have seen, (by experimentation) that by even powering on my USB Keyboard, it outputs its data. I am not sure about other models.

Now I just need a way to decode the USB code.

There are keyboards that can use the old serial connection also,
maybe it's trying that mode.

(Ben Eater has a video for that topic also:)

I think USB clients are slave only, and don't emit information without being asked.

Have a look at that thread:

See. I have tested it now. It starts outputting keystrokes when it is powered on, even when the data pins are not connected anywhere. (I connected them to LEDs to confirm the data communication).

So now I want to know that if there is a library or code to decode these keyboard data pulses, or do I need to make my own makeshift code to decode it?

Great Personality Ben Eater explained these data pulses very well in his video.

I like keyboards that only emit keystrokes when used.
How do you know the data is keystrokes?

The LEDs lights which are connected to data pins, they blink when I press keys. Also, the blinking is with different delay for different keys.

Then I connected these to Arduino, to output any pulses to Serial. I also saw there than those pulses were different for different keys.

Use a logic analyzer or an oscilloscope to look at the data,
blinking will not help you much in identifying the protocol or data.

Ok. I will analyse the data protocol. (BTW Ben Eater has already used oscilloscope to identify the data protocol) But I just want to know if there is a library.

If not then I can make my own code based on the data protocol. (Library will just make it easy)......

Tell me if anybody knows some library to decode USB data.

Quite funny. Is your Google broken?

Which libraries do you mean? Sigrok? USB Data Analyser?

You need to do the device discovery and constant polling of the keyboard.

Ben has a video on device discovery too...

Probably your keyboard is falling back to PS/2 mode when no USB communication is made.

Do USB Keyboards have the functionality to switch to PS2 Mode? Do they do it? On what conditions do they switch?

Some do. Other will not have it. Did your keyboard came with an USB to PS/2 adapter?

Maybe. I don't remember. But on what conditions do the keyboard switch to PS2 Mode? So I can make it switch faster.

I would just try a PS/2 keyboard library on the Arduino...