Function CapsLock() As Boolean CapsLock = (GetKeyState(VK_CAPITAL) And 1 = 1) End FunctionPrivate Sub Form_Load() If CapsLock() = 1 Then Label1 = "On" Else Label1 = "Off" End SubPrivate Sub cmdToggle_Click() GetKeyboardState kbArray kbArray.kbByte(VK_CAPITAL) = _ IIf(kbArray.kbByte(VK_CAPITAL) = 1, 0, 1) SetKeyboardState kbArray If CapsLock() Then Label1.Caption = "On" Else Label1.Caption = "Off" End If End SubPrivate Sub cmdTurnOn_Click() GetKeyboardState kbArray kbArray.kbByte(VK_CAPITAL) = 1 SetKeyboardState kbArray If CapsLock() Then Label1.Caption = "On" Else Label1.Caption = "Off" End If End SubPrivate Sub cmdTurnOff_Click() GetKeyboardState kbArray kbArray.kbByte(VK_CAPITAL) = 0 SetKeyboardState kbArray If CapsLock() Then Label1.Caption = "On" Else Label1.Caption = "Off" End If End Sub
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer Private Sub Command1_Click() MsgBox CBool(GetKeyState(vbKeyCapital) And 1) End Sub
给你个例子看看'In a module Public Const DT_CENTER = &H1 Public Const DT_WORDBREAK = &H10 Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, ByVal lpDrawTextParams As Any) As Long Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Global Cnt As Long, sSave As String, sOld As String, Ret As String Dim Tel As Long Function GetPressedKey() As String For Cnt = 32 To 128 'Get the keystate of a specified key If GetAsyncKeyState(Cnt) <> 0 Then GetPressedKey = Chr$(Cnt) Exit For End If Next Cnt End Function Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) Ret = GetPressedKey If Ret <> sOld Then sOld = Ret sSave = sSave + sOld End If End Sub 'In a form Private Sub Form_Load() Me.Caption = "Key Spy" 'Create an API-timer SetTimer Me.hwnd, 0, 1, AddressOf TimerProc End Sub Private Sub Form_Paint() Dim R As RECT Const mStr = "Start this project, go to another application, type something, switch back to this application and unload the form. If you unload the form, a messagebox with all the typed keys will be shown." 'Clear the form Me.Cls 'API uses pixels Me.ScaleMode = vbPixels 'Set the rectangle's values SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight 'Draw the text on the form DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0& End Sub Private Sub Form_Resize() Form_Paint End Sub Private Sub Form_Unload(Cancel As Integer) 'Kill our API-timer KillTimer Me.hwnd, 0 'Show all the typed keys MsgBox sSave End Sub下面是getkeystate函数的帮助说明GetKeyState VB声明 Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal nVirtKey As Long) As Integer 说明 针对已处理过的按键,在最近一次输入信息时,判断指定虚拟键的状态 返回值 Integer,如开关键打开,则位0设为1(开关键包括CapsLock, NumLock,ScrollLock);如某个键当时正处于按下状态,则位15为1;如已经抬起,则为0 参数表 参数 类型及说明 nVirtKey Long,欲测试的虚拟键键码。对字母、数字字符(A-Z、a-z、0-9),用它们实际的ASCII值
获得CapsLock状态Private Declare Function GetKeyboardState Lib "user32" (ByVal pbKeyState As String) As BooleanPrivate Sub Command1_Click() Dim KeyMap As String * 256 Dim keystate As String
KeyMap = String(255, 0) If GetKeyboardState(KeyMap) Then keystate = Mid(KeyMap, &H15, 1) MsgBox iif(Asc(keystate)=1,"按下了CapsLock","没有按下CapsLock") End If End Sub
CapsLock = (GetKeyState(VK_CAPITAL) And 1 = 1)
End FunctionPrivate Sub Form_Load()
If CapsLock() = 1 Then Label1 = "On" Else Label1 = "Off"
End SubPrivate Sub cmdToggle_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = _
IIf(kbArray.kbByte(VK_CAPITAL) = 1, 0, 1)
SetKeyboardState kbArray If CapsLock() Then
Label1.Caption = "On"
Else
Label1.Caption = "Off"
End If
End SubPrivate Sub cmdTurnOn_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 1
SetKeyboardState kbArray If CapsLock() Then
Label1.Caption = "On"
Else
Label1.Caption = "Off"
End If
End SubPrivate Sub cmdTurnOff_Click()
GetKeyboardState kbArray
kbArray.kbByte(VK_CAPITAL) = 0
SetKeyboardState kbArray If CapsLock() Then
Label1.Caption = "On"
Else
Label1.Caption = "Off"
End If
End Sub
Private Sub Command1_Click()
MsgBox CBool(GetKeyState(vbKeyCapital) And 1)
End Sub
Public Const DT_CENTER = &H1
Public Const DT_WORDBREAK = &H10
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, ByVal lpDrawTextParams As Any) As Long
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Global Cnt As Long, sSave As String, sOld As String, Ret As String
Dim Tel As Long
Function GetPressedKey() As String
For Cnt = 32 To 128
'Get the keystate of a specified key
If GetAsyncKeyState(Cnt) <> 0 Then
GetPressedKey = Chr$(Cnt)
Exit For
End If
Next Cnt
End Function
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Ret = GetPressedKey
If Ret <> sOld Then
sOld = Ret
sSave = sSave + sOld
End If
End Sub
'In a form
Private Sub Form_Load()
Me.Caption = "Key Spy"
'Create an API-timer
SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
End Sub
Private Sub Form_Paint()
Dim R As RECT
Const mStr = "Start this project, go to another application, type something, switch back to this application and unload the form. If you unload the form, a messagebox with all the typed keys will be shown."
'Clear the form
Me.Cls
'API uses pixels
Me.ScaleMode = vbPixels
'Set the rectangle's values
SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight
'Draw the text on the form
DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0&
End Sub
Private Sub Form_Resize()
Form_Paint
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Kill our API-timer
KillTimer Me.hwnd, 0
'Show all the typed keys
MsgBox sSave
End Sub下面是getkeystate函数的帮助说明GetKeyState VB声明
Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal nVirtKey As Long) As Integer
说明
针对已处理过的按键,在最近一次输入信息时,判断指定虚拟键的状态
返回值
Integer,如开关键打开,则位0设为1(开关键包括CapsLock, NumLock,ScrollLock);如某个键当时正处于按下状态,则位15为1;如已经抬起,则为0
参数表
参数 类型及说明
nVirtKey Long,欲测试的虚拟键键码。对字母、数字字符(A-Z、a-z、0-9),用它们实际的ASCII值
Dim KeyMap As String * 256
Dim keystate As String
KeyMap = String(255, 0)
If GetKeyboardState(KeyMap) Then
keystate = Mid(KeyMap, &H15, 1)
MsgBox iif(Asc(keystate)=1,"按下了CapsLock","没有按下CapsLock")
End If
End Sub