Results 1 to 11 of 11

Thread: [RESOLVED] Caps Lock/Num lock on or off?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    55

    Resolved [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

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    55

    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

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    55

    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

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  6. #6

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    55

    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

  8. #8

  9. #9
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Caps Lock/Num lock on or off?


  10. #10

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    55

    Resolved 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
  •  



Click Here to Expand Forum to Full Width