2

I need to check if caps lock is pressed, and found a function that requires an int online.

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern short GetKeyState(int keyCode);
derp_in_mouth
  • 2,003
  • 4
  • 15
  • 17

2 Answers2

4

C# also has:

if (Control.IsKeyLocked(Keys.CapsLock))

Keys.CapsLock = 20 (0x14 in hex)

Holger Brandt
  • 4,324
  • 1
  • 20
  • 35
2

From googling, I found this one:

void CheckKeyState()
{
   if ((GetKeyState(VK_CAPITAL) & 0x0001)!=0)
      AfxMessageBox("Caps Lock ON!");
   else
      AfxMessageBox("Caps Lock OFF!");
}

With VK_CAPITAL being 0x14.

So you could write in C#:

public void CheckKeyState()
{
   if ((GetKeyState(0x14) & 0x0001)!=0)
      System.Windows.Forms.MessageBox.Show("Caps Lock ON!");
   else
      System.Windows.Forms.MessageBox.Show("Caps Lock OFF!");
}

Of course, what Holger says makes a lot more sense than using the P/Invoke stuff.

Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291