12Mar/100
caps lock
как программно определить, включен ли caps lock?
решение платформозависимо.
Windows:
#include <windows.h> int i = GetKeyState(VK_CAPITAL); bool caps_on = (i == 1);
что угодно, но с иксами (X11), будь то Linux, Unix, etc...:
(тянет за собой libx11-dev, требует -lX11)
#include <X11/X.h> #include <X11/Xlib.h> #include <X11/XKBlib.h> Display * Dpy = XOpenDisplay((char*)0); bool caps_on = false; if (Dpy) { unsigned n; XkbGetIndicatorState(Dpy, XkbUseCoreKbd, &n); caps_on = (n & 0x01) == 1; }
UPD: как показал опыт, в иксах включать достаточно XKBLib.h. при этом если это юзать в QT, то возникают глюки использования QEvent (см. X11/X.h):
#define KeyPress 2 #define KeyRelease 3 #define ButtonPress 4 #define ButtonRelease 5 #define MotionNotify 6 #define EnterNotify 7 #define LeaveNotify 8 #define FocusIn 9 #define FocusOut 10 #define KeymapNotify 11 #define Expose 12 #define GraphicsExpose 13 #define NoExpose 14 #define VisibilityNotify 15 #define CreateNotify 16 #define DestroyNotify 17 #define UnmapNotify 18 #define MapNotify 19 #define MapRequest 20 #define ReparentNotify 21 #define ConfigureNotify 22 #define ConfigureRequest 23 #define GravityNotify 24 #define ResizeRequest 25 #define CirculateNotify 26 #define CirculateRequest 27 #define PropertyNotify 28 #define SelectionClear 29 #define SelectionRequest 30 #define SelectionNotify 31 #define ColormapNotify 32 #define ClientMessage 33 #define MappingNotify 34 #define LASTEvent 35 /* must be bigger than any event # */
это конфликтует с этим (см. qcoreevent.h):
enum Type { /* If you get a strange compiler error on the line with None, it's probably because you're also including X11 headers, which #define the symbol None. Put the X11 includes after the Qt includes to solve this problem. */ None = 0, // invalid event Timer = 1, // timer event MouseButtonPress = 2, // mouse button pressed MouseButtonRelease = 3, // mouse button released MouseButtonDblClick = 4, // mouse button double click MouseMove = 5, // mouse move KeyPress = 6, // key pressed KeyRelease = 7, // key released FocusIn = 8, // keyboard focus received FocusOut = 9, // keyboard focus lost Enter = 10, // mouse enters widget Leave = 11, // mouse leaves widget Paint = 12, // paint widget Move = 13, // move widget Resize = 14, // resize widget Create = 15, // after widget creation Destroy = 16, // during widget destruction Show = 17, // widget is shown Hide = 18, // widget is hidden Close = 19, // request to close widget Quit = 20, // request to quit application ParentChange = 21, // widget has been reparented ParentAboutToChange = 131, // sent just before the parent change is done // ... etc ...
в частности в пунктах FocusIn, FocusOut, KeyPress...
так что если юзать указанный мной подход, надо делать #undef для конфликтующих имён.
#include <X11/XKBlib.h> #undef KeyPress #undef FocusIn // ... etc ...
так что этот подход лишает нас возможности юзать события иксов в QT. что ж, не очень-то и хотелось ))
UPD 2: а вообще лучше это вынести в отдельный файл, тогда конфликтов не будет.