关于vb6全局键盘事件
代码如下:PrivateDeclareFunctionGetAsyncKeyStateLib"user32"(ByValvKeyAsLong)AsIntegerPriva...
代码如下:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF5) Then
Text1 = Text1 & "1"
End If
End Sub
我想要只要按下F5(不松开),就显示一次1。请问高手,怎么修改代码? 展开
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF5) Then
Text1 = Text1 & "1"
End If
End Sub
我想要只要按下F5(不松开),就显示一次1。请问高手,怎么修改代码? 展开
3个回答
展开全部
窗体中
Private Sub Form_Load()
HooK
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHooK
End Sub
下面是模块,加入至工程:
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private 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
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13
Private lngHook As Long
Public Function HotKey(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim p As PKBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
Call CopyMemory(p, ByVal lParam, Len(p))
If p.vkCode = vbKeyF5 Then '开始
Form1.Text1.Text=Form1.Text1.Text & "1"
End If
Case Else
End Select
End If
Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)
End Function
Public Sub HooK()
lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf HotKey, App.hInstance, 0)
End Sub
Public Sub UnHooK()
Call UnhookWindowsHookEx(lngHook)
End Sub
追问
我觉得太麻烦了。假如我要做个全局键盘钩子呢?
展开全部
代码最上面加个变量
dim runonce as boolean
以下代码稍微改一下即可
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF5)=ture and runonce=false Then
Text1 = Text1 & "1"
runonce=true
End If
End Sub
然后在按键抬起事件时再把变量设置为runonce=false即可
dim runonce as boolean
以下代码稍微改一下即可
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF5)=ture and runonce=false Then
Text1 = Text1 & "1"
runonce=true
End If
End Sub
然后在按键抬起事件时再把变量设置为runonce=false即可
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
窗体的KeyPreview属性设为true,用Form_KeyDown试试
追问
这个好像不能实现全局吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询