How do mouse, keyboard, etc drivers work?

Like, what do they control outside of basic functions and what takes priority during functions?
The keyboard driver stack consists of the following: Kbdclass, the upper-level keyboard class filter driver, one or more optional upper-level keyboard filter driver, and I8042prt, the function driver [1]. The mouse driver stack consists of the following: Mouclass, the upper-level mouse class filter driver, one or more optional upper-level mouse filter driver, and I8042prt, the function driver [1]. Kbdclass and Mouclass can support more than one device in two different modes: in the one-to-one mode, each device has an independent device stack, and in the grandmaster mode, the class driver operates all the devices [1]. 
Device drivers work within the kernel layer of the operating system [2]. The kernel is the part of the operating system that directly interacts with the physical structure of the system [2]. An operating system loads device drivers and calls the specific functions in the driver software in order to execute specific tasks on the device [2]. Each driver contains the device specific codes required to carry out the actions on the device [2]. Device drivers allow your computer's operating system to communicate with hardware devices [1].