I don’t like using Sendkeys. This is sometimes quite tricky. Here you have some code based on API calls that does the job.
Option Explicit
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Dim kbArray As KeyboardBytes
Private Declare Function GetKeyState Lib “user32” (ByVal nVirtKey As Long) As Long
Private Declare Function GetKeyboardState Lib “user32” (kbArray As KeyboardBytes) As Long
Private Declare Function SetKeyboardState Lib “user32” (kbArray As KeyboardBytes) As Long
Const VK_NUMLOCK = &H90
Private Function Get_Value() As Boolean
Get_Value = GetKeyState(VK_NUMLOCK) And 1 = 1
End Function
Private Sub Set_Value(boolVal As Boolean)
Call GetKeyboardState(kbArray)
kbArray.kbByte(VK_NUMLOCK) = Abs(boolVal)
Call SetKeyboardState(kbArray)
End Sub
Sub change_NUMLOCK()
Dim State_Numlock As Boolean
State_Numlock = Get_Value
If State_Numlock = True Then
Call Set_Value(False)
Else
Call Set_Value(True)
End If
End Sub
With the Get_Value function, I read the numlock status. With the Set_Value subroutine I set the numlock status.
In the change_NUMLOCK subroutine, I just toggle the numlock key.