How to Toggle Num Lock (in two ways)
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 'System & API - How to Toggle Num Lock (in two ways) 'Method 1: Option Explicit Private Const VER_PLATFORM_WIN32_NT = 2 Private Const VER_PLATFORM_WIN32_WINDOWS = 1 Private Const VK_NUMLOCK = &H90 Private Const KEYEVENTF_EXTENDEDKEY = &H1 Private Const KEYEVENTF_KEYUP = &H2 Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type 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 Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer Public Function NumLockOn() As Boolean 'This function will tell us whether the capslock is on or off Dim iKeyState As Integer iKeyState = GetKeyState(vbKeyNumlock) NumLockOn = (iKeyState = 1 Or iKeyState = -127) End Function Public Sub ToggleNumlock(ByVal bTurnOn As Boolean) 'To turn Numlock on, set bTurnOn to true 'To turn Numlock off, set bTurnOn to false Dim bytKeys(255) As Byte Dim bCapsLockOn As Boolean 'Get status of the 256 virtual keys GetKeyboardState bytKeys(0) bCapsLockOn = bytKeys(VK_NUMLOCK) Dim typOS As OSVERSIONINFO If bCapsLockOn <> bTurnOn Then If typOS.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then 'Win9x bytKeys(VK_NUMLOCK) = 1 SetKeyboardState bytKeys(0) Else 'WinNT/2000 'Simulate Key Press keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0 'Simulate Key Release keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0 End If End If End Sub Private Sub Main() If Not NumLockOn Then If MsgBox("NumLock is OFF. Would you like to turn it on?", vbYesNo + vbQuestion, "NumLock") = vbYes Then Call ToggleNumlock(True) End If Else If MsgBox("NumLock is ON. Would you like to turn it off?", vbYesNo + vbQuestion, "NumLock") = vbYes Then Call ToggleNumlock(False) End If End If End Sub 'Method 2: 'This method will change the state of Num lock internally but will not change status on keyboard Declare Sub GetKeyboardState Lib "user32" (lpKeyState As Any) Declare Sub SetKeyboardState Lib "user32" (lpKeyState As Any) Private Const VK_NUMLOCK = &H90 Private Sub Main() ReDim KeyboardBuffer(256) As Byte GetKeyboardState KeyboardBuffer(0) If KeyboardBuffer(VK_NUMLOCK) And 1 Then KeyboardBuffer(VK_NUMLOCK) = 0 Else KeyboardBuffer(VK_NUMLOCK) = 1 End If SetKeyboardState KeyboardBuffer(0) End Sub |
Related posts:
- How to Toggle CapsLock (in two ways)
- How to get the NumLock state whether it is on or off
- How do we close a program forcefully without going through then Unload Procedure? (in two ways)
- How to get the CapsLock state whether it is on or off
- How to Minimize all windows (in four ways)
- How to Dis-connect Internet programmatically (in two ways)
- How to Disable/Toggle screensaver in Windows XP/2003
- How to get screen resolution of the system (in two ways)
- How to make a Toggle Button
- How to check whether the system is connected with internet (in two ways)