I find two method to handle global events on Mac OS:
You can use Quartz Event Taps for that. In order to monitor/change/block keyboard events, your process will need to be trusted for Accessibility access.