0

I am trying to install an event tap for my own process to intercept mouse events.

For this I found the function CGEventTapCreateForPSN which expects a process' serial number as its first parameter. How do I get the PSN of my own process? GetCurrentProcess is deprecated, GetProcessInformation has also been removed.

I know about CGEventTapCreate but from what I understand that installs a system-wide hook and that doesn't sound like a good idea or something I'd like to do. I also know about CGEventTapCreateForPid but I can't use that because I am also targeting older MacOSX versions that don't have that function and it also appears to be undocumented so who knows when it will go away.

FYI, I don't own the actual program, my code is only loaded into the process as a dynamic library so I cannot just modify the program's code or anything like that.

user3700562
  • 693
  • 11
  • 23
  • Can you create a [local event monitor](https://developer.apple.com/documentation/appkit/nsevent/1534971-addlocalmonitorforeventsmatching)? It requires using Cocoa, and so either Swift or Objective-C, while event taps are a C API. Also, event taps are restricted on macOS 10.14 to apps which have been granted control over the system. Lastly, if you're worried about `CGEventTapCreateForPid()` because it's too new: 1) presence in (non-private) header files is official documentation of support; 2) you could weak link it, check if it's available, and fall back to `GetCurrentProcess()` if not. – Ken Thomases May 09 '19 at 19:35
  • I can only use APIs that can be invoked from C. About event taps being restricted...does this also apply to local event taps that you install in your own process from your own process? – user3700562 May 09 '19 at 19:56
  • I haven't tested. The WWDC video which described the restrictions suggested it applied to all event taps without making an exception for your process. It does seem like that should be allowed, though. – Ken Thomases May 09 '19 at 20:12

0 Answers0