VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 1800
TabIndex = 0
Top = 1320
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_SETHOTKEY = &H32
Const HOTKEYF_SHIFT = &H1 ' shift键
Const HOTKEYF_CONTROL = &H2 'ctrl键
Const HOTKEYF_ALT = &H4 'alt键
Const HOTKEYF_EXT = &H8 '附加键
Private Type tInteger
aint As Integer
End Type
Private Type t2Byte
lByte As Byte
hByte As Byte
End Type
Private ii As tInteger
Private bb As t2Byte
Private Sub Command1_Click()
Dim wParam As Long, I As Long
'设定ctl-shift-T为该window的hotkey
bb.hByte = HOTKEYF_CONTROL Or HOTKEYF_SHIFT
bb.lByte = vbKeyT
LSet ii = bb
wParam = CLng(ii.aint)
I = SendMessage(Me.hwnd, WM_SETHOTKEY, wParam, 0)
If I = 1 Then
Debug.Print "Ctl-Shift-T为hotkey"
Else
If I = 2 Then
Debug.Print "有其他Window也用Ctl-Shift-T作Hotkey"
Else
Debug.Print "指定失败"
End If
End If
End Sub
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 1800
TabIndex = 0
Top = 1320
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_SETHOTKEY = &H32
Const HOTKEYF_SHIFT = &H1 ' shift键
Const HOTKEYF_CONTROL = &H2 'ctrl键
Const HOTKEYF_ALT = &H4 'alt键
Const HOTKEYF_EXT = &H8 '附加键
Private Type tInteger
aint As Integer
End Type
Private Type t2Byte
lByte As Byte
hByte As Byte
End Type
Private ii As tInteger
Private bb As t2Byte
Private Sub Command1_Click()
Dim wParam As Long, I As Long
'设定ctl-shift-T为该window的hotkey
bb.hByte = HOTKEYF_CONTROL Or HOTKEYF_SHIFT
bb.lByte = vbKeyT
LSet ii = bb
wParam = CLng(ii.aint)
I = SendMessage(Me.hwnd, WM_SETHOTKEY, wParam, 0)
If I = 1 Then
Debug.Print "Ctl-Shift-T为hotkey"
Else
If I = 2 Then
Debug.Print "有其他Window也用Ctl-Shift-T作Hotkey"
Else
Debug.Print "指定失败"
End If
End If
End Sub
"user32" _
Alias "SendMessageA"_
(ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam_
As Long, lParam As Long) As LongPrivate Const WM_SETHOTKEY = &H32
'// Shift + A
Private Const HK_SHIFTA = &H141
'// Shift * B
Private Const HK_SHIFTB = &H142
'// Control + A
Private Const HK_CONTROLA = &H241
Private Const HK_ALTZ = &H45APrivate Sub cmdEnd_Click()
Unload Me
End SubPrivate Sub Form_Load()
'// Start subclassing the form
Subclass Me.hwnd'// Assign our hotkey
SendMessage Me.hwnd, WM_SETHOTKEY,_
HK_ALTZ, 0
End SubPrivate Sub Form_Unload(Cancel As Integer)
'// End subclassing unless you want
'// abnormal effects on the system
UnSubclass Me.hwnd
End Sub
Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal wNewWord As Long) As Long
Public 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 LongPublic Const GWL_WNDPROC As Long = (-4)
Public Const WM_ACTIVATE As Long = &H6
Public Const WM_ACTIVATEAPP As Long = &H1C
Public Const WA_INACTIVE As Long = 0
Public Const WA_ACTIVE As Long = 1
Public Const WA_CLICKACTIVE As Long = 2'// This variable holds the previous window
'// procedure's address so that we can pass
'// messages back to it.
Public OldProc As Long'// This flag tells us whether we are
'// currently subclassing the form.
Public blnSubclassed As BooleanPublic Function WndProc(ByVal hwnd As Long,_
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long'// This function handles messages sent
'// to our formOn Error Resume NextSelect Case uMsg Case WM_ACTIVATEAPP
Select Case LoWord(wParam)
'// Only message that we want to handle
'// Tells us that the form has been activated
Case WA_ACTIVE
'// Carry out your function/procedure here
Case Else
End Select
Case Else
End Select'// Check if we are subclassing
If blnSubclassed = True Then
'// Pass any messages on to the old window procedure
WndProc = CallWindowProc(OldProc, _
hwnd, uMsg, wParam, ByVal lParam)
Else
blnSubclassed = False
End If
End FunctionPublic Sub UnSubclass(hwnd As Long)
'// Check if we have actually implemented our
'// window procedure
If OldProc Then
'// If so then pass control back
SetWindowLong hwnd, GWL_WNDPROC, OldProc
OldProc = 0
End If
End SubPublic Sub Subclass(hwnd As Long)
On Error Resume Next'// Get the address of our window procedure
'// and make it the default for our form
OldProc = SetWindowLong(hwnd, GWL_WNDPROC,_
AddressOf WndProc)
End Sub'// Function by Randy Birch:
'// http://www.mvps.org/vbnet
Public Function LoWord(dw As Long) As Integer
If dw And &H8000 Then
LoWord = &H8000 Or (dw And &H7FFF&)
Else
LoWord = dw And &HFFF&
End If
End Function
http://www.developer.com/net/vb/article.php/1539451注册了热键后,在用户按热键之后系统会发送消息到注册热键的窗口,所以你需要像上面那样子类化窗口以接收热键消息。
Option ExplicitPrivate Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End TypePrivate Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
Dim Message As Msg
'loop until bCancel is set to True
Do While Not bCancel
'wait for a message
WaitMessage
'check if it's a HOTKEY-message
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
'响应热键,调用记事本,可以根据需要换成你的代码
Shell "notepad", vbNormalFocus
End If
DoEvents
Loop
End Sub
Private Sub Form_Load()
Dim ret As Long
bCancel = False
'注册Ctrl-Shift-T为hotkey
ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_SHIFT Or MOD_CONTROL, vbKeyT)
Show
ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
'unregister hotkey
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub