Global Mouse Moved Events in Cocoa

前端 未结 3 1692
感动是毒
感动是毒 2020-12-25 09:55

Is there a way to register for global mouse moved events in Cocoa? I was able to register for the events using Carbon\'s InstallEventHandler(), but would prefer

3条回答
  •  感动是毒
    2020-12-25 10:07

    A similar question was already asked on StackOverflow: How to make a transparent NSView subclass handle mouse events?

    To summarize, the tansparent view method didn't work. Quartz Event Taps seem to be the best answer.

    Here are some hints on working with taps:

    1. Create the tap with CGEventTapCreate.
      a) For the location (first) parameter you'll probably want to use kCGSessionEventTap.
      b) For the placement (second) parameter you'll probably want kCGHeadInsertEventTap.
      c) For the event mask parameter, try (1 << kCGEventMouseMoved).

    2. Create a run loop source with CFMachPortCreateRunLoopSource, passing the event tap as the second parameter.

    3. Add the run loop source to your run loop. Assuming you want it added to the main run loop, do: CFRunLoopAddSource(CFRunLoopGetMain(), sourceFromStep2, kCFRunLoopDefaultMode);

    4. Enable the event tap with CGEventTapEnable

提交回复
热议问题