Thread: How to detect that capslock in on in C#

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    39

    How to detect that capslock in on in C#

    Hi every one,
    How to detect that capslock in on in C# and How to Close it!!

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    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");
            }
        }
    }

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    39
    thankz budy!!
    i will try this code.. thank you, again

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    Hi Bob,
    This piece of code is really wonderful....but i didn't get the details of it...

    can you provide me any good links from where i can learn such dll programming ?

    Thank you


    Quote Originally Posted by BobS0327 View Post
    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");
            }
        }
    }

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    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.

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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...
    Maybe C# is not the right choice to write such programs. C++ will be the best choice for programs that call many system functions.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Write a Program in C to detect the Ethernet Cable?
    By christyyim in forum C Programming
    Replies: 1
    Last Post: 03-11-2009, 04:12 AM
  2. Detect Running Programs?
    By MattKamil in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2006, 06:13 AM
  3. Edge Detect
    By 250co in forum C Programming
    Replies: 0
    Last Post: 05-22-2006, 08:51 PM
  4. detect variable overflow
    By surdy in forum C Programming
    Replies: 9
    Last Post: 05-13-2004, 12:49 PM
  5. Replies: 5
    Last Post: 11-20-2003, 01:27 AM