keybd_event VB声明 
Declare Sub keybd_event Lib "user32" Alias "keybd_event" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) 
说明 
这个函数模拟了键盘行动 
参数表 
参数 类型及说明 
bVk Byte,欲模拟的虚拟键码 
bScan Byte,键的OEM扫描码 
dwFlags Long,零;或设为下述两个标志之一 
KEYEVENTF_EXTENDEDKEY 指出是一个扩展键,而且在前面冠以0xE0代码 
KEYEVENTF_KEYUP 模拟松开一个键 
dwExtraInfo Long,通常不用的一个值。api函数GetMessageExtraInfo可取得这个值。允许使用的值取决于特定的驱动程序 
注解 
这个函数支持屏幕捕获(截图)。在win95和nt4.0下这个函数的行为不同 

解决方案 »

  1.   

    Private Const VK_CAPITAL = &H14
    Private Const VK_NUMLOCK = &H90
    Private Const VK_SCROLL = &H91
      

  2.   

    请问怎样调用,我现在想让Caps Lock键开启?
      

  3.   

    引自MSDN:Option ExplicitPrivate Type OSVERSIONINFO
            dwOSVersionInfoSize As Long
            dwMajorVersion As Long
            dwMinorVersion As Long
            dwBuildNumber As Long
            dwPlatformId As Long
            szCSDVersion As String * 128      '  Maintenance string for PSS usage
    End TypePrivate Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    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 GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long' Constant declarations:
    Private Const VK_NUMLOCK = &H90
    Private Const VK_SCROLL = &H91
    Private Const VK_CAPITAL = &H14
    Private Const KEYEVENTF_EXTENDEDKEY = &H1
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const VER_PLATFORM_WIN32_NT = 2
    Private Const VER_PLATFORM_WIN32_WINDOWS = 1Private Sub Command1_Click()
      Dim o As OSVERSIONINFO
      Dim NumLockState As Boolean
      Dim ScrollLockState As Boolean
      Dim CapsLockState As Boolean  o.dwOSVersionInfoSize = Len(o)
      GetVersionEx o
      Dim keys(0 To 255) As Byte
      GetKeyboardState keys(0)  ' Win95、Win98
      If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
        If keys(VK_CAPITAL) = 0 Then
          keys(VK_CAPITAL) = 1
        Else
          keys(VK_CAPITAL) = 0
        End If
        SetKeyboardState keys(0)
      End If
      
      ' WinNT、Win2000
      If o.dwPlatformId = VER_PLATFORM_WIN32_NT Then
        'Simulate Key Press
         keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
         'Simulate Key Release
         keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
      End If
    End Sub