|
-
Jun 24th, 2007, 06:28 PM
#1
Thread Starter
Member
[RESOLVED] Caps Lock/Num lock on or off?
Hi,
Is there any simple way to check if Num lock/Caps lock is on or off?
//¤dark
Dinmamma.Fat = True
-
Jun 24th, 2007, 06:34 PM
#2
Re: Caps Lock/Num lock on or off?
Have a look at this sample.
-
Jun 24th, 2007, 06:44 PM
#3
Thread Starter
Member
Re: Caps Lock/Num lock on or off?
Code:
Private Sub NTToggleKeyCaps()
Call keybd_event(vbKeyCapital, _
MapVirtualKey(vbKeyCapital, 0), _
KEYEVENTF_EXTENDEDKEY Or 0, 0)
Call keybd_event(vbKeyCapital, MapVirtualKey(vbKeyCapital, 0), _
KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
End Sub
Well, I don't get really much, but it seems to me that this only toggles the caps lock? ><
I want to check if caps lock is on, and if it is, i want to turn it off...
//¤dark
Dinmamma.Fat = True
-
Jun 24th, 2007, 06:50 PM
#4
Thread Starter
Member
Re: Caps Lock/Num lock on or off?
And, by the way, if I only wished to toggle Caps Lock, wouldn't it just be easier to Sendkeys("{CAPSLOCK}")? ...
//¤dark
Dinmamma.Fat = True
-
Jun 24th, 2007, 06:55 PM
#5
Re: Caps Lock/Num lock on or off?
No, SendKeys is not reliable at all...
To check the state of say Caps key try this (btw - try to use the search engine - it's quite usefull):
Code:
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Command1_Click()
Dim result As Long
result = GetAsyncKeyState(vbKeyCapital)
If result <> 0 Then
'go ahead and turn it off...
Else
'go ahead and turn it on
End If
End Sub
-
Jun 24th, 2007, 06:55 PM
#6
Re: Caps Lock/Num lock on or off?
Why not always turn it off even if it's already off?
-
Jun 24th, 2007, 07:11 PM
#7
Thread Starter
Member
Re: Caps Lock/Num lock on or off?
Rhinobull, the result is always 0 no matter if caps lock is on or off... Exept for when I press the Caps Lock-button, the result is then -32676, but that value doesn't change depending on if caps lock is on or off when i press the caps lock-button.
Why not always turn it off even if it's already off?
Yes MartinLiss, I would like to do that, but my problem is that I don't know how to just TURN THE GODDAM CAPS LOCK OFF/ON, I only know how to TOGGLE the caps lock...
Last edited by henzino2; Jun 24th, 2007 at 07:18 PM.
//¤dark
Dinmamma.Fat = True
-
Jun 24th, 2007, 07:17 PM
#8
Re: Caps Lock/Num lock on or off?
-
Jun 24th, 2007, 07:18 PM
#9
Re: Caps Lock/Num lock on or off?
-
Jun 24th, 2007, 07:20 PM
#10
Re: Caps Lock/Num lock on or off?
 Originally Posted by henzino2
Rhinobull, the result is always 0 no matter if caps lock is on or off...
What OS are you using? It works just fine under XP and I don't use Vista.
-
Jun 24th, 2007, 08:14 PM
#11
Thread Starter
Member
Re: Caps Lock/Num lock on or off?
Thank you all guys! I combined all your help into one final solution (I've given reputation to all of you too ^^)
(Just posting it if someone's intersted)
In the module:
Code:
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
Declare Function MapVirtualKey Lib "user32" _
Alias "MapVirtualKeyA" _
(ByVal wCode As Long, ByVal wMapType As Long) As Long
Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Public Const KEYEVENTF_KEYUP = &H2
Public Const KEYEVENTF_EXTENDEDKEY = &H1
Public Const VK_NUMLOCK = &H90
Public Const VK_CAPITAL = &H14
Function CapsLock() As Boolean
CapsLock = (GetKeyState(VK_CAPITAL) And 1 = 1)
End Function
Function NumLock() As Boolean
NumLock = (GetKeyState(VK_NUMLOCK) And 1 = 1)
End Function
In the form:
Code:
If NumLock() = False Then
Call keybd_event(vbKeyNumlock, _
MapVirtualKey(vbKeyNumlock, 0), _
KEYEVENTF_EXTENDEDKEY Or 0, 0)
Call keybd_event(vbKeyNumlock, MapVirtualKey(vbKeyNumlock, 0), _
KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
End If
If CapsLock() = True Then
Call keybd_event(vbKeyCapital, _
MapVirtualKey(vbKeyCapital, 0), _
KEYEVENTF_EXTENDEDKEY Or 0, 0)
Call keybd_event(vbKeyCapital, MapVirtualKey(vbKeyCapital, 0), _
KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
End If
Last edited by henzino2; Jun 24th, 2007 at 08:18 PM.
//¤dark
Dinmamma.Fat = True
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
|