grip003 Posted September 2, 2004 Posted September 2, 2004 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? Quote
Administrators PlausiblyDamp Posted September 2, 2004 Administrators Posted September 2, 2004 (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 September 3, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
grip003 Posted September 3, 2004 Author Posted September 3, 2004 (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 September 3, 2004 by grip003 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.