请问如何实现监听键盘。
RT,使用vb.net如何实现监听键盘输入呢?
[解决办法]
js,根据输入字符的asc码判断,例如回车的asc码是13,那么:
- JScript code
<script type="text/javascript">function Enter(){ if(event.keyCode==13) { alert('您输入的回车!'); return false; }return true;}</script>
[解决办法]
仅供参考
http://topic.csdn.net/u/20071020/11/6dfaa044-6d75-4f32-8259-408f61e87e79.html
[解决办法]
使用HOOK
Module Module1
Public frm1 As New "你要监视的窗体名" '这个的作用,最后再说
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public hnexthookproc As Integer
Public Const PM_KEY_SPACE = &H20
Public Enum HookType
WH_KEYBOARD = 2
End Enum
Public Sub UnHook() '解Hook
If hnexthookproc <> 0 Then
UnhookWindowsHookEx(hnexthookproc)
hnexthookproc = 0
End If
End Sub
Public Function SetHook() '设置Hook
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD, AddressOf MyKeyboardProc, 0, GetCurrentThreadId())
'我把第三个参数设为0(即NULL),表示的是此Hook的代码在此进程中。第四个参数用了一个API去取安装Hook子程相关联的线程的标识符。(参见前面的API声明)
End Function
Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
MyKeyboardProc = 0
If nCode < 0 Then
MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
Exit Function
End If
If wParam = PM_KEY_SPACE Then
MyKeyboardProc = 1
'写入你自己的代码
frm1.TextBox1.Text = "HOOK成功!"
' frm1.TextBox2.Text = ""
frm1.TextBox2.Text += wParam.ToString
End If
End Function
Sub main()
Application.Run(frm1)
End Sub
End Module
[解决办法]
看看我这个
http://download.csdn.net/source/392508