Comment simuler une activation et désactivation de la fonction CAPS LOCK à l'aide de Xlib?Simulation CAPS LOCK appuyer sur Xlib
3
A
Répondre
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;
}
Remplacer "" -s par <> pour les fichiers d'en-tête. et compiler en utilisant main.c gcc -lX11 -lXtst
Publié solution ci-dessus dans mon blog
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;
}