Registering global hotkeys

From Agar

Jump to: navigation, search

The AG_BindGlobalKey() function associates a callback routine with a specific keyboard combination, which will be honored globally, regardless of windows and focus states (if using multiple windows under an external window system such as X Windows, at least one of the Agar application windows must hold focus).

In this example, we will create a window, and register a global hotkey to gracefully terminate the application whenever Ctrl-C is pressed:

#include <agar/core.h>
#include <agar/gui.h>
 
int main(int argc, char *argv[])
{
        AG_InitCore("myapp", 0);
        AG_InitGraphics(NULL);
        AG_BindGlobalKey(AG_KEY_C, AG_KEYMOD_CTRL, AG_QuitGUI);
        AG_EventLoop();
        return (0);
}

This code registers AG_QuitGUI() to be called whenever the combination of CTRL and 'C' are pressed together (see AG_KeySym(3) for available key constants). If you need to call more code before calling AG_Quit(), you can map the key to another function:

void MyQuitFunction()
{
        /* Do something useful here */
        AG_Quit();
}
 
int main(int argc, char *argv[])
{
        ...
        AG_BindGlobalKey(AG_KEY_C, AG_KEYMOD_CTRL, MyQuitFunction);
        ...
}

See also

Personal tools