Hi every one,
How to detect that capslock in on in C# and How to Close it!!
Hi every one,
How to detect that capslock in on in C# and How to Close it!!
Code:using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class CapsLockControl { [DllImport("user32.dll")] static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,UIntPtr dwExtraInfo); const int KEYEVENTF_EXTENDEDKEY = 0x1; const int KEYEVENTF_KEYUP = 0x2; public static void Main() { if (Control.IsKeyLocked(Keys.CapsLock)) { Console.WriteLine("Caps Lock key is ON. We'll turn it off"); keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0); keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr) 0); } else { Console.WriteLine("Caps Lock key is OFF"); } } }
thankz budy!!
i will try this code.. thank you, again
You should avoid this kind of code. Using Control.IsKeyLocked(Keys.CapsLock) has no problem. But calling a function in a system DLL should be avoided as long as possible. You can make all letters capitel or lower case in a control input.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Yes while this works I wouldn't think it's the best approach from a C# standpoint. I believe this information is provided to you in the key down/up handlers.
Here is Microsoft's approach to toggling the caps lock key in VB. Please note that the VB example is using pinvoke. So, you'll have to translate VB to C#. Also, virtual keys such as the caps lock aren't processed in the key up and keydown handlers.
Here is a link to a platform invoke tutorial.
Please note that C# has very little capability to address system level issues. For instance, if you're writing a C# GUI that will do network monitoring, a lot of your code will be pinvoke code. Reason being, C# does not have the "native" ability to ping a workstation to determine if it is on the network. C# does not have the "native" ability to query the remote workstation's available hard drive space. C# does not have the "native" ability to query the total uptime of the remote workstation. C# does not have the "native" ability to determine who is logged on to the remote workstation. I could go on and on. So, pinvoke to is used to more or less "flesh out" an application if you're doing any type of systems level programmingl. You can also use this same argument for the advanced GUI (GDI) drawing.
Here is link to a lot of pinvoke info.
Maybe C# is not the right choice to write such programs. C++ will be the best choice for programs that call many system functions.Please note that C# has very little capability to address system level issues. For instance, if you're writing a C# GUI that will do network monitoring, a lot of your code will be pinvoke code. Reason being, C# does not have the "native" ability to ping a workstation to determine if it is on the network...
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C