didjoman
August 10th, 2012, 10:09 PM
Hello,
I am creating a sort of window manager in bash and in C and I would like to create shortcuts for my application with the Xlib, but I have some troubles to do it ...
I need to make 2 different shortcuts :
SUPER + ALT_L + letter
SUPER + letter
The first one functions when I type SUPER, then Alt and finally the letter. But it doesn't functions when I begin by typing ALT_L ....
And the 2° doesn't functions ...
I don't really know what to do ...
Could you help me ?
(please, sorry for the possible mistakes in English, I am french)
Here is what I have done :
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
GC gc; // Contexte graphique
Display* display;
int screen;
Window root;
void swallow_keystroke(Display * dpy, XEvent * ev)
{
XAllowEvents(dpy, AsyncKeyboard, ev->xkey.time);
}
void passthru_keystroke(Display * dpy, XEvent * ev)
{
/* pass it through to the app, as if we never intercepted it */
XAllowEvents(dpy, ReplayKeyboard, ev->xkey.time);
XFlush(dpy);
}
int main(int argc, char* argv[]){
char cwd[1024];
getcwd(cwd, sizeof(cwd));
// Connexion à un serveur X
display = XOpenDisplay(NULL);
if(!display){
printf("Can not open display.\n");
exit(EXIT_FAILURE);
}
// Récupère la valeur par default des différentes variables.
screen = DefaultScreen(display);
gc = DefaultGC (display, screen);
root = RootWindow (display, screen);
// Gestion des évènements
XEvent ev;
char c;
KeyCode SUPER= XKeysymToKeycode(display, XStringToKeysym("Super_L"));
KeyCode ALT_L = XKeysymToKeycode(display, XStringToKeysym("Alt_L"));
KeyCode Z_KEY = XKeysymToKeycode(display, XStringToKeysym("z"));
KeyCode S_KEY = XKeysymToKeycode(display, XStringToKeysym("s"));
KeyCode Q_KEY = XKeysymToKeycode(display, XStringToKeysym("q"));
KeyCode D_KEY = XKeysymToKeycode(display, XStringToKeysym("d"));
KeyCode O_KEY = XKeysymToKeycode(display, XStringToKeysym("o"));
KeyCode T_KEY = XKeysymToKeycode(display, XStringToKeysym("t"));
// TAB
KeyCode RIGHT = XKeysymToKeycode(display, XStringToKeysym("Right"));
KeyCode DOWN = XKeysymToKeycode(display, XStringToKeysym("Down"));
KeyCode LEFT = XKeysymToKeycode(display, XStringToKeysym("Left"));
KeyCode UP = XKeysymToKeycode(display, XStringToKeysym("Up"));
XGrabKey(display, SUPER, AnyModifier, DefaultRootWindow(display), 1, GrabModeSync, GrabModeSync);
XGrabKey(display, ALT_L, AnyModifier, DefaultRootWindow(display), 1, GrabModeSync, GrabModeSync);
for(;;){
XNextEvent(display, &ev);
if(ev.type == KeyPress ){
if(ev.xkey.keycode == SUPER){
fprintf(stderr, "got SUPER ALT_L S\n");
while(ev.type != KeyRelease){
if(ev.xkey.keycode == Q_KEY)
fprintf(stderr, "got SUPER ALT_L S\n");
}
}
if (ev.xkey.state & Mod1Mask && ev.xkey.state & Mod4Mask){
if(ev.xkey.keycode == S_KEY){
fprintf(stderr, "got SUPER ALT_L S\n");
if(ev.type == KeyPress)
fprintf(stderr, "got SUPER ALT_L S\n");
//printf("%s",exePath);
//system(strcat(cwd,"twmpowa cmd_relative_move x -70"));
swallow_keystroke(display, &ev);
}
else if (ev.xkey.keycode == Q_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == S_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == D_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == Z_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == UP){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == DOWN){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == LEFT){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == RIGHT)
;
}
else if(ev.xkey.keycode == Q_KEY && ev.xkey.state & Mod4Mask)
fprintf(stderr, "got Super+g\n");
else if(ev.xkey.state & Mod4Mask){
fprintf(stderr, "super pressed \n");
if (ev.xkey.keycode == Q_KEY)
// fprintf(stderr, "got Super+g\n");
if(ev.type == KeyPress)
fprintf(stderr, "got Super+g\n");
swallow_keystroke(display, &ev);
if (ev.xkey.keycode == S_KEY)
;
if (ev.xkey.keycode == D_KEY)
;
if (ev.xkey.keycode == Z_KEY)
;
if (ev.xkey.keycode == O_KEY)
;
if (ev.xkey.keycode == T_KEY)
;
}
/*
else if(ev.xkey.keycode == ALT_L){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt\n");
swallow_keystroke(display, &ev);
}
*/
else{
fprintf(stderr, "got (something else)+F1, passing through\n");
passthru_keystroke(display, &ev);
}
}
}
XCloseDisplay(display);
return EXIT_SUCCESS;
}
/*
void buttonPressed(){
printf("salut");
}
*/
I am creating a sort of window manager in bash and in C and I would like to create shortcuts for my application with the Xlib, but I have some troubles to do it ...
I need to make 2 different shortcuts :
SUPER + ALT_L + letter
SUPER + letter
The first one functions when I type SUPER, then Alt and finally the letter. But it doesn't functions when I begin by typing ALT_L ....
And the 2° doesn't functions ...
I don't really know what to do ...
Could you help me ?
(please, sorry for the possible mistakes in English, I am french)
Here is what I have done :
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
GC gc; // Contexte graphique
Display* display;
int screen;
Window root;
void swallow_keystroke(Display * dpy, XEvent * ev)
{
XAllowEvents(dpy, AsyncKeyboard, ev->xkey.time);
}
void passthru_keystroke(Display * dpy, XEvent * ev)
{
/* pass it through to the app, as if we never intercepted it */
XAllowEvents(dpy, ReplayKeyboard, ev->xkey.time);
XFlush(dpy);
}
int main(int argc, char* argv[]){
char cwd[1024];
getcwd(cwd, sizeof(cwd));
// Connexion à un serveur X
display = XOpenDisplay(NULL);
if(!display){
printf("Can not open display.\n");
exit(EXIT_FAILURE);
}
// Récupère la valeur par default des différentes variables.
screen = DefaultScreen(display);
gc = DefaultGC (display, screen);
root = RootWindow (display, screen);
// Gestion des évènements
XEvent ev;
char c;
KeyCode SUPER= XKeysymToKeycode(display, XStringToKeysym("Super_L"));
KeyCode ALT_L = XKeysymToKeycode(display, XStringToKeysym("Alt_L"));
KeyCode Z_KEY = XKeysymToKeycode(display, XStringToKeysym("z"));
KeyCode S_KEY = XKeysymToKeycode(display, XStringToKeysym("s"));
KeyCode Q_KEY = XKeysymToKeycode(display, XStringToKeysym("q"));
KeyCode D_KEY = XKeysymToKeycode(display, XStringToKeysym("d"));
KeyCode O_KEY = XKeysymToKeycode(display, XStringToKeysym("o"));
KeyCode T_KEY = XKeysymToKeycode(display, XStringToKeysym("t"));
// TAB
KeyCode RIGHT = XKeysymToKeycode(display, XStringToKeysym("Right"));
KeyCode DOWN = XKeysymToKeycode(display, XStringToKeysym("Down"));
KeyCode LEFT = XKeysymToKeycode(display, XStringToKeysym("Left"));
KeyCode UP = XKeysymToKeycode(display, XStringToKeysym("Up"));
XGrabKey(display, SUPER, AnyModifier, DefaultRootWindow(display), 1, GrabModeSync, GrabModeSync);
XGrabKey(display, ALT_L, AnyModifier, DefaultRootWindow(display), 1, GrabModeSync, GrabModeSync);
for(;;){
XNextEvent(display, &ev);
if(ev.type == KeyPress ){
if(ev.xkey.keycode == SUPER){
fprintf(stderr, "got SUPER ALT_L S\n");
while(ev.type != KeyRelease){
if(ev.xkey.keycode == Q_KEY)
fprintf(stderr, "got SUPER ALT_L S\n");
}
}
if (ev.xkey.state & Mod1Mask && ev.xkey.state & Mod4Mask){
if(ev.xkey.keycode == S_KEY){
fprintf(stderr, "got SUPER ALT_L S\n");
if(ev.type == KeyPress)
fprintf(stderr, "got SUPER ALT_L S\n");
//printf("%s",exePath);
//system(strcat(cwd,"twmpowa cmd_relative_move x -70"));
swallow_keystroke(display, &ev);
}
else if (ev.xkey.keycode == Q_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == S_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == D_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == Z_KEY){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == UP){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == DOWN){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == LEFT){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt+g\n");
swallow_keystroke(display, &ev);
}
if (ev.xkey.keycode == RIGHT)
;
}
else if(ev.xkey.keycode == Q_KEY && ev.xkey.state & Mod4Mask)
fprintf(stderr, "got Super+g\n");
else if(ev.xkey.state & Mod4Mask){
fprintf(stderr, "super pressed \n");
if (ev.xkey.keycode == Q_KEY)
// fprintf(stderr, "got Super+g\n");
if(ev.type == KeyPress)
fprintf(stderr, "got Super+g\n");
swallow_keystroke(display, &ev);
if (ev.xkey.keycode == S_KEY)
;
if (ev.xkey.keycode == D_KEY)
;
if (ev.xkey.keycode == Z_KEY)
;
if (ev.xkey.keycode == O_KEY)
;
if (ev.xkey.keycode == T_KEY)
;
}
/*
else if(ev.xkey.keycode == ALT_L){
if(ev.type == KeyPress)
fprintf(stderr, "got Alt\n");
swallow_keystroke(display, &ev);
}
*/
else{
fprintf(stderr, "got (something else)+F1, passing through\n");
passthru_keystroke(display, &ev);
}
}
}
XCloseDisplay(display);
return EXIT_SUCCESS;
}
/*
void buttonPressed(){
printf("salut");
}
*/