关于全局热键
搞来搞去还是设置一个全局热键而已,请问如何设置多个全局热键呢?给代码。学习。满意再加分!VB的!...
搞来搞去还是设置一个全局热键而已,请问如何设置多个全局热键呢?给代码。学习。 满意再加分!
VB 的! 展开
VB 的! 展开
1个回答
展开全部
1.下面调用全局热键的示例:
激活 3 个热键:(注:SetHotkey 是我自定义的函数)
SetHotkey 1, "Ctrl,112", "Add" 按 Ctrl+F1 激活指定程序
SetHotkey 2, 113, "Add" 按 F2 激活指定程序
SetHotkey 3, "Ctrl+Alt,113", "Add" 按 Ctrl+Alt+F2 激活指定程序
注释:注:关于激活热键后的操作,由自定义函数 SetHotkey 的 KeyId 传送的值来判断
注销 3 个热键:
SetHotkey 1, "", "Del" 退出程序是一定要用上的,不然会导至程序死掉
SetHotkey 2, "", "Del"
SetHotkey 3, "", "Del"
请大家新建一个模块(.bas)文件,自定义 SetHotkey函数及其他,这样在以后的任何程序中只要调用此模块就可以了。
注释:本模块是有关热键操作的
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fskey_Modifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Const WM_HOTKEY = &H312
Const MOD_ALT = &H1
Const MOD_CONTROL = &H2
Const MOD_SHIFT = &H4
Const GWL_WNDPROC = (-4) 注释:窗口函数的地址
Dim key_preWinProc As Long 注释:用来保存窗口信息
Dim key_Modifiers As Long, key_uVirtKey As Long, key_idHotKey As Long
Dim key_IsWinAddress As Boolean 注释:是否取得窗口信息的判断
Function keyWndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
Select Case wParam 注释:wParam 值就是 key_idHotKey
Case 1 注释:激活 3 个热键后,3 个热键所对应的操作,大家在其他的程序中,只要修改此处就可以了
MsgBox "aa"
Case 2
MsgBox "bb"
Case 3
MsgBox "cc"
End Select
End If
注释:将消息传送给指定的窗口
keyWndproc = CallWindowProc(key_preWinProc, hwnd, Msg, wParam, lParam)
End Function
Function SetHotkey(ByVal KeyId As Long, ByVal KeyAss0 As String, ByVal Action As String)
Dim KeyAss1 As Long
Dim KeyAss2 As String
Dim i As Long
i = InStr(1, KeyAss0, ",")
If i = 0 Then
KeyAss1 = Val(KeyAss0)
KeyAss2 = ""
Else
KeyAss1 = Right(KeyAss0, Len(KeyAss0) - i)
KeyAss2 = Left(KeyAss0, i - 1)
End If
key_idHotKey = 0
key_Modifiers = 0
key_uVirtKey = 0
If key_IsWinAddress = False Then 注释:判断是否需要取得窗口信息,如果重复取得,再最后恢复窗口时,将会造成程序死掉
注释:记录原来的window程序地址
key_preWinProc = GetWindowLong(Form1.hwnd, GWL_WNDPROC)
注释:用自定义程序代替原来的window程序
SetWindowLong Form1.hwnd, GWL_WNDPROC, AddressOf keyWndproc
End If
key_idHotKey = KeyId
Select Case Action
Case "Add"
If KeyAss2 = "Ctrl" Then key_Modifiers = MOD_CONTROL
If KeyAss2 = "Alt" Then key_Modifiers = MOD_ALT
If KeyAss2 = "Shift" Then key_Modifiers = MOD_SHIFT
If KeyAss2 = "Ctrl+Alt" Then key_Modifiers = MOD_CONTROL + MOD_ALT
If KeyAss2 = "Ctrl+Shift" Then key_Modifiers = MOD_CONTROL + MOD_SHIFT
If KeyAss2 = "Ctrl+Alt+Shift" Then key_Modifiers = MOD_CONTROL + MOD_ALT + MOD_SHIFT
If KeyAss2 = "Shift+Alt" Then key_Modifiers = MOD_SHIFT + MOD_ALT
key_uVirtKey = Val(KeyAss1)
RegisterHotKey Form1.hwnd, key_idHotKey, key_Modifiers, key_uVirtKey 注释:向窗口注册系统热键
key_IsWinAddress = True 注释:不需要再取得窗口信息
Case "Del"
SetWindowLong Form1.hwnd, GWL_WNDPROC, key_preWinProc 注释:恢复窗口信息
UnregisterHotKey Form1.hwnd, key_uVirtKey 注释:取消系统热键
key_IsWinAddress = False 注释:可以再次取得窗口信息
End Select
End Function
该程序在 VB5.0,Win 98 下运行良好
-------------------------------------------------------
2.VB中定义全局热键
发布时间:2005-6-30 8:29:34
信息来源:メ冰枫ぱ雪
第一种:
比如说金山词霸用ctrl+alt+F1可实现开/关取词功能.
怎么做的呢?
bas 模块代码:
'********************************************************
' DECLARATIONS NEEDED TO INTERCEPT WINDOW MESSAGES *
'********************************************************
Public Declare Function SetWindowLong Lib "USER32" Alias
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong
As Long) As Long
Public Declare Function CallWindowProc Lib "USER32" Alias
"CallWindowProcA" (ByVal OldwndProc As Long, ByVal hWnd As Long, ByVal wMsg
As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = -4
Public OldwndProc As Long
'********************************************************
' DECLARATIONS NEEDED TO CREATE THE HOTKEY *
'********************************************************
Public Declare Function RegisterHotKey Lib "USER32" (ByVal hWnd As
Long, ByVal HotKeyID As Long, ByVal fsModifiers As Long, ByVal vKey As Long)
As Long
Public Declare Function UnregisterHotKey Lib "USER32" (ByVal hWnd As
Long, ByVal HotKeyID As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const WM_NCDESTROY = &H82
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const MOD_WIN = &H8
Public HotKeyID(12) As Long
Public Function WindowProc(ByVal hWnd As Long, ByVal WindowMsg As
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'This is where all the messages for this form as directed to
'We will need to check for the WindowMessage WM_HOTKEY
'to see if a hotkey is pressed and then we need
'To check the wParam to see which HotKey (1-12) has been pressed
Select Case WindowMsg
Case WM_HOTKEY
'WM_HOTKEY
'idHotKey = (int) wParam; // identifier of hot key
'fuModifiers = (UINT) LOWORD(lParam); // key-modifier flags
'uVirtKey = (UINT) HIWORD(lParam); // virtual-key code
Select Case wParam
'This is where you put the code you want to start
'whenever someone has pressed a hotkey
Case HotKeyID(1)
MsgBox "Hotkey F1 has been pressed."
Case HotKeyID(2)
MsgBox "Hotkey F2 has been pressed."
Case HotKeyID(3)
MsgBox "Hotkey F3 has been pressed."
Case HotKeyID(4)
MsgBox "Hotkey F4 has been pressed."
Case HotKeyID(5)
MsgBox "Hotkey F5 has been pressed."
Case HotKeyID(6)
MsgBox "Hotkey F6 has been pressed."
Case HotKeyID(7)
MsgBox "Hotkey F7 has been pressed."
Case HotKeyID(8)
MsgBox "Hotkey F8 has been pressed."
Case HotKeyID(9)
MsgBox "Hotkey F9 has been pressed."
Case HotKeyID(10)
MsgBox "Hotkey F10 has been pressed."
Case HotKeyID(11)
MsgBox "Hotkey F11 has been pressed."
Case HotKeyID(12)
MsgBox "Hotkey F12 has been pressed."
End Select
End Select
'No matter what happens we *always* end with the normal
'window procedure to finish/handle the message by
'calling the CallWindowProc.
WindowProc = CallWindowProc(OldwndProc, hWnd, WindowMsg, wParam,
lParam)
End Function
[h4]Form 模块代码:[h4]
'窗体上有12个checkbox控件数组,名字为chkKey.
'一个按钮cmdQUIT,
'代码:
Option Explicit
Dim HotKey As Byte
Dim HotKeyEnabled(12) As Boolean
Dim ClickedBefore As Boolean
Public Sub Cleanup()
For HotKey = 1 To 12
If HotKeyEnabled(HotKey) = True Then
Call DeleteHotkey
End If
Next HotKey
Unload Me
'Don't end with End since this will cause the program to crash
'even if you restored the SetWindowLong property to the OldwndHnd
End Sub
Public Sub CreateHotkey()
Dim ReturnValue As Long
'用户自己指定一个热键标识,一个整型数,范围:
'应用程序: 0x0000 -- 0xBFFF.
'DLL: 0xC000 -- 0xFFFF
'WM_HOTKEY 的 wPara 就是该标识的值
HotKeyID(HotKey) = HotKey
'F1 -- 112
'F2 -- 113
'......
HotKey = HotKey + 111
'Register 一个 HotKey
ReturnValue = RegisterHotKey(hWnd, HotKeyID(HotKey - 111), 0, HotKey)
'hWnd: 哪个窗体将接收 WM_HOTKEY 事件
'fsModifiers可取 :
'0, MOD_ALT, MOD_SHIFT, MOD_CONTROL, MOD_WIN
'HotKey是键码常数(注意不是ASCII码),即 vbKeyF1, vbKeyF2....
End Sub
Public Sub DeleteHotkey()
Dim ReturnValue As Long
'To disable/unload the selected hotkey (index number of the checkbox
'that was clicked on) simply unregister the HotKeyID (from the form it
'was registered to)
ReturnValue = UnregisterHotKey(hWnd, HotKeyID(HotKey))
End Sub
Private Sub chkKEY_Click(Index As Integer)
'A checkbox was clicked. We need to find out if now it is
'checked or not and act accordingly
If chkKEY(Index).Value = 1 Then 'The box that was clicked is now
checked
'so enable the function key as a hotkey.
'We know that index holds the value of the function-key to enable
HotKey = Index
Call CreateHotkey 'Go to the CreateHotKey sub to actually create
the hotkey
HotKeyEnabled(Index) = True
Else 'The box is now unchecked, so we need to disable this hotkey
HotKey = Index
Call DeleteHotkey
HotKeyEnabled(Index) = False
End If
If ClickedBefore = False Then
MsgBox "Now you can press F" & Index & " to see that it acts as a
hotkey." & vbCrLf & _
"If you want you can select more hotkeys.", vbInformation,
App.Title
ClickedBefore = True
End If
End Sub
Private Sub cmdQUIT_Click()
Call Cleanup
End Sub
Private Sub Form_Load()
'回调函数
OldwndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Private Sub Form_Terminate()
Call Cleanup
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Cleanup
End Sub
第二种:
按下某组键(HotKey)便执行某程式
在Dos的年代,我们常会以拦截中断向量的方式,做到按下某个hotkey而自动执行某个程式,在Window呢,也可以,不过它是用RegisterHotkey API来完成。
使用RegisterHotkey的概念是,它会定义一组按键的组合,当使用者不管在哪个程式之中,按下Window有注册的HotKey时,OS会传送WM_HOTKEY 的讯息给待接收该讯息的Window,而该Window收到WM_HOTKEY时,
便可知道有本身Thread所定义的HotKey被按下,於是可以从 wParam, lParam来得知是哪一组HotKey被按下。
RegisterHotKey(
ByVal hwnd As Long , //接收Hotkey的Window
ByVal idHotKey as Long, // identifier of hot key,range 0x0000 through
0xBFFF
ByVal Modifiers As Long, // 定义alt shift control等的组合
ByVal uVirtKey As Long // virtual-key code
)
WM_HOTKEY 叁数的定义
idHotKey = wParam; // identifier of hot key
Modifiers = (UINT) LOWORD(lParam); // key-modifier flags
uVirtKey = (UINT) HIWORD(lParam); // virtual-key code
所以了,除了设定RegisterHotkey外,另要使用SubClassing的技巧才会得知HotKey被按下;最后,程式结束前要使用UnRegisterHotkey将HotKey的定义取消掉。
以下程式功能是:不管在哪个程式中,只要按下ALT-SHIFT-G 便执行NotePad。
'以下在.Bas
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As
Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long,
ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long,
ByVal id As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hword As Integer
End Type
Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hword = uVirtKey Then
Debug.Print "HotKey Shift-Alt-G Pressed "
Shell "notepad", vbNormalFocus
End If
End If
End If
'将之送往原来的Window Procedure
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
'以下在 Form
Sub Form_Load()
Dim ret As Long
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc)
idHotKey = 1 'in the range &h0000 through &hBFFF
Modifiers = MOD_ALT + MOD_SHIFT
uVirtKey = vbKeyG
ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
'取消Message的截取,而使之又只送往原来的Window Procedure
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hwnd, uVirtKey)
End Sub
激活 3 个热键:(注:SetHotkey 是我自定义的函数)
SetHotkey 1, "Ctrl,112", "Add" 按 Ctrl+F1 激活指定程序
SetHotkey 2, 113, "Add" 按 F2 激活指定程序
SetHotkey 3, "Ctrl+Alt,113", "Add" 按 Ctrl+Alt+F2 激活指定程序
注释:注:关于激活热键后的操作,由自定义函数 SetHotkey 的 KeyId 传送的值来判断
注销 3 个热键:
SetHotkey 1, "", "Del" 退出程序是一定要用上的,不然会导至程序死掉
SetHotkey 2, "", "Del"
SetHotkey 3, "", "Del"
请大家新建一个模块(.bas)文件,自定义 SetHotkey函数及其他,这样在以后的任何程序中只要调用此模块就可以了。
注释:本模块是有关热键操作的
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fskey_Modifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Const WM_HOTKEY = &H312
Const MOD_ALT = &H1
Const MOD_CONTROL = &H2
Const MOD_SHIFT = &H4
Const GWL_WNDPROC = (-4) 注释:窗口函数的地址
Dim key_preWinProc As Long 注释:用来保存窗口信息
Dim key_Modifiers As Long, key_uVirtKey As Long, key_idHotKey As Long
Dim key_IsWinAddress As Boolean 注释:是否取得窗口信息的判断
Function keyWndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
Select Case wParam 注释:wParam 值就是 key_idHotKey
Case 1 注释:激活 3 个热键后,3 个热键所对应的操作,大家在其他的程序中,只要修改此处就可以了
MsgBox "aa"
Case 2
MsgBox "bb"
Case 3
MsgBox "cc"
End Select
End If
注释:将消息传送给指定的窗口
keyWndproc = CallWindowProc(key_preWinProc, hwnd, Msg, wParam, lParam)
End Function
Function SetHotkey(ByVal KeyId As Long, ByVal KeyAss0 As String, ByVal Action As String)
Dim KeyAss1 As Long
Dim KeyAss2 As String
Dim i As Long
i = InStr(1, KeyAss0, ",")
If i = 0 Then
KeyAss1 = Val(KeyAss0)
KeyAss2 = ""
Else
KeyAss1 = Right(KeyAss0, Len(KeyAss0) - i)
KeyAss2 = Left(KeyAss0, i - 1)
End If
key_idHotKey = 0
key_Modifiers = 0
key_uVirtKey = 0
If key_IsWinAddress = False Then 注释:判断是否需要取得窗口信息,如果重复取得,再最后恢复窗口时,将会造成程序死掉
注释:记录原来的window程序地址
key_preWinProc = GetWindowLong(Form1.hwnd, GWL_WNDPROC)
注释:用自定义程序代替原来的window程序
SetWindowLong Form1.hwnd, GWL_WNDPROC, AddressOf keyWndproc
End If
key_idHotKey = KeyId
Select Case Action
Case "Add"
If KeyAss2 = "Ctrl" Then key_Modifiers = MOD_CONTROL
If KeyAss2 = "Alt" Then key_Modifiers = MOD_ALT
If KeyAss2 = "Shift" Then key_Modifiers = MOD_SHIFT
If KeyAss2 = "Ctrl+Alt" Then key_Modifiers = MOD_CONTROL + MOD_ALT
If KeyAss2 = "Ctrl+Shift" Then key_Modifiers = MOD_CONTROL + MOD_SHIFT
If KeyAss2 = "Ctrl+Alt+Shift" Then key_Modifiers = MOD_CONTROL + MOD_ALT + MOD_SHIFT
If KeyAss2 = "Shift+Alt" Then key_Modifiers = MOD_SHIFT + MOD_ALT
key_uVirtKey = Val(KeyAss1)
RegisterHotKey Form1.hwnd, key_idHotKey, key_Modifiers, key_uVirtKey 注释:向窗口注册系统热键
key_IsWinAddress = True 注释:不需要再取得窗口信息
Case "Del"
SetWindowLong Form1.hwnd, GWL_WNDPROC, key_preWinProc 注释:恢复窗口信息
UnregisterHotKey Form1.hwnd, key_uVirtKey 注释:取消系统热键
key_IsWinAddress = False 注释:可以再次取得窗口信息
End Select
End Function
该程序在 VB5.0,Win 98 下运行良好
-------------------------------------------------------
2.VB中定义全局热键
发布时间:2005-6-30 8:29:34
信息来源:メ冰枫ぱ雪
第一种:
比如说金山词霸用ctrl+alt+F1可实现开/关取词功能.
怎么做的呢?
bas 模块代码:
'********************************************************
' DECLARATIONS NEEDED TO INTERCEPT WINDOW MESSAGES *
'********************************************************
Public Declare Function SetWindowLong Lib "USER32" Alias
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong
As Long) As Long
Public Declare Function CallWindowProc Lib "USER32" Alias
"CallWindowProcA" (ByVal OldwndProc As Long, ByVal hWnd As Long, ByVal wMsg
As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = -4
Public OldwndProc As Long
'********************************************************
' DECLARATIONS NEEDED TO CREATE THE HOTKEY *
'********************************************************
Public Declare Function RegisterHotKey Lib "USER32" (ByVal hWnd As
Long, ByVal HotKeyID As Long, ByVal fsModifiers As Long, ByVal vKey As Long)
As Long
Public Declare Function UnregisterHotKey Lib "USER32" (ByVal hWnd As
Long, ByVal HotKeyID As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const WM_NCDESTROY = &H82
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const MOD_WIN = &H8
Public HotKeyID(12) As Long
Public Function WindowProc(ByVal hWnd As Long, ByVal WindowMsg As
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'This is where all the messages for this form as directed to
'We will need to check for the WindowMessage WM_HOTKEY
'to see if a hotkey is pressed and then we need
'To check the wParam to see which HotKey (1-12) has been pressed
Select Case WindowMsg
Case WM_HOTKEY
'WM_HOTKEY
'idHotKey = (int) wParam; // identifier of hot key
'fuModifiers = (UINT) LOWORD(lParam); // key-modifier flags
'uVirtKey = (UINT) HIWORD(lParam); // virtual-key code
Select Case wParam
'This is where you put the code you want to start
'whenever someone has pressed a hotkey
Case HotKeyID(1)
MsgBox "Hotkey F1 has been pressed."
Case HotKeyID(2)
MsgBox "Hotkey F2 has been pressed."
Case HotKeyID(3)
MsgBox "Hotkey F3 has been pressed."
Case HotKeyID(4)
MsgBox "Hotkey F4 has been pressed."
Case HotKeyID(5)
MsgBox "Hotkey F5 has been pressed."
Case HotKeyID(6)
MsgBox "Hotkey F6 has been pressed."
Case HotKeyID(7)
MsgBox "Hotkey F7 has been pressed."
Case HotKeyID(8)
MsgBox "Hotkey F8 has been pressed."
Case HotKeyID(9)
MsgBox "Hotkey F9 has been pressed."
Case HotKeyID(10)
MsgBox "Hotkey F10 has been pressed."
Case HotKeyID(11)
MsgBox "Hotkey F11 has been pressed."
Case HotKeyID(12)
MsgBox "Hotkey F12 has been pressed."
End Select
End Select
'No matter what happens we *always* end with the normal
'window procedure to finish/handle the message by
'calling the CallWindowProc.
WindowProc = CallWindowProc(OldwndProc, hWnd, WindowMsg, wParam,
lParam)
End Function
[h4]Form 模块代码:[h4]
'窗体上有12个checkbox控件数组,名字为chkKey.
'一个按钮cmdQUIT,
'代码:
Option Explicit
Dim HotKey As Byte
Dim HotKeyEnabled(12) As Boolean
Dim ClickedBefore As Boolean
Public Sub Cleanup()
For HotKey = 1 To 12
If HotKeyEnabled(HotKey) = True Then
Call DeleteHotkey
End If
Next HotKey
Unload Me
'Don't end with End since this will cause the program to crash
'even if you restored the SetWindowLong property to the OldwndHnd
End Sub
Public Sub CreateHotkey()
Dim ReturnValue As Long
'用户自己指定一个热键标识,一个整型数,范围:
'应用程序: 0x0000 -- 0xBFFF.
'DLL: 0xC000 -- 0xFFFF
'WM_HOTKEY 的 wPara 就是该标识的值
HotKeyID(HotKey) = HotKey
'F1 -- 112
'F2 -- 113
'......
HotKey = HotKey + 111
'Register 一个 HotKey
ReturnValue = RegisterHotKey(hWnd, HotKeyID(HotKey - 111), 0, HotKey)
'hWnd: 哪个窗体将接收 WM_HOTKEY 事件
'fsModifiers可取 :
'0, MOD_ALT, MOD_SHIFT, MOD_CONTROL, MOD_WIN
'HotKey是键码常数(注意不是ASCII码),即 vbKeyF1, vbKeyF2....
End Sub
Public Sub DeleteHotkey()
Dim ReturnValue As Long
'To disable/unload the selected hotkey (index number of the checkbox
'that was clicked on) simply unregister the HotKeyID (from the form it
'was registered to)
ReturnValue = UnregisterHotKey(hWnd, HotKeyID(HotKey))
End Sub
Private Sub chkKEY_Click(Index As Integer)
'A checkbox was clicked. We need to find out if now it is
'checked or not and act accordingly
If chkKEY(Index).Value = 1 Then 'The box that was clicked is now
checked
'so enable the function key as a hotkey.
'We know that index holds the value of the function-key to enable
HotKey = Index
Call CreateHotkey 'Go to the CreateHotKey sub to actually create
the hotkey
HotKeyEnabled(Index) = True
Else 'The box is now unchecked, so we need to disable this hotkey
HotKey = Index
Call DeleteHotkey
HotKeyEnabled(Index) = False
End If
If ClickedBefore = False Then
MsgBox "Now you can press F" & Index & " to see that it acts as a
hotkey." & vbCrLf & _
"If you want you can select more hotkeys.", vbInformation,
App.Title
ClickedBefore = True
End If
End Sub
Private Sub cmdQUIT_Click()
Call Cleanup
End Sub
Private Sub Form_Load()
'回调函数
OldwndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Private Sub Form_Terminate()
Call Cleanup
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Cleanup
End Sub
第二种:
按下某组键(HotKey)便执行某程式
在Dos的年代,我们常会以拦截中断向量的方式,做到按下某个hotkey而自动执行某个程式,在Window呢,也可以,不过它是用RegisterHotkey API来完成。
使用RegisterHotkey的概念是,它会定义一组按键的组合,当使用者不管在哪个程式之中,按下Window有注册的HotKey时,OS会传送WM_HOTKEY 的讯息给待接收该讯息的Window,而该Window收到WM_HOTKEY时,
便可知道有本身Thread所定义的HotKey被按下,於是可以从 wParam, lParam来得知是哪一组HotKey被按下。
RegisterHotKey(
ByVal hwnd As Long , //接收Hotkey的Window
ByVal idHotKey as Long, // identifier of hot key,range 0x0000 through
0xBFFF
ByVal Modifiers As Long, // 定义alt shift control等的组合
ByVal uVirtKey As Long // virtual-key code
)
WM_HOTKEY 叁数的定义
idHotKey = wParam; // identifier of hot key
Modifiers = (UINT) LOWORD(lParam); // key-modifier flags
uVirtKey = (UINT) HIWORD(lParam); // virtual-key code
所以了,除了设定RegisterHotkey外,另要使用SubClassing的技巧才会得知HotKey被按下;最后,程式结束前要使用UnRegisterHotkey将HotKey的定义取消掉。
以下程式功能是:不管在哪个程式中,只要按下ALT-SHIFT-G 便执行NotePad。
'以下在.Bas
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As
Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long,
ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long,
ByVal id As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hword As Integer
End Type
Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hword = uVirtKey Then
Debug.Print "HotKey Shift-Alt-G Pressed "
Shell "notepad", vbNormalFocus
End If
End If
End If
'将之送往原来的Window Procedure
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
'以下在 Form
Sub Form_Load()
Dim ret As Long
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc)
idHotKey = 1 'in the range &h0000 through &hBFFF
Modifiers = MOD_ALT + MOD_SHIFT
uVirtKey = vbKeyG
ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
'取消Message的截取,而使之又只送往原来的Window Procedure
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hwnd, uVirtKey)
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询