Simulate CAPS LOCK press in Xlib
How do I simulate a CAPS LOCK on and off depress using Xlib开发者_运维技巧?
#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;
}
- http://tronche.com/gui/x/xlib/
- http://xopendisplay.hilltopia.ca/index.html
#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;
}
Replace " " -s with < > for header files. and compile using gcc main.c -lX11 -lXtst
Posted above solution in my blog
精彩评论