vb 如何按下键盘某键后触发事件
在程序非激活状态,我想让创建一个热键,比如(F4),按后程序自动开始。必须是按键按下时才会触发事件,弹起的时候不触发,我从网上找的大多都是GetKeyState(keyc...
在程序非激活状态,我想让创建一个热键,比如(F4),按后程序自动开始。必须是按键按下时才会触发事件,弹起的时候不触发,我从网上找的大多都是GetKeyState(keycode1) 这样的取得按键状态的方法,缺点是这样是在按键按下的时候就触发了,弹起的时候又触发一次,按完一个键通常是触发2次事件,得到完全相反的结果。
我现在想得到的就是当按下F4后,在按键按下时才触发,按键弹起时就不要触发了,或者弹起时触发按下时不触发,总的来说就是:不要按下按键后触发2次事件。
如果代码长,能不能截下图发上来。 展开
我现在想得到的就是当按下F4后,在按键按下时才触发,按键弹起时就不要触发了,或者弹起时触发按下时不触发,总的来说就是:不要按下按键后触发2次事件。
如果代码长,能不能截下图发上来。 展开
2个回答
展开全部
给你个VB键盘钩子代码
模块中:
Public Function LowLevelKeyboardProc(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'******************************************************
Dim p As PKBDLLHOOKSTRUCT
Dim strKey As String
If ncode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN
CopyMemory p, ByVal lParam, Len(p)
If p.vkCode > 0 And p.vkCode < &H92 Then
If (GetKeyState(VK_CONTROL) And &H8000) <> 0 Then
strKey = "{Ctrl}+"
End If
If (GetKeyState(VK_SHIFT) And &H8000) <> 0 Then
strKey = strKey & "{Shift}+"
End If
Select Case p.vkCode
Case VK_MULTIPLY
strKey = strKey & "{Multiply}"
Case VK_ADD
strKey = strKey & "{Add}"
Case VK_SEPARATOR
strKey = strKey & "{Separator}"
Case VK_SUBTRACT
strKey = strKey & "{Subtract}"
Case VK_DECIMAL
strKey = strKey & "{Decimal}"
Case VK_DIVIDE
strKey = strKey & "{Divide}"
Case VK_PRIOR
strKey = strKey & "{Prior}"
Case VK_NEXT
strKey = strKey & "{Nexe}"
Case VK_END
strKey = strKey & "{End}"
Case VK_HOME
strKey = strKey & "{Home}"
Case VK_LEFT
strKey = strKey & "{Left}"
Case VK_UP
strKey = strKey & "{Up}"
Case VK_DOWN
strKey = strKey & "{Down}"
Case VK_RIGHT
strKey = strKey & "{Right}"
Case VK_INSERT
strKey = strKey & "{Insert}"
Case VK_DELETE
strKey = strKey & "{Delete}"
Case VK_BACK
strKey = strKey & "{Back}"
Case VK_F1 To VK_F12
strKey = strKey & "F" & CStr(p.vkCode - &H70)
Case VK_NUMLOCK
strKey = strKey & "{NumLock}"
Case VK_NUMPAD0 To VK_NUMPAD9
strKey = strKey & CStr(p.vkCode - &H60)
Case VK_RETURN
strKey = strKey & "{Return}"
Case VK_SPACE
strKey = strKey & "{Space}"
Case VK_TAB
strKey = strKey & "{Tab}"
Case VK_ESCAPE
strKey = strKey & "{Escape}"
Case &H30 To &H39, &H41 To &H5A
strKey = strKey & Chr(p.vkCode)
Case Else
End Select
If strKey = "{Ctrl}+E" Then
我要做的事
End If
End If
Case Else
End Select
End If
CallNextHookEx WH_KEYBOARD_LL, ncode, wParam, lParam
End Function
Public Sub Hook()
PrevHook = SetWindowsHookEx(WH_KEYBOARD_LL, _
AddressOf LowLevelKeyboardProc, _
App.hInstance, _
0)
End Sub
Public Sub ReleaseHook()
Call UnhookWindowsHookEx(PrevHook)
End Sub
声明用模块中:
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
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 GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Const VK_CONTROL = &H11
Public Const VK_SHIFT = &H10
Public Const VK_MULTIPLY = &H6A
Public Const VK_ADD = &H6B
Public Const VK_SEPARATOR = &H6C
Public Const VK_SUBTRACT = &H6D
Public Const VK_DECIMAL = &H6E
Public Const VK_DIVIDE = &H6F
Public Const VK_PRIOR = &H21
Public Const VK_NEXT = &H22
Public Const VK_END = &H23
Public Const VK_HOME = &H24
Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_DOWN = &H28
Public Const VK_RIGHT = &H27
Public Const VK_INSERT = &H2D
Public Const VK_DELETE = &H2E
Public Const VK_F1 = &H70
Public Const VK_F12 = &H7B
Public Const VK_NUMLOCK = &H90
Public Const VK_NUMPAD0 = &H60
Public Const VK_NUMPAD9 = &H69
Public Const VK_RETURN = &HD
Public Const VK_SPACE = &H20
Public Const VK_TAB = &H9
Public Const VK_ESCAPE = &H1B
Public Const VK_BACK = &H8
Public Const WM_KEYDOWN = &H100
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const HC_ACTION = 0
Public Const WH_KEYBOARD_LL = 13
Public Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
窗体中:
load中写
Call Hook
就哦了。(程序是我从我一个程序里抠出来的,要有忘记的声明自己加上)
程序中。。按ctrl+e键以后运行我写字地方的代码。按其它键怎么改应该一看就明白。。实现功能自己写
模块中:
Public Function LowLevelKeyboardProc(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'******************************************************
Dim p As PKBDLLHOOKSTRUCT
Dim strKey As String
If ncode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN
CopyMemory p, ByVal lParam, Len(p)
If p.vkCode > 0 And p.vkCode < &H92 Then
If (GetKeyState(VK_CONTROL) And &H8000) <> 0 Then
strKey = "{Ctrl}+"
End If
If (GetKeyState(VK_SHIFT) And &H8000) <> 0 Then
strKey = strKey & "{Shift}+"
End If
Select Case p.vkCode
Case VK_MULTIPLY
strKey = strKey & "{Multiply}"
Case VK_ADD
strKey = strKey & "{Add}"
Case VK_SEPARATOR
strKey = strKey & "{Separator}"
Case VK_SUBTRACT
strKey = strKey & "{Subtract}"
Case VK_DECIMAL
strKey = strKey & "{Decimal}"
Case VK_DIVIDE
strKey = strKey & "{Divide}"
Case VK_PRIOR
strKey = strKey & "{Prior}"
Case VK_NEXT
strKey = strKey & "{Nexe}"
Case VK_END
strKey = strKey & "{End}"
Case VK_HOME
strKey = strKey & "{Home}"
Case VK_LEFT
strKey = strKey & "{Left}"
Case VK_UP
strKey = strKey & "{Up}"
Case VK_DOWN
strKey = strKey & "{Down}"
Case VK_RIGHT
strKey = strKey & "{Right}"
Case VK_INSERT
strKey = strKey & "{Insert}"
Case VK_DELETE
strKey = strKey & "{Delete}"
Case VK_BACK
strKey = strKey & "{Back}"
Case VK_F1 To VK_F12
strKey = strKey & "F" & CStr(p.vkCode - &H70)
Case VK_NUMLOCK
strKey = strKey & "{NumLock}"
Case VK_NUMPAD0 To VK_NUMPAD9
strKey = strKey & CStr(p.vkCode - &H60)
Case VK_RETURN
strKey = strKey & "{Return}"
Case VK_SPACE
strKey = strKey & "{Space}"
Case VK_TAB
strKey = strKey & "{Tab}"
Case VK_ESCAPE
strKey = strKey & "{Escape}"
Case &H30 To &H39, &H41 To &H5A
strKey = strKey & Chr(p.vkCode)
Case Else
End Select
If strKey = "{Ctrl}+E" Then
我要做的事
End If
End If
Case Else
End Select
End If
CallNextHookEx WH_KEYBOARD_LL, ncode, wParam, lParam
End Function
Public Sub Hook()
PrevHook = SetWindowsHookEx(WH_KEYBOARD_LL, _
AddressOf LowLevelKeyboardProc, _
App.hInstance, _
0)
End Sub
Public Sub ReleaseHook()
Call UnhookWindowsHookEx(PrevHook)
End Sub
声明用模块中:
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
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 GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Const VK_CONTROL = &H11
Public Const VK_SHIFT = &H10
Public Const VK_MULTIPLY = &H6A
Public Const VK_ADD = &H6B
Public Const VK_SEPARATOR = &H6C
Public Const VK_SUBTRACT = &H6D
Public Const VK_DECIMAL = &H6E
Public Const VK_DIVIDE = &H6F
Public Const VK_PRIOR = &H21
Public Const VK_NEXT = &H22
Public Const VK_END = &H23
Public Const VK_HOME = &H24
Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_DOWN = &H28
Public Const VK_RIGHT = &H27
Public Const VK_INSERT = &H2D
Public Const VK_DELETE = &H2E
Public Const VK_F1 = &H70
Public Const VK_F12 = &H7B
Public Const VK_NUMLOCK = &H90
Public Const VK_NUMPAD0 = &H60
Public Const VK_NUMPAD9 = &H69
Public Const VK_RETURN = &HD
Public Const VK_SPACE = &H20
Public Const VK_TAB = &H9
Public Const VK_ESCAPE = &H1B
Public Const VK_BACK = &H8
Public Const WM_KEYDOWN = &H100
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105
Public Const HC_ACTION = 0
Public Const WH_KEYBOARD_LL = 13
Public Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
窗体中:
load中写
Call Hook
就哦了。(程序是我从我一个程序里抠出来的,要有忘记的声明自己加上)
程序中。。按ctrl+e键以后运行我写字地方的代码。按其它键怎么改应该一看就明白。。实现功能自己写
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询