返回首页
当前位置: 主页 > Excel教程 > Excel VBA教程 >

如何用VBA检查Caps Lock键是否打开

时间:2012-08-19 00:07来源:Office教程学习网 www.office68.com编辑:麦田守望者

要用VBA代码判断Caps Lock键、Num Lock键及Scroll Lock键的开启状态,可以用下面的代码,前四行代码为API声明,需放置在代码窗口的最上方:

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Private Sub KeyStates()
If GetKeyState(VK_CAPITAL) Then 'Caps Lock键
MsgBox "Caps Lock ON"
Else
MsgBox "Caps Lock OFF"
End If

If GetKeyState(VK_NUMLOCK) Then 'Num Lock键
MsgBox "Num Lock ON"
Else
MsgBox "Num Lock OFF"
End If
If GetKeyState(VK_SCROLL) Then 'Scroll Lock键
MsgBox "Scroll Lock ON"
Else
MsgBox "Scroll Lock OFF"
End If
End Sub

另外,在Word中可以使用下面的VBA代码来判断Caps Lock键是否开启:

Sub KeyStates()
If Selection.Information(wdCapsLock) Then
MsgBox "Caps Lock 键已开启"
Else
MsgBox "Caps Lock 键已关闭"
End If
End Sub

------分隔线----------------------------
标签(Tag):excel excel2007 excel2010 excel2003 excel技巧 excel教程 excel实例教程 excel2010技巧
------分隔线----------------------------
推荐内容
猜你感兴趣