Understanding of global hotkeys and events in general

Discussion in 'Mac Programming' started by xqbt, Jan 20, 2016.

  1. xqbt macrumors newbie

    Jan 20, 2016

    I am writing a C++ program and I want to integrate global hotkeys capabilities. There are ways to implement global hotkeys in objective-c, like by using DDHotkey or CGEventTap.

    Setting aside the problems of invoking objective-c code from c++ code, my question is quite simple. (Using one of the techniques noted above) After I register a hotkey and I press that hotkey, what happens next? Is the program suspended and the event handler called right away? Or do I just call some function (whenever I want to), which calls the appropriate handlers if a hotkey was fired by that time? What is the program's flow like?

    As you might see, I do not understand the big picture. All I see is a pile of code snippets, which tell me nothing. I am very new to Mac OS X development.

    Extra credit question: is DDHotkey legacy, because it's based on Carbon, which is deprecated?

    Please, let me kindly ask you to clarify the above points.
  2. cruisin macrumors 6502a


    Apr 1, 2014
    The general flow is not unique to OSX. I believe that most if not all devices since the beginning use something similar.

    The first google search result for DDHotkey is a cocoa class: https://github.com/davedelong/DDHotKey. So it looks like it will still work.

    The simplest method is to have your program continuously wait for input. On every input check against the current hotkey definition and jump to the correct handler or just ignore if it isn't one of the chosen hotkeys (or show an error). Then return to the waiting cycle.

    The more involved method is to make the program call your functions asynchronously, that is to never suspend just call handlers based on the hotkey.

    It really depends on what you are doing.
  3. xqbt, Jan 22, 2016
    Last edited: Jan 22, 2016

    xqbt thread starter macrumors newbie

    Jan 20, 2016
    Thanks for the reply :)
    The thing is that, looking at DDHotkeyCenter.h from the provided link, it looks like there are only facilities to register/unregister a hotkey. I do not understand how to perform the "input check". BTW, yeah, I want synchronous stuff - just wait for an input.
  4. xqbt thread starter macrumors newbie

    Jan 20, 2016
    Hmmm, alright, thanks, I will reflect on that link.

    BTW, what code is executed after return NSApplicationMain(...) statement? I plowed through all the files and I have no clue.

Share This Page