VB使用keybd_event

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB使用keybd_event相关的知识,希望对你有一定的参考价值。

我在程序中判断键盘是否按下R键 如果按下则启动 keybd_event 虚拟按下R键 只要R键不放开~!就一直启动~! 但是这些功能我都可以完成了~!就是按R键不放~!我按其他键就是失效~!怎样可以让他按其他键也不失效~!给出代码即可~!

参考技术A 你说的功能我不会·等待高人解答吧·不过我给你一段钩子的代码希望能帮助到你模块:Option Explicit
Public Const WH_KEYBOARD = 13
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public hNextHookProc As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const BM_CLICK = &HF5Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End TypePublic Sub UnHookKBD()
On Error Resume Next
If hNextHookProc <> 0 Then
UnhookWindowsHookEx hNextHookProc
hNextHookProc = 0
End If
End SubPublic Function EnableKBDHook()
On Error Resume Next
If hNextHookProc <> 0 Then
Exit Function
End If
hNextHookProc = SetWindowsHookEx(WH_KEYBOARD, AddressOf MyKBHFunc, App.hInstance, 0)
If hNextHookProc <> 0 Then
EnableKBDHook = hNextHookProc
End IfEnd FunctionPublic Function MyKBHFunc(ByVal iCode As Long, ByVal wParam As Long, lParam As KBDLLHOOKSTRUCT) As Long
On Error Resume Next
MyKBHFunc = 0
If iCode < 0 Then
MyKBHFunc = CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
Exit Function
End If
If lParam.vkCode = vbKeyR Then ‘如果按下R,这里你可以改改
MyKBHFunc = 1
Else
Call CallNextHookEx(hNextHookProc, iCode, wParam, lParam) ’传递给下一钩子
End If
End Function
窗体中加载钩子用:Call EnableKBDHook别忘记在窗体的unload事件中加入UnHookKBD ’卸载钩子
参考技术B 在注册表内注册个热键 然后用钩子 勾取到键盘是r键 就执行

以上是关于VB使用keybd_event的主要内容,如果未能解决你的问题,请参考以下文章

vb如何使用she皮肤

vb6怎么使用EXCEL的表格

vb的使用问题?

VB中combobox的使用

vb.net中怎么使用createobject添加类呢,就好像vb中一样

VB中如何使用TreeView控件.