|
-
Aug 29th, 2008, 04:19 AM
#1
Thread Starter
Hyperactive Member
[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.
Last edited by Oliver1; Sep 1st, 2008 at 05:53 AM.
Reason: esolved
-
Aug 29th, 2008, 04:37 AM
#2
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
-
Aug 29th, 2008, 11:27 AM
#3
Thread Starter
Hyperactive Member
Re: [2008] turn on off caps lock
Thanks loads for this, will check it out on Monday, as am off for the weekend now.
-
Aug 29th, 2008, 04:05 PM
#4
Frenzied Member
Re: [2008] turn on off caps lock
Deepak or you able using that code to simulate any key?
-
Aug 30th, 2008, 04:03 AM
#5
Re: [2008] turn on off caps lock
 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.
-
Aug 30th, 2008, 09:45 AM
#6
Frenzied Member
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..
-
Aug 30th, 2008, 11:11 AM
#7
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.
-
Aug 30th, 2008, 11:27 AM
#8
Frenzied Member
Re: [2008] turn on off caps lock
Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission...
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Aug 30th, 2008, 03:11 PM
#9
Frenzied Member
Re: [2008] turn on off caps lock
 Originally Posted by obi1kenobi
Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission... 
Ya i would agree.
-
Aug 31st, 2008, 10:57 PM
#10
Re: [2008] turn on off caps lock
 Originally Posted by obi1kenobi
Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission... 
 Originally Posted by noahssite
Ya i would agree.
Thanks . I will submit it into the Code Bank if there is no matching code/thread.
-
Sep 1st, 2008, 05:44 AM
#11
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
-
Sep 1st, 2008, 05:55 AM
#12
Thread Starter
Hyperactive Member
Re: [2008] turn on off caps lock
Thanks for this, worked a treat.
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
|