VBA, MS Access MS Access в примерах

Проверка состояния и вклюяение - выключение NumLock (API)

По материалам: https://stackoverflow.com/questions/38018232/send-keys-is-disabling-numlock

'----------------------------------------------------------------------------------------------
'https://stackoverflow.com/questions/38018232/send-keys-is-disabling-numlock
'modNumLOCK_Operate_API - Проверка состояния и вклюяение - выключение NumLock (API)
'----------------------------------------------------------------------------------------------
' API declarations
#If VBA7 And Win64 Then
    Private Declare PtrSafe Sub keybd_event Lib "USER32" ( _
                                ByVal bVk As Byte, _
                                ByVal bScan As Byte, _
                                ByVal dwflags As Long, _
                                ByVal dwExtraInfo As Long)
    Private Declare PtrSafe Function GetKeyState Lib "USER32" (ByVal nVKey As Long) As Integer
#Else
    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 GetKeyState Lib "USER32" (ByVal nVKey As Long) As Integer
#End If
'----------------------------------------------------------------------------------------------
'Constant declarations
Private Const VK_NUMLOCK = &H90
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
'----------------------------------------------------------------------------------------------
Public Property Get Numlock() As Boolean
'Returns the current Numlock state
    Numlock = Numlock_State
End Property

Public Property Let Numlock(State As Boolean)
'Sets the Numlock state
'   true = turn numlock on
'   false = turn numlock off
'----------------------------------------------------------------------------------------------
    If State <> Numlock_State Then Numlock_Toggle
End Property

'----------------------------------------------------------------------------------------------
'METHODS
'----------------------------------------------------------------------------------------------
Private Function Numlock_State() As Boolean
'Returns the current Numlock state
    DoEvents    'Required for key messages to be processed
    Numlock_State = CBool(GetKeyState(VK_NUMLOCK))
End Function

Private Sub Numlock_Set(State As Boolean)
'Sets the Numlock state
'   State:  true = turn numlock on
'           false = turn numlock off
'----------------------------------------------------------------------------------------------
    If State <> Numlock_State Then Numlock_Toggle
End Sub

Public Sub Numlock_Toggle()
'Toggles the Numlock state
'----------------------------------------------------------------------------------------------
    Dim previous_state As Boolean
    previous_state = Numlock_State
    
    keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY, 0  'Simulate Numlock key Press
    keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0        'Simulate Numlock key Release
    
End Sub
'----------------------------------------------------------------------------------------------
'Usage examples:
'----------------------------------------------------------------------------------------------

Public Sub Example()
    'Turn Numlock on:
    Numlock = True

    'Turn Numlock off:
    Numlock = False
    
    'Check Numlock state:
    Dim IsOn As Boolean
    IsOn = Numlock
    
    'Toggle Numlock state:
    Numlock_Toggle
End Sub


Назад ToTop
L.E. 25.03.2022
Рейтинг@Mail.ru