anyway to detect whether the NUMLOCK is currently on or off.
I need to off the NUMLOCK before sending DOWN to a dos program to create arrow down signal, otherwise '2' is sent instead.
So, any idea to do that? How? Any example code?Thanks
Printable View
anyway to detect whether the NUMLOCK is currently on or off.
I need to off the NUMLOCK before sending DOWN to a dos program to create arrow down signal, otherwise '2' is sent instead.
So, any idea to do that? How? Any example code?Thanks
perhaps someone else will post the code (i'm too tired)
but the api call you want is GetKeyboardState.
Study up on it and arrays. It needs a 256 byte array to work.
VB Code:
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long Private Const VK_CAPITAL = &H14 Private Const VK_NUMLOCK = &H90 Private Const VK_SCROLL = &H91 ReDim KeyboardBuffer(256) As Byte GetKeyboardState KeyboardBuffer(0) 'This example show you how to press the Caps Lock button. if you want 'to press the Num Lock button, Replace all the following 3 'VK_CAPITAL' 'with 'VK_NUMLOCK' or VK_SCROLL as desired. If KeyboardBuffer(VK_CAPITAL) And 1 Then KeyboardBuffer(VK_CAPITAL) = 0 Else KeyboardBuffer(VK_CAPITAL) = 1 End If SetKeyboardState KeyboardBuffer(0) Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer 'To check if Num Lock is on Replace the 'vbKeyCapital' below with 'vbKeyNumlock' Dim lngWhatState As Long lngWhatState = GetKeyState(vbKeyCapital) If lngWhatState = 1 Then MsgBox "The Caps Lock Is On" Else MsgBox "The Caps Lock Is Off" End If
then, how to off the NUMLOCK if it is on?
my best guess it would be in the code Hack provided. My guess would be u sed VK_CAPITAL to 0 for off and 1 for on then tell it to setkeyboardstate keyboardbuffer(0)Code:If KeyboardBuffer(VK_CAPITAL) And 1 Then
KeyboardBuffer(VK_CAPITAL) = 0
Else
KeyboardBuffer(VK_CAPITAL) = 1
End If
SetKeyboardState KeyboardBuffer(0)
VB Code:
'*****'This decleration block is needed to switch Numlock to on.. yike ' Declare Type for API call: 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 declarations: 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 ' Constant declarations: 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 ' *********End of Numlock Check declarations *********8 Public Sub TurnNumLockON() Dim o As OSVERSIONINFO Dim NumLockState As Boolean o.dwOSVersionInfoSize = Len(o) GetVersionEx o Dim keys(0 To 255) As Byte GetKeyboardState keys(0) ' Switch Numlock on if needed NumLockState = keys(VK_NUMLOCK) If NumLockState <> True Then 'Turn numlock on If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98 keys(VK_NUMLOCK) = 1 SetKeyboardState keys(0) ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT '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
The code I just posted actually switches numlock on if its off, but you can just change the code in the turnnumlockon sub. I ripped it outta one of my programs... god knows who actually wrote this code..