First, for an operating system to process input, it must continuously scan the keyboard matrix to detect keypresses. This adds extra load to both the OS and the CPU. Additionally, every OS must implement the same logic with the same parameters. Since there are many types of keyboards, each OS would need to know every keyboard's matrix layout.
However, before addressing that, like keyboards other HID peripherals are not directly part of the hardware. They connect to computers via USB or Bluetooth, and to do so, keyboards must have an internal chip, "a brain" even just for communication, let alone scanning keyboard matrix. This is similar to how a mouse processes sensor data internally rather than relying on the computer to interpret it. As a result, they can be used on almost any smart device with USB or Bluetooth.
Since we already have this extra chip, "brain", we can add personalized custom logic to simplify our lives and give our hands and wrists a break. This is similar to how gaming mice reduce DPI when the aim button is pressed or how old joysticks had an auto-fire button.
Shh! The computer doesn’t need to know ;)