Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Hey everyone! This is my first post, and I hope this isn't too easy of a question. I am writing an application that uses a status bar. I want the status bar to show "caps" in all lower case when caps is not on and "CAPS" in all upper case when caps is on. I know how to handle the keypress and toggle between the two. My problem is that I don't know if caps is on or not when the application loads. Is there any way to determine this? If there is, can the same be done with Num Lock and Insert?
  • Administrators
Posted (edited)

using System;
using System.Runtime.InteropServices;

public class KeyTest
{
[DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);

public static bool CapsLock()
{
int state = GetKeyState((int)System.Windows.Forms.Keys.NumLock);
return ((state==1) || (state==-127));
}
}

it can be called like

bool b = KeyTest.CapsLock();

You could probably do similar checks for other keys (on a laptop so I don't have numlock etc :()

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

I have tried your code, but for some reason I always get true when I call KeyTest.CapsLock();

 

Ooooops, I was using Keys.NumLock in the Key Test and not Keys.CapsLock. It works perfectly!! Thanks.

Edited by grip003

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...