创建Keyboard类,将它添加到项目,然后使用它。你将看到没有比这更容易的了。你不需要知道或记住这些讨厌的API调用是如何工作的。随着过程的继续,我敢保证我们会介绍更多的API包装类。Mike Gilbert和我已经为各种项目编写了大量的类。你不应该再感到烦恼了,下面是可以方便调用的类模块:
Begin Listing One
Property Get KeyboardType() As Long
' Determine the type of keyboard on the system.
' 1 IBM PC/XT or compatible (83-key) keyboard
' 2 Olivetti "ICO" (102-key) keyboard
' 3 IBM PC/AT (84-key) or similar keyboard
' 4 IBM enhanced (101- or 102-key) keyboard
' 5 Nokia 1050 and similar keyboards
' 6 Nokia 9140 and similar keyboards
' 7 Japanese keyboard
KeyboardType = GetKeyboardType(0)
End Property
Property Get FunctionKeys() As Long
' Determine the number of function keys on the keyboard.
' 1 10
' 2 12 (sometimes 18)
' 3 10
' 4 12
' 5 10
' 6 24
' 7 Hardware dependent and specified by the OEM
FunctionKeys = GetKeyboardType(2)
End Property
Property Get Capslock() As Boolean
Return the Capslock toggle.
Capslock = CBool(GetKeyState(vbKeyCapital) And 1)
End Property
Property Get Numlock() As Boolean
' Return the Numlock toggle.
Numlock = CBool(GetKeyState(vbKeyNumlock) And 1)
End Property
Property Get ScrollLock() As Boolean
' Return the ScrollLock toggle.
' ScrollLock = CBool(GetKeyState(vbKeyScrollLock) And 1)
End Property
Property Let Capslock(Value As Boolean)
' Set the Capslock toggle.
Call SetKeyState(vbKeyCapital, Value)
End Property
Property Let Numlock(Value As Boolean)
' Set the Numlock toggle.
Call SetKeyState(vbKeyNumlock, Value)
End Property
Property Let ScrollLock(Value As Boolean)
' Set the ScrollLock toggle.
Call SetKeyState(vbKeyScrollLock, Value)
End Property
Private Sub SetKeyState(intKey As Integer, _
fTurnOn As Boolean)
' Retrieve the keyboard state, set the particular
' key in which you're interested, and then set
' the entire keyboard state back the way it
' was, with the one key altered.
Dim abytBuffer(0 To 255) As Byte
Call GetKeyboardState(abytBuffer(0))
abytBuffer(intKey) = CByte(Abs(fTurnOn))
Call SetKeyboardState(abytBuffer(0))
End Sub
Property Let Delay(Value As Long)
' Sets the keyboard repeat-delay setting.
' Only values 0 through 3 are acceptable. Others will be
' set back to 0.
Call SystemParametersInfo(SPI_SETKEYBOARDDELAY, Value, _
0, SPIF_TELLALL)
End Property
Property Get Delay() As Long
Dim lngValue As Long
Call SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, _
lngValue, 0)
Delay = lngValue
End Property
Property Let Speed(Value As Long)
' Sets the keyboard repeat-speed setting.
' Only values 0 through 31 are acceptable. Others will
' be set back to 0.
Call SystemParametersInfo(SPI_SETKEYBOARDSPEED, Value, _
0, SPIF_TELLALL)
End Property
Property Get Speed() As Long
' Get the keyboard repeat-speed setting.
Dim lngValue As Long
Call SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, _
lngValue, 0)
Speed = lngValue
End Property
Property Get CaretBlinkTime() As Long
' Retrieve the number of milliseconds
' between blinks of the caret.
' SYSTEM RESOURCE. Change this with care.
CaretBlinkTime = GetCaretBlinkTime()
End Property
Property Let CaretBlinkTime(Value As Long)
' Set the number of milliseconds
' between blinks of the caret.
' SYSTEM RESOURCE. Change this with care.
' Allowable values: 200 to 1200 (multiples of 100)
Call SetCaretBlinkTime(Value)
End Property
End Listing One
Ken Getz 和 Mike Gilbert是MCW Technologies的高级顾问,MCW Technologies是Microsoft Solution Provider,其重点是Visual Basic 和 Office 和 BackOffice 组件。他们最近完成了VBA Developer’s Handbook 和 Access 97 Developer’s Handbook (与 Paul Litwin合作),两本书均由SYBEX出版。