Results 1 to 5 of 5

Thread: how to determine if CapsLock is on [crossplatform]

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default how to determine if CapsLock is on [crossplatform]

    hi all!
    i'll tell you how to determine the state of CapsLock (this code can be modifyed for NumLock/ScrollLock, but you are to do that by yourself)

    note: this will work under windows and any system that uses X11.

    the function is simple:

    Qt Code:
    1. bool QMyClass::checkCapsLock()
    2. {
    3. // platform dependent method of determining if CAPS LOCK is on
    4. #ifdef Q_OS_WIN32 // MS Windows version
    5. return GetKeyState(VK_CAPITAL) == 1;
    6. #else // X11 version (Linux/Unix/Mac OS X/etc...)
    7. Display * d = XOpenDisplay((char*)0);
    8. bool caps_state = false;
    9. if (d)
    10. {
    11. unsigned n;
    12. XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
    13. caps_state = (n & 0x01) == 1;
    14. }
    15. return caps_state;
    16. #endif
    17. }
    To copy to clipboard, switch view to plain text mode 

    but we must have some includes:

    Qt Code:
    1. #ifdef Q_OS_WIN32
    2. # include <windows.h>
    3. #else
    4. # include <X11/XKBlib.h>
    5. #endif
    To copy to clipboard, switch view to plain text mode 

    to use X11/XKBLib.h, you have to install libx11-dev and link with -lX11

    but there are some troubles!

    for example, let's see eventFilter:

    Qt Code:
    1. bool QMyClass::eventFilter(QObject* obj, QEvent* event)
    2. {
    3. if (event->type() == QEvent::KeyPress) {
    4. // ...
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    look's good, but won't work under X11:

    Qt Code:
    1. error: expected token ':' got '2'
    To copy to clipboard, switch view to plain text mode 

    why? what happened?
    everything is simple. just look at X11/X.h:

    Qt Code:
    1. [186] #define KeyPress 2
    To copy to clipboard, switch view to plain text mode 

    so that's that! we need to #undef those defines that conflicts with QEvent::Type enum:

    Qt Code:
    1. #ifdef Q_OS_WIN32
    2. # include <windows.h>
    3. #else
    4. # include <X11/XKBlib.h>
    5. # undef KeyPress
    6. # undef KeyRelease
    7. # undef FocusIn
    8. # undef FocusOut
    9. // etc...
    10. #endif
    To copy to clipboard, switch view to plain text mode 

    this will make some troubles if you want to use some specific X11 features, but i think if you are using Qt, you don't need that ;]

    that's all, see ya =)

    PS: sorry for my bad english

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,530
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to determine if CapsLock is on [crossplatform]


  3. #3
    Join Date
    Jun 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to determine if CapsLock is on [crossplatform]

    Hi, I've a similar problem. I need to check which is the physical modifiers' positions, Control, Shift and Alt. On Windows I did it handling the winEvents I'm trying to do the same in linunx handling the x11events but I can't check if the current XEvent is a KeyPress event. One of the problems is what you mentioned, I needed to undef the X11 KeyPress macro. I have tried to compare the XEvent type with 2 because this is the constant declared in X11/X.h but didn't work.
    Qt Code:
    1. maiko@cits-d530:~/Projetos/Positivo$ grep -i keypress /usr/include/X11/X.h
    2. #define KeyPressMask (1L<<0)
    3. #define KeyPress 2
    To copy to clipboard, switch view to plain text mode 
    this is my current code
    Qt Code:
    1. bool MainWindow::x11Event(XEvent *xe)
    2. {
    3. //TODO: remove #undef KeyPress
    4. if(xe->type == 2)
    5. {
    6. switch (xe->xkey.keycode)
    7. {
    8. case 37://Control_L
    9. keyPressEvent(xe->xkey.keycode, "Key_ControlL");
    10. break;
    11. case 105://Control_R
    12. keyPressEvent(xe->xkey.keycode, "Key_ControlR");
    13. break;
    14. case 50://Shift_L
    15. keyPressEvent(xe->xkey.keycode, "Key_ShiftL");
    16. break;
    17. case 62://Shift_R
    18. keyPressEvent(xe->xkey.keycode, "Key_ShiftR");
    19. break;
    20. default:
    21. return false;
    22. }
    23. return true;
    24. }
    25. return false;
    26. }
    To copy to clipboard, switch view to plain text mode 

    Bye and thx.
    Last edited by MaikoID; 2nd May 2011 at 16:24.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,972
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to determine if CapsLock is on [crossplatform]

    Quote Originally Posted by Lesiok View Post
    Looked at QApplication::keyboardModifiers(). I don't see anything there that you could use to tell the state of the NumLock, CapsLock, or ScrollLock keys. My interpretation of this method is that if I had pressed Shift, Alt, or one of the other modifier keys while pressing another key, this would tell me that. The "Lock" keys are not modifier keys.

  5. #5

    Default Re: how to determine if CapsLock is on [crossplatform]

    I am also curious about num, scroll, and kana locks. If

    XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
    caps_state = (n & 0x01) == 1;

    will give us caps lock's state, and I know that

    num_state = (n & 0x02) == 2;

    will give us number lock's state, and I believe that

    scroll_state = (n & 0x04) == 4;

    should give us the scroll lock's state, but what about Kana lock? 0x08????

Similar Threads

  1. Determine the Process ID
    By Jimmy2775 in forum General Programming
    Replies: 5
    Last Post: 7th October 2011, 09:16
  2. Replies: 0
    Last Post: 11th November 2009, 19:23
  3. Looking for a crossplatform library that works with zip archives
    By Berberis in forum General Programming
    Replies: 1
    Last Post: 10th March 2009, 07:06
  4. How to determine available memory
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2007, 00:00
  5. How to determine if my app is active...
    By Ben.Hines in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 16:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.