How does the 'USB keyboard' work?



Many people casually connect a 'USB keyboard' to a PC and use it, but not many people may have thought about the detailed mechanism. Ben Eater, a gadget-based YouTuber, explains the question, 'How does the USB keyboard work?'

How does a USB keyboard work? --YouTube


This time, we will use an oscilloscope that analyzes electrical signals to see how the USB keyboard works. First, the oscilloscope before connecting the USB keyboard shows two signal lines, 'D + (yellow)' and 'D- (green)'.



When I plugged the DELL USB keyboard into the oscilloscope, the D + remained relatively low, but the D- moved to a higher position.



A low D + and a high D- indicate that the USB keyboard is idle at 'Low Speed'.



In other words, this change in signal indicates that the USB keyboard is connected to the device and that the connection is at Low Speed.



When I tried connecting another USB keyboard, it seemed that this was 'High Speed (maximum transfer speed is 480 Mbps)', D + moved to a high position, and D- moved to a low position.



Pressing a key does not change the oscilloscope waveform, because USB keyboard communication can be started from the PC side. But this time, I'll connect it to a special cable so that the oscilloscope can reflect the signal from the USB keyboard.



Then a signal was generated on the oscilloscope.



It looks like this when zoomed.



Record the zoomed waveform as a screenshot to analyze the waveform in detail ...



Combine in Photoshop.



And when I write it out on paper, it looks like this.



Most parts have waveforms that move in opposite directions, such as 'D + is high and D- is low' or 'D + is low and D- is high', but in some places 'D + is low and D- is also low'. There is a part that is in the state.



This is a condition called single-ended zero (SE0).



When SE0 is shown in the waveform, it looks like this.



It is said that the fact that SE0 continues for 2 bits indicates the 'end of packet' ...



You can see that this waveform contains three major packets.



In addition, the state where D + is low and D- is high is called 'J state', and the state where D + is high and D- is low is called 'K state' ...



If you enter the J state and K state for each bit, it looks like this.



The J state continues for a while after the end of the packet, and the K state is the starting point of the packet. At first, it always starts with the pattern 'KJKJKJKK', but since this shows the synchronization part of the signal called 'Sync packet', the actual data is included in the subsequent parts.



The data in a packet is represented by a repetition of '0' and '1', but this waveform does not mean that either 'K' or 'J' corresponds to '0' or '1'. In this waveform, 'different from the previous state = 0' and 'same as the previous state = 1', and the 'K' next to the first 'KJKJKJ ... KK' is '1'. '. You can assign 0s and 1s to the next 'J' as '0', the next 'K' as '0', the next 'K' as '1', and so on.



As I manually decrypted the packet, I found that the first packet represented the device ID input command 'IN', the USB keyboard device ID '24', and the confirmation command.



The next packet looks like this for a longer time. It is a data input command 'DATA0', 8-bit data '00' '00' '00' '00' '00' '00' '00' '00', and a confirmation command.



The last packet was a confirmation packet of the data to be sent.



What is important here is the second data packet '00' '00' '00' '00' '00' '00' '00' '00', which means 'no key'. I haven't pressed it. '



Now connect the USB keyboard to a more sophisticated oscilloscope and look at the packets when you press a key.



After connecting and adjusting, the manually decoded packet is now displayed directly on the oscilloscope. The data packet when the key is not pressed is still '00' '00' '00' '00' '00' '00' '00' '00' ...



When you press the 'Windows key', 'Shift key', 'Ctrl key', etc., the first bit changes to the value corresponding to each key. For example, the 'Ctrl key' is displayed as '01' and the 'Alt key' is displayed as '04'.



When multiple keys are pressed at the same time, the sum of the values corresponding to each key is displayed, and it is possible to determine which key is being pressed at the same time. This time, since the 'Ctrl key' and 'Alt key' are pressed at the same time, '05', which is the sum of '01' and '04', is displayed.



The second bit from the beginning is not used for anything and is always in the state of '00', and other keys such as 'A' and 'B' are displayed as a combination of the remaining 6 bits.



When multiple keys were pressed, each of the 6 bits took a different value, and it was possible to recognize that up to 6 keys were 'pressed at the same time'. Eater said that the recognition ability of this input key differs depending on the performance of the USB keyboard, so depending on the object, it can be identified by pressing more keys at the same time.



in Hardware,   Video, Posted by log1h_ik