|
-
Dec 21st, 2005, 12:30 AM
#1
Thread Starter
Lively Member
Caps Lock On
hi
Form load Caps Lock turn on....
How to do?
plzz help
-
Dec 21st, 2005, 04:27 PM
#2
New Member
Re: Caps Lock On
Found these links about caps lock. C++ example seems to show how to turn it on, but only c# example was more about monitoring the state
C++ Turn Caps Lock On
http://www.greatis.com/delphicb/tips...apslockon.html
Monitor Key States
http://www.codecomments.com/archive2...-5-489956.html
-
Dec 21st, 2005, 09:51 PM
#3
Re: Caps Lock On
I'm not sure how to toggle Caps Lock for the system, but the following code will control it for your app specifically:
VB Code:
Private Const VK_CAPITAL As Integer = 20
Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByRef pbKeyState As Byte) As Integer
Declare Function SetKeyboardState Lib "user32" Alias "SetKeyboardState" (ByVal lppbKeyState As Byte()) As Integer
Private Sub onButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles onButton.Click
Dim keyState(255) As Byte
Me.GetKeyboardState(keyState(0))
keyState(VK_CAPITAL) = 1
Me.SetKeyboardState(keyState)
End Sub
Private Sub offButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles offButton.Click
Dim keyState(255) As Byte
Me.GetKeyboardState(keyState(0))
keyState(VK_CAPITAL) = 0
Me.SetKeyboardState(keyState)
End Sub
That code will not change the Caps Lock light on your keyboard or affect any other apps, but it will turn Caps Lock on and off in your app. I would have thought that simply SendKeys.Send("{CAPSLOCK}") would have done the job of toggling Caps Lock for the system but apparently not.
Last edited by jmcilhinney; Dec 22nd, 2005 at 07:25 PM.
-
Dec 22nd, 2005, 10:15 AM
#4
Re: Caps Lock On
 Originally Posted by jmcilhinney
I'm not sure how to toggle Caps Lock for the system, but the following code will control it for your app specifically:
VB Code:
Private Const VK_CAPITAL As Integer = 20
Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByRef pbKeyState As Byte) As Integer
Declare Function SetKeyboardState Lib o"user32" Alias "SetKeyboardState" (ByVal lppbKeyState As Byte()) As Integer
Private Sub onButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles onButton.Click
Dim keyState(255) As Byte
Me.GetKeyboardState(keyState(0))
keyState(VK_CAPITAL) = 1
Me.SetKeyboardState(keyState)
End Sub
Private Sub offButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles offButton.Click
Dim keyState(255) As Byte
Me.GetKeyboardState(keyState(0))
keyState(VK_CAPITAL) = 0
Me.SetKeyboardState(keyState)
End Sub
That code will not change the Caps Lock light on your keyboard or affect any other apps, but it will turn Caps Lock on and off in your app. I would have thought that simply SendKeys.Send("{CAPSLOCK}") would have done the job of toggling Caps Lock for the system but apparently not.
This would work in C#?
-
Dec 22nd, 2005, 07:27 PM
#5
Re: Caps Lock On
Oops. I forget I'm in the C# forum sometimes. Here's the direct translation, which I've tested and it works:
Code:
private const int VK_CAPITAL = 0x14;
[DllImport("user32.dll")]
private static extern int GetKeyboardState(ref byte pbKeyState);
[DllImport("user32.dll")]
private static extern int SetKeyboardState(byte[] lppbKeyState);
private void onButton_Click(object sender, System.EventArgs e)
{
byte[] keyState = new byte[256];
GetKeyboardState(ref keyState[0]);
keyState[VK_CAPITAL] = 1;
SetKeyboardState(keyState);
}
private void offButton_Click(object sender, System.EventArgs e)
{
byte[] keyState = new byte[256];
GetKeyboardState(ref keyState[0]);
keyState[VK_CAPITAL] = 0;
SetKeyboardState(keyState);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|