Detect Caps Lock C#

Asked

Viewed 363 times

6

I made an application to detect the state of caps lock in C#, but I found a problem, when the application loses focus events can no longer be triggered causing notifications to no longer be displayed by the application. I took a brief look at the internet and saw that using windows DLL’s is possible to do this. Anyone have any suggestions?

  • 1

    The answer provided answered what you wanted? You can accept it with correct. If you know how, see in [about]

1 answer

5


Using Console.Capslock

 Console.WriteLine( "Caps Lock " + ( Console.CapsLock ? "Ligado" : "Desligado" ) ); 

To check the status after returning to the application, it would be the case to do a Polling.

Using DLL:

Add to the code:

using System.Runtime.InteropServices;

This is the detection itself:

[DllImport("user32.dll", CharSet = CharSet.Auto,
   ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]

public static extern short GetKeyState(int keyCode); 

// CapsLock = 0x14, NumLock = 0x90 e ScrollLock = 0x91
bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;

Console.WriteLine( "Caps Lock " + ( CapsLock ? "Ligado" : "Desligado" ) ); 
  • Wouldn’t it be worth linking to the list of other existing virtual-key codes? http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx

  • The specifics for state keys (which are independent of being pressed), I put in the comment. The others would normally use c# even, not need the DLL. In any case, your comment link is good to add this extra information if someone is interested.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.