vb里怎样监测键盘输入的是什么键?举例说明一下

king8192 2006-01-07 01:47:37
vb里怎样监测键盘输入的是什么键?举例说明一下。谢谢!!
...全文
242 点赞 收藏 12
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
szh3210 2006-01-08
学习
回复
bbhere 2006-01-07
只知道
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
End Sub
回复
qjqmoney 2006-01-07
VB里没有全局钩子,所以无法获得所有程序的键盘事件。
回复
IamDeane 2006-01-07
http://post.baidu.com/f?kz=70091170
有个例子,楼主可以看看
回复
hhjjhjhj 2006-01-07
key spy 代码
'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
回复
ljk02079 2006-01-07
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

End Sub
KeyCode 按键代码
Shift 检测Shift有没有按下
回复
vansoft 2006-01-07
可以用HOOK,来HOOK整个键盘。
回复
Gutta 2006-01-07
KeyPress事件中有个keyAscii参数,这就是所按键的ASCII码

可以根据这个来判断
回复
rainstormmaster 2006-01-07
这个钩子的具体用法可以参考:
http://www.vb-helper.com/howto_disable_keys.html
回复
rainstormmaster 2006-01-07
//VB里没有全局钩子,所以无法获得所有程序的键盘事件。

用WH_KEYBOARD_LL 钩子
回复
jlum99 2006-01-07
一个是用GetAsyncKeyState ,上面有代码,一个就是用日志HOOK,都是全局的。
回复
king8192 2006-01-07
谢谢各位帮忙!!
回复
发帖
VB基础类
创建于2007-09-28

7476

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2006-01-07 01:47
社区公告
暂无公告