Detecting Key Holds MacOS

I've been experimenting around with Ncurses for a bit and discovered the wonderful world of getch() and detecting arrow key presses. However, I am unable to think(or find) a way of detecting when a key is held down without using external libraries.

Multiple simultaneous key presses can be easily detected via getch(), but this way repeated key presses do not work. Using macOS's keyrepeat function allows me to mimic detection of a held key, but it only allows one key press at a time.

Is there a way to detect multiple keys being held down at the same time in default C++ without any external libraries? I don't think there is, but I am also hopeful for any solutions there may be. If there are any easily installed external libraries that would allow me to do this, that would be nice as well.
What are you hoping to get out of multiple keys auto repeating?

If a and b are pressed at the same time, do you expect to see abababababab..... ?

Bearing in mind this will almost certainly screw up any attempts to use modifiers, like say ctrl-shift-v

> Is there a way to detect multiple keys being held down at the same time in default C++ without any external libraries?
No.
It seems like your best bet is to use Ncurses and maybe start hacking the library code to add the feature you're looking for.
I don't think there is a function/class in Standard C/C++ that gives you "low-level" keyboard status.

If you don't want to use third-party libraries, which hide the "ugly" details of the underlying operating system, then you'll have to deal with the "native" API that the operating system gives to you. I'm no expert on MacOS X, but since MacOS X is in fact based on Darwin (NeXTSTEP/BSD), and therefore is a fully-fledged Unix system under the hood, it probably works similar to other Unix-like operating system, such as Linux.

See here for a Linux example on how to open the keybaord "device file" and read the keyboard events:
https://stackoverflow.com/a/20946151
Last edited on
I was hoping to be able to check if a key is held down, and in the case it was held down I could do an action in each frame. I don't think it's possible to hack Ncurses here or do this without an external library, as I saw someone else said that ANSI terminals don't have key hold / key release actions recorded at all or something along those lines.

I don't think Mac has an easily accessible system to find low-level keyboard status. Windows has windows.h, Linux has linux/input.h, but I am unaware of any equivalent library on MacOS and was unable to find one either.

I think I'll go with an external library, but I'm not sure which is the most lightweight and easiest to install.
Last edited on
Well, if you find any OpenSource library that does what you need, you could check out their code and see which API they are using to get the keyboard status information from MacOS X ;-)
Last edited on
Thanks for the help! I'll experiment with stuff like that and see if I can throw something together.
Are you using a console app (implied by use of ncurses)?

Any GUI will send separate key down/up messages, so you can handle those if that's the case.
Topic archived. No new replies allowed.