C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-13-2008, 05:17 AM   #1
Registered User
 
Join Date: Nov 2006
Posts: 36
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!!
obaid is offline   Reply With Quote
Old 07-18-2008, 08:28 PM   #2
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,059
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");
        }
    }
}
BobS0327 is offline   Reply With Quote
Old 07-19-2008, 04:13 AM   #3
Registered User
 
Join Date: Nov 2006
Posts: 36
thankz budy!!
i will try this code.. thank you, again
obaid is offline   Reply With Quote
Old 07-28-2008, 02:06 PM   #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");
        }
    }
}
sangfroid is offline   Reply With Quote
Old 07-28-2008, 03:57 PM   #5
System Novice
 
siavoshkc's Avatar
 
Join Date: Jan 2006
Location: Tehran
Posts: 1,075
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.
__________________
Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2)

Learn the language before using it. (C++ Books and C Books)
Read the FAQ before making a problem.
Then make a Google and Forum search.

My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!)

SiavoshKC
siavoshkc is offline   Reply With Quote
Old 07-28-2008, 04:20 PM   #6
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,814
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.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 07-29-2008, 06:17 AM   #7
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,059
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.
BobS0327 is offline   Reply With Quote
Old 07-30-2008, 05:27 AM   #8
System Novice
 
siavoshkc's Avatar
 
Join Date: Jan 2006
Location: Tehran
Posts: 1,075
Quote:
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.
__________________
Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2)

Learn the language before using it. (C++ Books and C Books)
Read the FAQ before making a problem.
Then make a Google and Forum search.

My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!)

SiavoshKC
siavoshkc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Write a Program in C to detect the Ethernet Cable? christyyim C Programming 1 03-11-2009 04:12 AM
Detect Running Programs? MattKamil Windows Programming 2 07-03-2006 06:13 AM
Edge Detect 250co C Programming 0 05-22-2006 08:51 PM
detect variable overflow surdy C Programming 9 05-13-2004 12:49 PM
Using RS232 25pin i need a program to detect magnitude and frequency of lightning ajastru2000 C++ Programming 5 11-20-2003 01:27 AM


All times are GMT -6. The time now is 08:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22