How do I Google?

The Long complex answer of how I get to google.com

1000ft view

  • Keyboard
  • Computer to Program
  • URL Bar in Browser
  • Finding the Web Server
  • Sending Data to Web Server
  • Retrieving Data from Web Server
  • Browser getting other files from webpage
  • Browser rendering the Data for the viewer

1000ft view

  • Keyboard
  • Computer to Program
  • URL Bar in Browser
  • Finding the Web Server
  • Sending Data to Web Server
  • Retrieving Data from Web Server
  • Browser getting other files from webpage
  • Browser rendering the Data for the viewer

The Keyboard

Keyboard Assumptions

  • This is a mechanical Keyboard
  • This is not a Virtual Keyboard like those found on a Mobile Device

How does a Keyboard Work

While a keyboard couldĀ  have wires that go to every key on the keyboard they don't

Key Matrix

Key Matrix

Key Matrix

Key Matrix and Polling

Well what happens when I press more than one key at a time?

Error Cases

Solution

Fixed Edge Cases

Why are LEDs Common in Keyboards?

LED

  • Light
  • Emitting
  • Diode

More Keyboard Assumptions

  • I will assume its a USB Keyboard
  • It is not an old PS/2 Keyboard
  • It is not a Wireless keyboard

Why not PS/2?

  • A PS/2 Connector uses CPU interrupts to get the keyboard value
  • And I don't want to get into how Interrupts work on a CPU.

Why not Wireless Keyboards

  • I don't want to get into Bluetooth
    • Most of the time its the same USB info anyway
  • I don't want to get into Infrared Communication
  • They usually have a USB dongle plugged into the computer

USB

USB Cliff Notes

  • When the USB keyboard is plugged in it tells the computer that it is a Human Interface Device (HID)
    • The Computer sets rate at which it asks for updates from the Keyboard
      • Called polling rate (Commonly 1000 Hz or 1ms)
  • The keyboard only sends data when there is a keypress or key release
    • This prevents unnecessary data being transmitted

Keycode

  • Used to map keys to a value in between 0-255
    • Takes up a single Byte of Data

Keycode in Hex

  • Used to map keys to a value in between 0x00 - 0xFF
    • Takes up a single Byte of Data

Examples of Keycodes

Key Pressed Integer Keycode Hex Keycode
1 30 0x1E
2 31 0x1F
3 32 0x20
a 4 0x04
b 5 0x05
Enter 40 0x28

Data to Computer

USB Data Transfer Examples

"1" Key Pressed

"2" Key Pressed

"1" + "2" Key Pressed

"Enter" Key Pressed

"Left Control" + "Enter" Pressed

"Left Control" + "Left Alt" + "Delete" Pressed

Data from Computer

1000ft view

  • Keyboard
  • Computer to Program
  • URL Bar in Browser
  • Finding the Web Server
  • Sending Data to Web Server
  • Retrieving Data from Web Server
  • Browser getting other files from webpage
  • Browser rendering the Data for the viewer

Computer to Program

OS Drivers

  • The Keyboard Driver Takes that Information and compares it to the previous information.
    • On OSX this is handed by the I/O Kit kext keyboard driver
    • On Windows this is done by the KBDHID.sys driver
    • On Linux this is done by the evdev driver
  • This driver uses this new data with the previous state to generate a list of keyboard events to give to the Windows Manager

OS Windows Managers

  • The Keyboard Events are sent to the Windows Manager and added to the event queue to be processed
    • In OSX this is called the WindowServer process
    • In Windows this is called the Win32K.sys service
    • In Linux this is called the X Server
  • The Windows manager keeps track of what is the Active Window and sends the appropriate Keyboard Events to the Active Process
    • The two main Events for a keyboard are the KeyUp and the Keydown Event

How does Spotify know to pause the music when I press the Play/Pause key?

Making an EventHandler

  • All OS have a variations on this but all follow the same way
  • Programs like Spotify can make a function to listen to events from the event handler.
  • If the play/pause button is pressed then the program receives a key down event from the OS
    • The program the checks to make sure the Key is the Play/Pause button
    • If this event is not what it wants then it can ignore it pass on the event to the next handler in the list.
    • Spotify then knows that the play/pause button was clicked and pauses the song if it is playing or plays it if was paused

Making an EventHandler

  • Since it is up to the Program to pass it to the next handler
  • Because there is the option to either act on the event or ignore it this is why only one program starts or pauses a song or video.
    • Since It acted on that event the program will decide not to pass that event to the next event handler
  • Since the EventHandlers are in an order there is a priority of which handlers are called before others. This depends on the OS.
    • Windows put the newest registered EventHandler first.
  • And yes this is also a way to make a Keylogger

The Web Browser

  • Since the Web Browser is the active Window and the URL bar is the active element in that window the keyboard data gets put into that bar.

1000ft view

  • Keyboard
  • Computer to Program
  • URL Bar in Browser
  • Finding the Web Server
  • Sending Data to Web Server
  • Retrieving Data from Web Server
  • Browser getting other files from webpage
  • Browser rendering the Data for the viewer
Made with Slides.com