【发布时间】:2019-05-31 07:39:14
【问题描述】:
我想记录同时按下的按键并使用 C 在 x11 中测试一个功能。例如,我可以设置类似, 如果按下“Q”, 让窗口调整大小。
但我找不到使用 Ctrl+Enter 等组合键执行相同操作的方法,因此当按下“Ctrl+Enter”时,窗口会调整大小。
x11 中是否有任何事件类型或掩码或函数来记录这些同时发生的关键事件?
下面的代码是我迄今为止编写的用于记录单个键并执行指定操作的代码。
// USES KEYBOARD KEY TO RESIZE A WINDOW
// Compile : gcc -o go key_and_win.c -lX11
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
Display *d;
Window window;
XEvent event, ev;
int s;
/* open connection with the server */
d = XOpenDisplay(NULL);
if (d == NULL)
{
fprintf(stderr, "Cannot open d\n");
exit(1);
}
s = DefaultScreen(d);
/* create window */
window = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 200, 1,
BlackPixel(d, s), BlackPixel(d, s));
/* select kind of events we are interested in */
XSelectInput(d, window, StructureNotifyMask | ExposureMask | KeyPressMask | KeyReleaseMask );
/* map (show) the window */
XMapWindow(d, window);
/* event loop */
while (1)
{
XNextEvent(d, &event);
/* keyboard events */
if (event.type == KeyPress)
{
printf( "KeyPress: %x\n", event.xkey.keycode );
if(event.xkey.keycode == 0x18) // Resize on pressing Q as, key Q => 0x18
{
printf("Here in Q\n");
int r = XResizeWindow(d, window, 100, 200); // Resizing the window through Q keypress
if(r==BadValue || r==BadWindow)
printf("Error in resizing\n");
XNextEvent(d, &event); // To get ConfigureNotify event
if(event.type == ConfigureNotify)
printf("Resized!\n");
else
printf("Not resized\n");
//XMapWindow(d, window); // Map the resized window (not necessary)
}
/* exit on ESC key press */
if ( event.xkey.keycode == 0x09 )
break;
}
}
/* close connection to server */
XCloseDisplay(d);
return 0;
}
【问题讨论】: