Modificar el estado de NumLock, CapsLock y ScrollLock


Agradecemos a El Judas por este truco...

Pasos a seguir :
- Usaremos la función GetKeyboardState para determinar el estado de las teclas.
- Determinaremos el Sistema Operativo mediante la función GetVersionEx del API.
- En Windows 95, usaremos la función SetKeyboardState para modificar el estado de la tecla. En Windows NT, usaremos la función keybd_event para simular la pulsación de la tecla.

El ejemplo que se muestra a continuación pone las tres teclas en on, pero se puede modificar muy fácilmente para ponerlas off, o simplemente chequear el estado de las mismas.

Declaramos en un formulario :

Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type
' API
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
' Constantes
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS = 1

Y para modificar el estado de las teclas :

Dim o As OSVERSIONINFO
Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean

o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
'Manejo del NumLock (BloqNum)
NumLockState = keys(VK_NUMLOCK)
If NumLockState <> True Then
    'Poner numlock a on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
        '===== Win95
        keys(VK_NUMLOCK) = 1
        SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then
        '===== WinNT
        'Simular que pulsamos la tecla
        keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
        'Simular que soltamos la tecla
        keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
    End If
End If
' Manejo del CapsLock (BloqMayús)
CapsLockState = keys(VK_CAPITAL)
If CapsLockState <> True Then
    'Ponemos capslock a on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
        '===== Win95
        keys(VK_CAPITAL) = 1
        SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then
        '===== WinNT
        'Simular que pulsamos la tecla
        keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
        'Simular que soltamos la tecla
        keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
    End If
End If
' Manejo del ScrollLock (BloqDespl)
ScrollLockState = keys(VK_SCROLL)
If ScrollLockState <> True Then
    'Ponemos Scroll lock a on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
        '===== Win95
        keys(VK_SCROLL) = 1
        SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then
        '===== WinNT
        'Simular que pulsamos la tecla
        keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
        'Simular que soltamos la tecla
        keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
    End If
End If