c++ - Launch application function using keyboard shortcut in GNU/Linux -
i created application using qt in gnu/linux , run in background. want execute application functionalities when user presses key combinations, example ctrl+alt+a...
i know possible, gnome pie don't know how can capture keys. tried using examples provided in question none of them worked...also wouldn't want run application root...
can point me resources or give me hints on that?
edit:
@iharob suggested should use libkeybinder. found it, tried uses gtk , gtk doesn't play qt...i'm not gtk beginner, never worked think gtk event loop conflicts qt event loop; when emit qt signal callback gets called after key pressed(which after gtk_init called) application crashes.
what great if create class emits signal whenever keyboard key combination pressed(e.g. ctrl+alt+a).
as far see , @samvarshavchik pointed out libkeybinder uses libx11 in background use libx11 in order rid of gtk event loop not qt friendly. afaik kde's kaction uses same technique global short keys think technique play qt's event loop.
these things being said, can use hot-key example presented here:
x11_hot_key.pro:
#------------------------------------------------- # # project created qtcreator 2015-05-04t01:47:22 # #------------------------------------------------- qt += core qt -= gui target = x11_hot_key config += console config -= app_bundle template = app sources += main.cpp config += link_pkgconfig pkgconfig += x11 main.cpp:
#include <qcoreapplication> #include <iostream> #include <x11/xlib.h> #include <x11/xutil.h> using namespace std; int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); display* dpy = xopendisplay(0); window root = defaultrootwindow(dpy); xevent ev; unsigned int modifiers = controlmask | shiftmask; int keycode = xkeysymtokeycode(dpy,xk_y); window grab_window = root; bool owner_events = false; int pointer_mode = grabmodeasync; int keyboard_mode = grabmodeasync; xgrabkey(dpy, keycode, modifiers, grab_window, owner_events, pointer_mode, keyboard_mode); xselectinput(dpy, root, keypressmask ); while(true) { bool shouldquit = false; xnextevent(dpy, &ev); switch(ev.type) { case keypress: cout << "hot key pressed!" << endl; xungrabkey(dpy,keycode,modifiers,grab_window); shouldquit = true; default: break; } if(shouldquit) break; } xclosedisplay(dpy); return a.exec(); } or use this simple library presented here has simple examples handy makefile along with.
as don't have knowledge of asynchronous correspondent xgrabkey, problem have while(true) loop never returns , blocks main thread application want move in separate thread , connect main thread using signals , slots. shouldn't big issue though , won't affect application's performance because afaik xnextevent blocks until key hit processor won't uselessly processing...
hope helps.
Comments
Post a Comment