AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VB编程

利用VBA的键盘类

51自学网 http://www.wanshiok.com


   当然,一旦你理解了所有的API调用之后,也就完成了创建类模块所必需的大部分工作。如果你想显示图1中列出的属性,仅仅是编写合适的Property Let 和 Property Get procedures过程的问题了。第10页上的清单1包括了所有的属性过程。

   为了使用Keyboard类模块,可以把它作为任何其他的类(如前所述,除了不能创建类的多个实例之外)来对待。例如,为了将^符号闪烁时间设置为100毫秒,并且打开c和n 切换(保留所有设置原来的状态),你可以编写如图4所示的代码:

Dim fCapsLock As Boolean

Dim fNumLock As Boolean

Dim intBlinkTime As Integer

Dim okb As Keyboard

Set okb = New Keyboard

intBlinkTime = okb.CaretBlinkTime

fCapsLock = okb.Capslock

fNumLock = okb.Numlock

okb.CaretBlinkTime = 100

okb.Capslock = True

okb.Numlock = True

Later, to reset the states:

okb.CaretBlinkTime = intBlinkTime

okb.Capslock = fCapsLock

okb.Numlock = fNumLock

图4 使用Keyboard类来设置闪烁速率以及c和n切换

肯定没有人否认使用简单对象的属性比直接调用Windows API要容易得多。一旦你已经完成了类模块(这应该是很容易的:只需从范例文件复制和粘贴代码即可,参见本文的结尾部分可以获得下载详细资料的信息),那么就可以使用任何VBA应用程序了。还有,因为IntelliSense,你甚至不需要记住属性的名字(我承认,当我发现我自己需要恢复到Office 或 VB以前的版本的时候,我通常在当前版本中编写代码,利用IntelliSense,然后将代码复制和粘贴进已经完成的旧版本)

试一试

创建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

本文中所引用的文件可以从位于http://www.informant.com/mod/modnewupl.htm 的Informant Web站点通过下载得到。文件名为MOD9712KG.ZIP。

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出版。

 
 

上一篇:利用VBA自定义Office的快捷方式  下一篇:在VB 中控制 Word