More robust key handling

This commit is contained in:
Bert 2011-01-21 10:30:10 +01:00
parent 9dc46c1d81
commit 2d5247af4a
1 changed files with 27 additions and 10 deletions

37
main.c
View File

@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include "sxiv.h" #include "sxiv.h"
@ -112,44 +113,60 @@ void cleanup() {
} }
void on_keypress(XEvent *ev) { void on_keypress(XEvent *ev) {
char key;
int len;
KeySym keysym; KeySym keysym;
if (!ev) if (!ev)
return; return;
keysym = XLookupKeysym(&ev->xkey, 0); len = XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
switch (keysym) { switch (keysym) {
case XK_Escape: case XK_Escape:
cleanup(); cleanup();
exit(1); exit(2);
case XK_q: case XK_space:
key = 'n';
len = 1;
break;
case XK_BackSpace:
key = 'p';
len = 1;
break;
}
if (!len)
return;
printf("%c\n", key);
switch (key) {
case 'q':
cleanup(); cleanup();
exit(0); exit(0);
case XK_n: case 'n':
case XK_space:
if (fileidx + 1 < filecnt) { if (fileidx + 1 < filecnt) {
img_load(&img, filenames[++fileidx]); img_load(&img, filenames[++fileidx]);
img_display(&img, &win); img_display(&img, &win);
update_title(); update_title();
} }
break; break;
case XK_p: case 'p':
case XK_BackSpace:
if (fileidx > 0) { if (fileidx > 0) {
img_load(&img, filenames[--fileidx]); img_load(&img, filenames[--fileidx]);
img_display(&img, &win); img_display(&img, &win);
update_title(); update_title();
} }
break; break;
case XK_plus: case '+':
case XK_equal: case '=':
if (img_zoom_in(&img)) { if (img_zoom_in(&img)) {
img_render(&img, &win); img_render(&img, &win);
update_title(); update_title();
} }
break; break;
case XK_minus: case '-':
if (img_zoom_out(&img)) { if (img_zoom_out(&img)) {
img_render(&img, &win); img_render(&img, &win);
update_title(); update_title();