0

I need to check if the the Caps-Lock in on or off.

I have tried this function

bool CheckKeyState()
{
   if ((GetKeyState(VK_CAPITAL) & 0x0001)!=0)
      return true;

    return false;

}

I had to include the windows library

#include <Windows.h>

But it is returning true weather the caps is on or off.

what am I doing wrong? how can I check if the Caps-Lock in on or not?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • This is not really a C++ question as the answer depends on the OS, more a winapi question. Looking on the net, your code seems correct: https://vcpptips.wordpress.com/2009/01/19/how-to-check-the-caps-lock-is-on-or-off/ – coyotte508 Apr 18 '15 at 20:11
  • Your code works for me too – gomons Apr 18 '15 at 20:17
  • @Remy: not a duplicate; the code the OP started with appears to be the same as the code given in the answers to that question. Voting to reopen. – Harry Johnston Apr 19 '15 at 00:13
  • Mike, can you please provide [an MCVE](http://stackoverflow.com/help/mcve). Also, please confirm that you're not running your code inside a virtual machine, over a Remote Desktop session, etc. – Harry Johnston Apr 19 '15 at 00:14
  • @HarryJohnson: all the more reason why it is a duplicate. It is asking the same question, using code from a previous answer. – Remy Lebeau Apr 19 '15 at 16:04
  • @RemyLebeau: I don't think "how am I supposed to do this" is really the same as "this is how you're supposed to do this, why doesn't it work"? (Granted, we need more information.) – Harry Johnston Apr 19 '15 at 22:09

1 Answers1

1

I would use GetAsyncKeyState( int vKey ) if you're having problems with GetKeyState( )

To be more specific:

GetAsyncKeyState( VK_CAPITAL );

To retrieve status information for an individual key, use the GetKeyState function. To retrieve the current state for an individual key regardless of whether the corresponding keyboard message has been retrieved from the message queue, use the GetAsyncKeyState function.

Documentation

Your program must have the focus to be able to properly check the KeyState.

If all of this is not working out for you, just write a Keyboard Hook using SetWindowsHookEx Function.

deW1
  • 5,562
  • 10
  • 38
  • 54
  • Thanks you. I am getting the same result from `GetAsyncKeyState( VK_CAPITAL )` it is returning true either way. – Jaylen Apr 18 '15 at 21:17
  • @Mike are you sure your program has the focus when executing the function? Another thing could be that your key is not in the message queue when executing the function. I would try to put that statement in a while loop breaking out of it when it recognizes that caps is active then you can test around while your program is running. – deW1 Apr 18 '15 at 21:53
  • @Mike edited as well. – deW1 Apr 18 '15 at 22:00
  • For some reason `GetAsyncKeyState` doesn't work in command line program. The other one should work `(GetKeyState(VK_CAPITAL) & 0x0001)!=0)` unless you want to know if shift-key is down? – Barmak Shemirani Apr 19 '15 at 00:50
  • The GetKeyState is not working either. How would I know if my program is on focus? I am using the command line – Jaylen Apr 19 '15 at 01:05
  • @Mike to be honest I would save myself some trouble and just write the keyboard hook. Good keyloggers usually use keyboard hooks and they're working perfectly. – deW1 Apr 19 '15 at 01:30
  • @Mike: "*I am using the command line*" - did you make a console app? Or a GUI app that accepts command-line parameters? `GetKeyState()` depends on processing GUI messages in a message loop, which console apps do not have. [What's the difference between GetKeyState and GetAsyncKeyState?](http://blogs.msdn.com/b/oldnewthing/archive/2004/11/30/272262.aspx): "*GetKeyState returns the virtual key state. In other words, GetKeyState reports the state of the keyboard based on the messages you have retrieved from your input queue. This is not the same as the physical keyboard state.*" – Remy Lebeau Apr 19 '15 at 16:10
  • This console program should print 1 when caps-lock is on: `int main(){ cout << GetKeyState(VK_CAPITAL); return 0; }` at least it does on my computer. – Barmak Shemirani Apr 19 '15 at 18:21