Raw mouse and keyboard input for macOS games post-Catalina? - macos

I have a feeling I know the answer to this question, but it's been surprisingly difficult to confirm it anywhere. Is it possible to get raw keyboard and mouse input post-Catalina without needing "Input Monitoring" access in the Security & Privacy preferences?
I've never programmed a game engine before on macOS, but so far as I can tell, the "usual" way for games to get raw mouse deltas and raw keyboard states has been to use IOKit's HID API. Up until recently, this apparently was also the only way to get input from Xbox and Playstation gamepads.
When I attempt to use the IOKit HID API to get mouse and keyboard input, I receive a prompt to allow Input Monitoring in Security & Privacy preferences. Elsewhere on the internet, I've learned that this is due to a change in macOS Catalina: to use IOKit HID input handling, you now need the Input Monitoring permission. As far as I'm concerned, it's a deal-breaker for my game to require users turn this on.
Am I understanding this situation correctly? And if I am, is it just not possible to get raw (unprocessed) keyboard and mouse input without the Input Monitoring permission? I mean, I can hack together a solution for FPS-style mouse input using mouseMoved: events and the like, but such a solution would disallow sub-pixel precision and would subject my game to macOS's cursor acceleration. Is there another solution that games/game engines use?

If you’re willing to take on the macOS 11 requirement, it seems like Apple added raw keyboard and mouse support to the Game Controller framework. Seems to even support caps lock.
https://developer.apple.com/documentation/gamecontroller/gckeyboardinput?language=objc

Related

Reading from a USB HID device that behaves like a keyboard

I am trying to read the input from a barcode scanner that behaves like a keyboard using the hidapi (https://github.com/signal11/hidapi). Apparently the OS does not allow me to access the scanner because it behaves like a keyboard and for security reasons the OS owns all the keyboard and mice devices. The manufacturer of this barcode scanner does not provide any drivers. Is there a way to prevent the OS from owning this barcode scanner or maybe a way to turn it into a serial port and read from it?
In case there is no workaround for this, does anyone know a cheap barcode scanner that does not behave like a keyboard so that I can use the hidapi to access it? Or how can I know that a barcode will not behave like a keyboard? It is a very specific question that most sellers don't know how to reply.
Thanks.
A device that only simulates keyboard input, such as a barcode scanner, does not have a way to control programmatically, rather than an interactive device such as a game controller.
You just wait for the barcode data to be notified as keyboard input data.
It will notify you even if you do not own the device. In other words, you need to own keyboard focus. It is ordinary Windows programming knowledge.
Many keyboard input barcode scanners have the ability to customize prefix / suffix codes that are appended to the front and back to identify barcode data input.
They can be customized by reading the settings barcode.
Check the notified key code one by one in the application program, and if such special data comes, please treat it as barcode data.
By "OS" do you mean Windows? Other operating systems usually provide a means to intercept keystrokes. Linux, for example, has the input subsystem which lets you intercept keyboard events like keys being pressed and released. Unfortunately, these leave it to you to convert to ASCII/UTF-8 characters yourself.
An easier option, regardless of OS, may be to look for a barcode scanner that emulates a USB-Serial device. That would appear to the system as COMx in Windows, /dev/ttyUSBx in Linux, etc. Then you can just read the data from it using a serial port library and not worry about keyboard focus.

Can I control the state of the Speaker Icon with Delphi code

Is it possible to control (Enable/Disable) the Speaker Icon in Delphi Code.
Is there a registry entry for this?
Many Thanks...
While it might be possible to "hide" the icon by changing a undocumented registry value, the user can unhide it again in the tray icon options so this is not really a permanent solution.
If you insist on ignoring your users desire to mute their machine you can unmute the volume programmatically.
On Windows XP/2003 and older you will have to use the old mixer API. This API is not fun to deal with and you have to remember to unmute both the Wave and master volume lines. See this and this for inspiration.
Vista and later has a new COM based API that should be less painful to deal with. Create a instance of IMMDeviceEnumerator and enumerate the render endpoints (or use GetDefaultAudioEndpoint) and call IAudioEndpointVolume::SetMute on the device endpoint. This example should be pretty close, just translate it to Delphi. Volume can also be controlled per application so you might need to deal with that as well if your users are clever.
Larry Ostermans blog is also a great source for help and details about this new API.
Note: As a workaround for your unmuting, the user can simply set the volume really low to get rid of your sound. There is really no way for you to know if the sound is audible at a low volume level and setting it higher just because you can would be very evil and could really hurt somebody with headphones etc.

Mac OS X 10.10 Find window by title, find button by label and press it

I use Mac OS X 10.10 and I would like to write a program that looks continuously for a window analyzing all the names of the opened windows. When the windows appear, I would like that the program will look for a button with a specific label and once found it, the app should send it a "pressed message".
I would be able to do it under windows, but I am not so familiar with Mac.
I have found a question related to mine (How do I get a list of the window titles on the Mac OSX?), but I think the most difficult part is finding the button and sending it a "pressed message".
Thank you in advance!
What you are looking for is the Accessibilty APIs. These are mostly Core Foundation style C APIs and typically prefixed with AX.
You might also want to consider additional identifiers beyond window title as window titles are not necessarily unique.
Using the AX APIs is not easy and is extremely verbose. You can use them to explore the UI and find things and interact with them but you might have more limited success observing user interaction. That might require a more fragile combination with event monitoring using NSEvent globalMonitor or CGEventTap depending on the UI widgets involved.
Also note that using the AX APIs to control anything outside your app is not sandbox capable.

Cocoa detect when hand is over right side of magic mouse

How can I detect if a user's hand is on the right side of a magic mouse? Not right clicking, just checking what side of the mouse the finger is on.
Unless you write something in the IOKit to handle this, it isn't that easy. What the App gets is what the driver (kext) sends it.
You could get something like Better Touch Tool or Magic Prefs which opens up a variety of options i.e. positions of fingers on mouse, where fingers are and are not registered etc...
Writing IOKit kext's isn't a simple process, but you could begin here:
https://developer.apple.com/library/mac/#documentation/devicedrivers/conceptual/IOKitFundamentals/Introduction/Introduction.html
Other than that, you're stuck with what the kext sends to your App as a notification.
Apple official driver is really limited . This include the lack
of support for advanced gestures like pinch and rotate.
The following proof of concept grab (very rawly) the pinch event,
using the euclide's distance between the two fingers, and send a
combined keystrokes as response to the front most application
(kCGHIDEEventTap). Launching the binary and bring a Preview.app window
to the front, you'll able to pinch in/out, using your magic
mouse...amazing! :-)
Take a look at http://www.iphonesmartapps.org/aladino/?a=multitouch
Extending Functionality of Magic Mouse: Do I Need a kext?
Apple Magic Mouse Api

Low level keyboard hook

I just bought a new keyboard, and I'm interested in tracking exactly how many keypresses/strokes I make during the entire life of the keyboard. (I would want to just record keyUp, as I don't care about repeats)
I've been googling around for the best way to do this, but I don't even know what approach to take, so I'm having trouble searching intelligently.
Also, the only language I've really worked with is C#. Haven't done anything with C/C++ or WinAPI
Is it difficult to modify the Win 7 drivers directly?
I do want to be able to record keystrokes from the moment the computer boots, but this may not be easily done.
I would prefer to use an existing driver or hook, as I really don't care to get into learning device drivers right now.
I've looked briefly at Ctrl2Cap, but I don't know how to modify something like that to suit my case.
Other questions I looked at,
Writing a keyboard device driver
Low level keyboard Hook not at UI thread
This one indicates writing a USB Filter driver may work, but I have no clue where to start on that.
Disabling the keyboard in windows c++?
It is not necessary to go the driver route, which is not for the faint of heart. You can use use a low level keyboard hook which will work fine from .NET.
I did a quick bing and found the following on codeplex
http://globalmousekeyhook.codeplex.com/
It should get you going
If you want to do that you need to use Kernel Level Keylogger. It's grabs the keystrokes before the operating system takes effects and this one 99% invisible for detection techniques. Then you can grab the Windows Log On screen typed things like password/username etc.
#ChrisTaylor's key logger not work for WinLog-on Screen I reckon. If you can install the keyboard driver filter before the system keyboard device driver take a action you can handle windows user login screen also. But In order to install this driver level key hook you need to have administrator privileges.
I found Unix and Windows Keyloggers from Github. Free to play with this https://www.adlice.com/kernelmode-rootkits-part-3-kernel-filters/ and have fun!
That, what you want is dangerous!
With the same code everyone can create keylogger*
But if you not a virus creator then I recommend you to search "hotkeying in C/C++" or something like that and hook keyboard with hotkeys!
I have written HotKey Maker in VB6 which can make hotkey from every button of keyboard.
But I really not interested in viruses :)
Good Luck !
*Virus which hooks your keyboard and send all what you wrote in keyboard to Virus owner

Resources