Keyboard input is really quite simple. It just emulates a joystick with a large number of buttons. Any event handler down the chain that can handle joystick input can request to monitor the keyboard as a raw device or as any available controller profile.
NES emulators, for example, don't know if their NES controller is being emulated by a joystick or a keyboard. The event handlers are identical for both.
When the controller's button dialog wants the user to press a button, it creates a button mapper for the controller it belongs to.