none
[VB6]如何取得CapsLook鍵是否開啟 (有燈)? RRS feed

  • 問題

  • 如何取得CapsLook鍵是否開啟?

    如果要轉為Boolean值 ,又該怎樣做?

     

    mAPI.bas
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function GetAsyncKeyState Lib "user32" (ByVal VKEY As Long) As Integer
    Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Declare Function GetForegroundWindow Lib "user32.dll" () As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

     

     

     

    mCommon.bas

    Public CapsLockOn As Boolean

     

    Sub ChkCaps()

     

    End Sub

     

    Public Function GetCaption(WindowHandle As Long) As String
        Dim strBuffer As String, lngTextLength As Long
        lngTextLength = GetWindowTextLength(WindowHandle)
        strBuffer = String(lngTextLength, 0)
        Call GetWindowText(WindowHandle, strBuffer, lngTextLength + 1)
        GetCaption$ = strBuffer
    End Function

     


     

    2007年6月12日 上午 04:52

解答

  • HI

     

    1) 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

    2)

        Const VK_CAPITAL = &H14

         Dim keys(0 To 255) As Byte
           GetKeyboardState keys(0)

         CapsLockState = keys(VK_CAPITAL)    '判斷Caps是否有按

    3) '模擬按CAPS鍵

        '===== if Win95
          keys(VK_CAPITAL) = 1
          SetKeyboardState keys(0)
          

        '===== if WinNT
          '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
          

     

    tihs

    2007年6月14日 上午 09:24