Detect Caps Lock C #

6

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

    
asked by anonymous 03.05.2014 / 13:37

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 your code:

using System.Runtime.InteropServices;

Here is the actual detection:

[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" ) ); 
    
03.05.2014 / 15:26