checking state of a specific keyboard key in c++

Is there a way to check (Mac c++) if a specific key is currently pressed, like a macOS counterpart to Windows' GetAsyncKeyState? If the user presses the 'W' or 'E' key, for example, my app modifies the behavior of a drawing tool as it's being dragged.

Replies

Yes, CGEventSourceKeyState. You need to use the virtual key codes, not character codes. The Carbon header file Events.h contains enumerated constants for these codes.

Thanks! I suspect macOS is blocking it, though.

In trying to find how to implement that in a non-typical Mac app (I write plugins for Adobe Illustrator using their c++ SDK and Hot Door's CORE), I found that macOS often blocks keyboard activity. In Ventura's System Settings > Privacy & Security > Input Monitoring I added Adobe Illustrator 2023 to the list of applications allowed to monitor the keyboard. I've been using a CORE function to monitor key presses, which has worked well until recently, and still works well for others with an older OS -- I suspect CORE already implemented CGEventSourceKeyState so my workaround is not likely to work.

Where else in macOS Ventura could those keystrokes be blocked?

It's conceivable that System Settings > Privacy & Security > Accessibility could be needed. Try calling CGRequestListenEventAccess, which may result in a prompt to add some permission in System Settings.