然后可以在各控件的keydown中调用:
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
SetShortcut(vbKeyBackup,13)
End Sub这样每次按下 退格 + 回车 就会触发Command1的事件请问该怎么实现?
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
SetShortcut(vbKeyBackup,13)
End Sub这样每次按下 退格 + 回车 就会触发Command1的事件请问该怎么实现?
"SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long Declare Function DefWindowProc Lib "user32" _
Alias "DefWindowProcA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long Public Const WM_SETHOTKEY = H32
Public Const WM_SHOWWINDOW = H18
Public Const HK_SHIFTA = H141 'Shift + A
Public Const HK_SHIFTB = H142 'Shift * B
Public Const HK_CONTROLA = H241 'Control + A
Public Const HK_ALTZ = H45A 'The value of the key-combination has to
'declared in lowbyte/highbyte-format
'That means as a hex-number: the last two
'characters specify the lowbyte (e.g.: 41 = a),
'the first the highbyte (e.g.: 01 = 1 = Shift)
'-Add this code to the form's load procedure: Private Sub Form_Load()
Me.WindowState = vbMinimized
'Let windows know what hotkey you want for
'your app, setting of lParam has no effect
erg = SendMessage(Me.hwnd, WM_SETHOTKEY, _
HK_ALTZ, 0)
'Check if succesfull
If erg <> 1 Then
MsgBox "You need another hotkey", vbOKOnly, _
"Error"
End If
'Tell windows what it should do, when the hotkey
'is pressed -> show the window!
'The setting of wParam and lParam has no effect
erg = DefWindowProc(Me.hwnd, WM_SHOWWINDOW, _
0, 0)
End Sub Note: Press Alt + Z to show the form
只是假设^_^
如果要设置多个快捷键怎么办?比如我同时注册了3组快捷键,该怎样处理?
'In a module
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'if idHook is less than zero, no further processing is required
If idHook < 0 Then
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
Else
'check if SHIFT-S is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
'show the result
Form1.Print "Shift-S pressed ..."
End If
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End If
End Function'In a form, called Form1
Private Sub Form_Load() 'set a keyboard hook
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub
Private Sub Form_Unload(Cancel As Integer)
'remove the windows-hook
UnhookWindowsHookEx hHook
End Sub
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'if idHook is less than zero, no further processing is required
If idHook < 0 Then
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
Else
'check if SHIFT-S is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
'show the result
Form1.Print "Shift-S pressed ..."
End If
'check if SHIFT-A is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("A") Then
'show the result
Form1.Print "Shift-A pressed ..."
End If
'check if SHIFT-B is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("B") Then
'show the result
Form1.Print "Shift-B pressed ..."
End If
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End If
End Function'In a form, called Form1
Private Sub Form_Load() 'set a keyboard hook
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub
Private Sub Form_Unload(Cancel As Integer)
'remove the windows-hook
UnhookWindowsHookEx hHook
End Sub
你的代码行是行,但为和每次按下快捷键下面的代码会执行4次而不是一次?
Form1.Print "Shift-S pressed ..."
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Dim bln As BooleanPublic Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If idHook >= 0 Then
'check if SHIFT-S is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
'show the result
If bln Then
Form1.Print "Shift-S pressed ..."
bln = False
Else
bln = True
End If
End If
KeyboardProc = 1
Exit Function
End If
KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
http://www.china-askpro.com/msg9/qa88.shtml
http://www.csdn.net/Develop/article/17%5C17917.shtm
http://vip.6to23.com/NowCan1/tech/kbtrc.htm
参考微软的文章:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q177/9/92.asp&NoWebContent=1