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

king8192 2006-01-07 01:47:37
vb里怎样监测键盘输入的是什么键?举例说明一下。谢谢!!
...全文
545 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
  • 打赏
  • 举报
回复
谢谢各位帮忙!!

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧