用户登录  |  用户注册
首 页源码下载网络学院最新源码源码排行屏蔽广告
当前位置:新兴网络 > 网络学院 > 编程开发 > Visual Basic

VB 设置CapsLock、ScrollLock、NumLock键状态

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2011-03-13 07:08:00

VB 设置CapsLock、ScrollLock、NumLock 按键状态:

VBScript code复制代码
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

  方法二

直接取得键盘状态修改后再传回就行了程式如下

VBScript code复制代码
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Const VK_NUMLOCK = &H90
Private Const VK_SCROLL = &H91
Private Const VK_CAPITAL = &H14
Public Sub setKeyState(vkkey As Byte, kset As Boolean)
Dim s(255) As Byte
Call GetKeyboardState(s(0))
If kset Then
    s(vkkey) = 1
Else
    s(vkkey) = 0
End If
Call SetKeyboardState(s(0))
End Sub

若要设定CapsLock只要使用
setKeyState VK_CAPITAL, True
解除的话只要使用
setKeyState VK_CAPITAL, False

Tags:VB CapsLock ScrollLock NumLock 状态

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      100%(1)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

网络学院评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论