好库网 好库网首页 | 我的好库
VB爱好者

设定CapsLock ScrollLock NumLock-VB资料

发布者:vbfans
发布日期:2011/4/10 15:00:46   更新日期:2011/4/10 15:00:46
阅读次数:1524
评分:4.80
介绍:设定CapsLock ScrollLock NumLock-VB资料
正文:

原本想说使用SendKeys来模拟按这三个Key,但是不会动,只好,再使用API来完成
这些程式都用到两次keybd_event,第一次模拟键按下,第二次模拟放开键,而我查
MSDN的文件,说SetNumLock只在NT上有效,95上没有作用,还真的呢!不过有时会成
功个一次,实在不了解为何如此。

Option Explicit
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2

Public Sub SetNumLock(ByVal bLock As Boolean)
Dim i As Boolean, ScanCode As Long
i = CBool(GetKeyState(vbKeyNumlock) And vbShiftMask)
If i <> bLock Then
ScanCode = MapVirtualKey(vbKeyNumlock, 0)
Call keybd_event(vbKeyNumlock, ScanCode, 0, 0)
Call keybd_event(vbKeyNumlock, ScanCode, KEYEVENTF_KEYUP, 0)
End If
End Sub
Public Sub SetScrollLock(ByVal bLock As Boolean)
Dim i As Boolean, ScanCode As Long
i = CBool(GetKeyState(vbKeyScrollLock) And vbShiftMask)
If i <> bLock Then
ScanCode = MapVirtualKey(vbKeyScrollLock, 0)
Call keybd_event(vbKeyScrollLock, ScanCode, 0, 0)
Call keybd_event(vbKeyScrollLock, ScanCode, KEYEVENTF_KEYUP, 0)
End If
End Sub
Public Sub SetCapsLock(ByVal bLock As Boolean)
Dim i As Boolean, ScanCode As Long
i = CBool(GetKeyState(vbKeyCapital) And vbShiftMask)
If i <> bLock Then
ScanCode = MapVirtualKey(vbKeyCapital, 0)
Call keybd_event(vbKeyCapital, ScanCode, 0, 0)
Call keybd_event(vbKeyCapital, ScanCode, KEYEVENTF_KEYUP, 0)
End If
End Sub

Private Sub Command1_Click()
Call SetScrollLock(True)
End Sub





评论 [发表评论]
账号 密码 还没帐号呢,现在注册一个?

免责声明:好库网所展示的信息由买卖双方自行提供,其真实性、准确性和合法性由信息发布人负责。好库网不提供任何保证,并不承担任何法律责任。