VBA-Programmierung in Microsoft Excel

Tutorial: Excel-Beispiele

Aus- und Einlesen der NumLock-, CapsLock und ScrollLock-Tasten

Gruppe

API

Bereich

Tastatur

Thema

Aus- und Einlesen der NumLock-, CapsLock und ScrollLock-Tasten

Problem

Wie kann ich den Status der NumLock-, CapsLock und ScrollLock-Tasten über UserForm-CheckBoxes auslesen und setzen?

Lösung

Geben Sie den nachfolgenden Code in ein Standardmodul ein und weisen Sie ihn einer Schaltfläche zu.




ClassModule: frmLock

Private Sub cmdCancel_Click()
   Unload Me
End Sub

Private Sub cmdOK_Click()
   Dim var As Variant
   Dim intNum As Integer, intCap As Integer, intScroll As Integer
   If chkNum.Value = True Then intNum = 1 Else intNum = 0
   If chkCap.Value = True Then intCap = 1 Else intCap = 0
   If chkScroll.Value = True Then intScroll = 1 Else intScroll = 0
   var = SetKeys(intNum, intCap, intScroll)
   Unload Me
End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
   Dim intNum As Integer, intCap As Integer, intScroll As Integer
   intNum = GetKeyState(VK_NUMLOCK)
   intCap = GetKeyState(VK_CAPITAL)
   intScroll = GetKeyState(VK_SCROLL)
   If intNum = 1 Then chkNum.Value = True Else chkNum = False
   If intCap = 1 Then chkCap.Value = True Else chkCap = False
   If intScroll = 1 Then chkScroll.Value = True Else chkScroll = False
End Sub

StandardModule: basMain

Public Const VK_NUMLOCK = &H90
Public Const VK_CAPITAL = &H14
Public Const VK_SCROLL = &H91

Private Type KeyboardBytes
  kbByte(0 To 255) As Byte
End Type

Public kbArray As KeyboardBytes

Public Declare Function GetKeyState Lib "user32" _
    (ByVal nVirtKey As Long) As Integer

Public Declare Function SetKeyboardState Lib "user32" _
    (kbArray As KeyboardBytes) As Long
    
Sub CallSetKeys()
   frmLock.Show
End Sub

Function SetKeys( _
   intNumLockKey As Integer, _
   intCapLockKey As Integer, _
   intScrollLockKey As Integer)
  With kbArray
    .kbByte(VK_NUMLOCK) = intNumLockKey
    .kbByte(VK_CAPITAL) = intCapLockKey
    .kbByte(VK_SCROLL) = intScrollLockKey
  End With
  SetKeyboardState kbArray
End Function

    


Beiträge aus dem Excel-Forum zu den Themen API und Tastatur