4

Basically I would like to write an app to capture whenever the Num Lock key is pressed. This needs to be supported across windows, linux, mac.

Could the app run in a browser somehow? Using HTML5 and such.

Should the app be written in Qt C++, if so, does Qt library have a cross platform call to interceptNumLock()?

Could it possibly be done on Java?

Through a cross browser add-on?

what are the pros and cons of the above approaches? If so, I can decide after I consider the cons and pros.

KJW
  • 15,035
  • 47
  • 137
  • 243

4 Answers4

1

You will have to use some native code in order to intercept any keystrokes not normally dispatched to your application by the OS.

In Java, you could use some JNA to accomplish this.

This question has an example.

Community
  • 1
  • 1
Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
1

You can do it in Java. Windows, Linux and MacOS all support Java. It's a one liner:

boolean zCapsLockOn = java.awt.Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

To do the same thing in binary code would require you (A) assume its an x86 machine, and (B) detect the operating system, which would be complex. If you did this, though, you could then make the OS-specific calls to get the capslock state based on which OS it was.

You can potentially do this platform check with Qt using code like this:

bool QMyClass::checkCapsLock()
{
    // platform dependent method of determining if CAPS LOCK is on
#ifdef Q_OS_WIN32 // MS Windows version
    return GetKeyState(VK_CAPITAL) == 1;
#else // X11 version (Linux/Unix/Mac OS X/etc...)
    Display * d = XOpenDisplay((char*)0);
    bool caps_state = false;
    if (d)
    {
        unsigned n;
        XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
        caps_state = (n & 0x01) == 1;
    }
    return caps_state;
#endif
}

Note that you cannot write binary code to get the CAPSLOCK state directly from the BIOS without going through the operating system, which is why any such code is OS-dependent.

Tyler Durden
  • 11,156
  • 9
  • 64
  • 126
  • So Java is the best way to go about this? – KJW Oct 17 '12 at 21:03
  • 1
    It depends on your objectives and how much time you want to spend. Java is definitely the easiest, fastest way to do a cross-platform caps lock detection, but has the downside that the Java runtime must be available on the machine. – Tyler Durden Oct 18 '12 at 14:28
0

I would do it as a normal website application running on browser, in this case the better is to use the jQuery library which will give you cross-browser compatibility among browsers and systems:

See my working demo of a jQuery caps-lock detector. You can try it in Windows / Linux and different browsers (Firefox, Chrome, IE).

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • what I am looking for is not a website but a way to capture capslock anywhere on the desktop operating screeen. – KJW Oct 09 '12 at 19:47
  • Running in background no matter what program is executing? if so you need something like a keylogger then, I can't help with that.. – Nelson Oct 09 '12 at 19:52
  • I just need to know when a user presses the num lock, and it will start the software. This is not possible on a javascript page. – KJW Oct 09 '12 at 20:55
  • In your caps lock detector, with caps lock ON, Shift+letter says "CAPS LOCK: OFF" which is wrong. Since caps lock detection is usefull for password fields, this is a serious bug. – oxygen Oct 16 '12 at 17:20
0

Qt 4.7.4: Is there a way to find out the status of CAPS LOCK?

Here is the way to do it in c/c++. If you then use a keyPressEvent in Qt you should be able to detect whenever a user presses a key and you can than check the state of the button.

Community
  • 1
  • 1
fonZ
  • 2,428
  • 4
  • 21
  • 40