How can I read input from a numpad while blocking its keystrokes in mac os?

36 views Asked by At

This person has a similar project but for linux: https://unix.stackexchange.com/questions/343305/disable-keyboard-but-still-allow-reading-from-it

What I'm trying to do is make a numpad act like a launchpad. My goal is to consume the keystrokes coming from it, blocking them from going to the currently focused software.

I tried keyboard and pynput but neither could block the key events from being read by the currently focused application. I also looked into a nodejs approach to no avail.

I have no desire to use this numpad as a regular keyboard, so it would be okay if it were incapable of it as long as I can still respond to inputs in some background software.

When I asked ChatGPT, it said this might not be the kind of project that's doable on MacOS, which seems believable. I've never seen a piece of software that consumes key events while not in focus.

Is it possible? If so, how can I read input from a numpad while blocking its keystrokes in mac os?

Nodejs or Python would be preferable, but if I have to go through a compiled language and XCode, it is what it is.

1

There are 1 answers

0
5rod On

Since I don't use mac os, I searched up the keyboard layout. Usually, there is a key on the keyboard labeled 'clear' or 'numlock', which effectively locks the numpad. To read keys from the numpad and distinguish between the other numerical keys, I did a little bit of digging, and found the perfect post about using pynput to do so.

Here's my snippet of code:

from pynput import keyboard
import keyboard as keys #named it keys so it is different from pynput

def on_press(key):
    if hasattr(key, 'vk') and 96 <= key.vk <= 105: #checks if it is numpad
        #do whatever you want with the numpad key

def lock_numpad():
    keys.press_and_release('numlock') #on mac os it might be 'clear'

def listen():
    with keyboard.Listener(on_press = on_press) as listener:
        listener.join()

and this might work. I was a little confused when reading your question, so I apologize if I misinterpreted it.