[2008] turn on off caps lock
Banging my head all morning about this, as seems to be something you cannot do without calling windows api functions.
Have scraped over the internet and come up with this
h Code:
Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
SetKeyState(System.Windows.Forms.Keys.CapsLock, True)
End Sub
Private Declare Auto Function SetKeyboardState Lib "user32.dll" _
(ByVal lppbKeyState As Byte) As Long
Private Declare Auto Function GetKeyState Lib "user32.dll" _
(ByVal nVirtKey As Long) As Boolean
Private Declare Auto Function GetKeyboardState Lib "user32.dll" _
(ByVal pbKeyState As Byte) As Long
Private Sub SetKeyState(ByVal Key As Long, ByVal State As Boolean)
Dim Keys(0 To 255) As Byte
Call GetKeyboardState(Keys(0))
Keys(Key) = Math.Abs(CInt(State))
Call SetKeyboardState(Keys(0))
End Sub
But it doesn't work and am well out of my depth.
I just want to check if caps lock is pressed and if so turn it off.
Any help would be greatly appreciated.
Re: [2008] turn on off caps lock
vb.net Code:
Imports System.Runtime.InteropServices
Public Class Form2
Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Integer, _
ByVal dwExtraInfo As Integer _
)
Private Const VK_CAPITAL As Integer = &H14
Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
Private Const KEYEVENTF_KEYUP As Integer = &H2
Private Structure OSVERSIONINFO
Dim dwOSVersionInfoSize As Integer
Dim dwMajorVersion As Integer
Dim dwMinorVersion As Integer
Dim dwBuildNumber As Integer
Dim dwPlatformId As Integer
<VBFixedString(128), MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> Public szCSDVersion() As Char
End Structure
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
' Toggle CapsLock
keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0) ' Simulate Key Press
keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) ' Simulate Key Release
End Sub
End Class
Re: [2008] turn on off caps lock
Thanks loads for this, will check it out on Monday, as am off for the weekend now.
Re: [2008] turn on off caps lock
Deepak or you able using that code to simulate any key?
Re: [2008] turn on off caps lock
Quote:
Originally Posted by noahssite
Deepak or you able using that code to simulate any key?
It would be better if you tell us your exact requirement.
Re: [2008] turn on off caps lock
Though don't i just replace the &H45? I have found a submission in the codebank by .paul. which retrieves that value of any key you press. As to get &H..
Re: [2008] turn on off caps lock
The &H denotes that the number is in hex. You can go with the dec version of hex 14 which is 20.
Private Const VK_CAPITAL As Integer = &H14 is the same as:
Private Const VK_CAPITAL As Integer = 20
Take a look at winuser.h - it is a kind of an include in the C++ environment when developing windows programs. It is very possible that you also have it buried somewhere in your computer too.
The file lists many numeric constants that Windows uses for it's api functions. Whether you are using C++ , VB or whatever, the OS needs those numbers and not their names. In winuser.h scroll down to the '* Virtual Keys, Standard Set' and you'll find
#define VK_CAPITAL 0x14. As i said 0x denotes hex in C++ just like &H denotes hex in VB. If you need to press the Pause key you take 0x13 or &H13 respectively...or just 19 in dec.
Re: [2008] turn on off caps lock
Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission... :)
Re: [2008] turn on off caps lock
Quote:
Originally Posted by obi1kenobi
Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission... :)
Ya i would agree.
Re: [2008] turn on off caps lock
Quote:
Originally Posted by obi1kenobi
Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission... :)
Quote:
Originally Posted by noahssite
Ya i would agree.
Thanks :). I will submit it into the Code Bank if there is no matching code/thread.
Re: [2008] turn on off caps lock
You should include the whole code. As it is, the OSVERSIONINFO structure is not needed. It is used to check if the current OS is not from the WinNT family. If that is the case, the keybd_event api won't work. One would have to use the SetKeyboardState api.
Here is the complete snippet: pinvoke.net
Re: [2008] turn on off caps lock
Thanks for this, worked a treat.