如何使用Xlib模拟CAPS LOCK开启和关闭?
答案 0 :(得分:2)
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
// Function: Click Key (press and release key)
void click_key( Display* p_display , KeyCode keycode ) {
XTestFakeKeyEvent( p_display , keycode , True , 0 ); // key press event
XTestFakeKeyEvent( p_display , keycode , False , 0 ); // key release event
XFlush( p_display );
return;
}
// Main
int main() {
Display* p_display = XOpenDisplay( NULL );
KeySym keysym = XK_VoidSymbol;
KeyCode keycode = NoSymbol;
keysym = XK_Caps_Lock;
keycode = XKeysymToKeycode( p_display , keysym );
click_key( p_display , keycode ); // activates CapsLock
click_key( p_display , keycode ); // deactivates CapsLock
XCloseDisplay( p_display );
return 0;
}
答案 1 :(得分:1)
#include "stdio.h"
#include "X11/Xlib.h"
#include "X11/keysym.h"
#include "time.h"
Display *display=NULL;
unsigned int keycode;
int main ()
{
display = XOpenDisplay(NULL);
keycode = XKeysymToKeycode(display, XK_Caps_Lock);
printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);
/* type something here */
printf ("\npressed\n");
XTestFakeKeyEvent(display, keycode, True, CurrentTime);
XFlush(display);
printf ("\nreleased\n");
XTestFakeKeyEvent(display, keycode, False, CurrentTime);
XFlush(display);
sleep(3);
return 0;
}
将“-s”替换为&lt; &GT;用于头文件。 并使用编译 gcc main.c -lX11 -lXtst
在我的blog
中发布了上述解决方案