By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,874 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,874 IT Pros & Developers. It's quick & easy.

Is it possible to turn on\off CAPS_LOCK ?

P: n/a
Hi,

Is it possible to turn on\off the CAPS LOCK button using C# program? and if
so, how can i do that?

Thanks,
Gidi
Mar 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hello Gidi,

Use
SendKeys.Send("{CAPSLOCK}");

or

PInvoke "SetKeyboardState"/"keybd_event"

[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte [] lpKeyState);

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr
dwExtraInfo);
http://msdn.microsoft.com/library/de...alKeyCodes.asp

G> Hi,
G>
G> Is it possible to turn on\off the CAPS LOCK button using C# program?
G> and if so, how can i do that?
G>
G> Thanks,
G> Gidi
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 5 '06 #2

P: n/a
Thanks Michael ,

I tried the SendKeys option, and it works only while i'm debugging, but in
regular runtime it doesn't turn on\off the capslock key.

About the second option, i didn't really understand how should i use it, so
if u can, please explain or give example...

Thanks,
Gidi.

"Michael Nemtsev" wrote:
Hello Gidi,

Use
SendKeys.Send("{CAPSLOCK}");

or

PInvoke "SetKeyboardState"/"keybd_event"

[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte [] lpKeyState);

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr
dwExtraInfo);
http://msdn.microsoft.com/library/de...alKeyCodes.asp

G> Hi,
G>
G> Is it possible to turn on\off the CAPS LOCK button using C# program?
G> and if so, how can i do that?
G>
G> Thanks,
G> Gidi
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Mar 5 '06 #3

P: n/a
Hello Gidi,

For second options

1) add using System.Runtime.InteropServices;
2) Put button on the form
3) add code below to the class declaraion where is button handler located

public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
...
}

4) add code below to the button handler

private void button1_Click(object sender, EventArgs e)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}

This will turn on/off caps lock each time u press the button

0x14 is a Virtual-key code for capslock. List of codes are here http://msdn.microsoft.com/library/de...alKeyCodes.asp
G> Thanks Michael ,
G>
G> I tried the SendKeys option, and it works only while i'm debugging,
G> but in regular runtime it doesn't turn on\off the capslock key.
G>
G> About the second option, i didn't really understand how should i use
G> it, so if u can, please explain or give example...
G>
G> Thanks,
G> Gidi.
G> "Michael Nemtsev" wrote:
G>
Hello Gidi,

Use SendKeys.Send("{CAPSLOCK}");

or

PInvoke "SetKeyboardState"/"keybd_event"

[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte [] lpKeyState);
[DllImport("user32.dll")]

static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr

dwExtraInfo);

http://msdn.microsoft.com/library/de...ary/en-us/winu
i/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp

G> Hi,
G>
G> Is it possible to turn on\off the CAPS LOCK button using C#
program?
G> and if so, how can i do that?
G>
G> Thanks,
G> Gidi
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents
do not cease to be insipid." (c) Friedrich Nietzsche

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 5 '06 #4

P: n/a
Thanks again Michael,

It works. I have one more question, if it's ok by you...

How can i check if the caps lock is on or off before i calling this event
handler?

Thanks,
Gidi.

"Michael Nemtsev" wrote:
Hello Gidi,

For second options

1) add using System.Runtime.InteropServices;
2) Put button on the form
3) add code below to the class declaraion where is button handler located

public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
...
}

4) add code below to the button handler

private void button1_Click(object sender, EventArgs e)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr) 0);
}

This will turn on/off caps lock each time u press the button

0x14 is a Virtual-key code for capslock. List of codes are here http://msdn.microsoft.com/library/de...alKeyCodes.asp
G> Thanks Michael ,
G>
G> I tried the SendKeys option, and it works only while i'm debugging,
G> but in regular runtime it doesn't turn on\off the capslock key.
G>
G> About the second option, i didn't really understand how should i use
G> it, so if u can, please explain or give example...
G>
G> Thanks,
G> Gidi.
G> "Michael Nemtsev" wrote:
G>
Hello Gidi,

Use SendKeys.Send("{CAPSLOCK}");

or

PInvoke "SetKeyboardState"/"keybd_event"

[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte [] lpKeyState);
[DllImport("user32.dll")]

static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr

dwExtraInfo);

http://msdn.microsoft.com/library/de...ary/en-us/winu
i/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp

G> Hi,
G>
G> Is it possible to turn on\off the CAPS LOCK button using C#
program?
G> and if so, how can i do that?
G>
G> Thanks,
G> Gidi
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents
do not cease to be insipid." (c) Friedrich Nietzsche

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Mar 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.