How to intercept a hot key in Cocoa when the application window is not active

前端 未结 2 1497
花落未央
花落未央 2021-02-01 00:02

I am trying to create a utility that doesn\'t open a window when executed, and that would be activated from a hot key; I read that currently Cocoa doesn\'t have a function for t

2条回答
  •  旧时难觅i
    2021-02-01 00:12

    Use the Carbon Event Manager's RegisterEventHotKey function. This function is supported in 64-bit (notice that it lacks the “not available in 64-bit” availability note).

    Conversely, NSEvent's new addGlobalMonitorForEventsMatchingMask:handler: method in Snow Leopard is not the easiest way to implement a hot-key. For one thing, it requires that the user have access for assistive devices turned on; moreover, it requires you to examine every event yourself, as compared with the RegisterEventHotKey system, which only calls your callback function when the user presses the specific key you registered for.

提交回复
热议问题