The following brief sections demonstrate how to use each of the Keyboard class properties. In general, to use the Keyboard class, create a new instance of the class, and then set or retrieve its available properties. For example, to retrieve the current keyboard delay setting, you might use code like this:
Dim okb As New Keyboard
If okb.Delay < 3 Then
okb.Delay = okb.Delay + 1
End If
Figure 9.12: Control Panel interface for properties of a Keyboard object
Use the CapsLock property to set or retrieve the state of the CapsLock keyboard toggle. It accepts and returns a Boolean parameter, so you might use code like this to retrieve the current state of the toggle, set the CapsLock toggle on, do some work, and then reset the toggle to its original state:
Dim fOldState As Boolean
Dim okb As New Keyboard
fOldState = okb.CapsLock
okb.CapsLock = True
' Do work with CapsLock on.
okb.Capslock = fOldState
Use the NumLock and ScrollLock properties just as you do the CapsLock property.
Because the key state settings you use in the Keyboard class are pertinent only to the current application, Windows doesn’t treat them as system-wide resources. Therefore, the indicator lights on your keyboard may not be affected by the CapsLock, NumLock, or ScrollLock Keyboard class property. (The behavior depends on your version of Windows—Windows 95 will correctly toggle the CapsLock and NumLock key light, but Windows NT does not. Neither operating system toggles the ScrollLock key light.)
The CaretBlinkTime property allows you to control the number of milliseconds between the “blinks” of the text input caret. To make the caret blink more slowly, you could use code like this:
Dim okb As New Keyboard
' Slow down the caret blink rate one "notch"
If okb.CaretBlinkTime < 1200 Then
okb.CaretBlinkTime = okb.CaretBlinkTime + 100
End If
Use the CaretOn property to show or hide the text-input caret. Most applications control this setting themselves, so you can’t count on a global effect when you change this property, nor would you want to, for the most part. To retrieve the current state of the caret and hide it, you might use code like this:
Dim okb As New Keyboard
' Hide the text caret.
okb.CaretOn = False
Hiding is cumulative. If your application hides the caret five times in a row, it must also unhide the caret five times before the caret reappears. Of course, because most VBA host applications control the caret themselves, you won’t get much use from this property. Test carefully when using CaretOn; it may work for you, but it may not.
Use the Delay property to control the amount of time the keyboard waits, while you’re pressing a particular key, before starting the autorepeat action. (See the section “Speed,” coming up in a moment, for information on controlling the speed at which the key autorepeats.) You can set this property to any value between 0 and 3, inclusive. The following code sets the keyboard delay to its smallest possible value:
Dim okb As New Keyboard
okb.Delay = 0
The FunctionKeys property simply returns the number of function keys on the installed keyboard. It returns one of the (admittedly ambiguous) values in Table 9.10:
Dim okb As New Keyboard
Select Case okb.FunctionKeys
Case 1,3,5
' Only 10 function keys. You need more for your
' application.
MsgBox "You don’t have enough function keys!"
End Select
Table 9.10: Possible Values for the FunctionKeys Property
Return Value | Number of Function Keys |
1 | 10 |
2 | 12 (sometimes 18) |
3 | 10 |
4 | 12 |
5 | 10 |
6 | 24 |
7 | Hardware dependent and specified by the OEM |
The KeyboardType property returns the specific type of keyboard that’s currently installed, as one of the values listed in Table 9.11. If your application requires the newer 101- or 102-key keyboard, you might include code like this:
Dim okb As New Keyboard
If okb.KeyBoardType <> 4 Then
MsgBox "This application works only with the new keyboard."
End If
Table 9.11: Possible Values for the KeyboardType Property
Return Value | Keyboard Type |
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 |
Use the Speed property to set the rate at which characters repeat while you’re holding down the key. (See the section “Delay” above for information on controlling the waiting period before the repeat.) Windows allows values between 0 and 31 for this property:
Dim okb As New Keyboard
' Set the fastest repeat rate.
okb.Speed = 31