Private Sub Form_Load() If App.PrevInstance = False Then '避免重载 lHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf CallHookProc, App.hInstance, 0) End If End SubPrivate Sub Form_Unload(Cancel As Integer) If lHook <> 0 Then UnhookWindowsHookEx lHook End If End Sub'------- '模块 Public Const WH_KEYBOARD_LL = 13 '************************************************************************* '**函 数 名:CallHookProc '**输 入:ByVal code(Long) - '** :ByVal wParam(Long) - '** :ByVal lParam(Long) - '**输 出:(Long) - '**功能描述:键盘钩子函数 '**全局变量: '**调用模块: '**作 者: '**日 期: '**修 改 人: '**日 期: '**版 本:版本1.0 '************************************************************************* Public Function CallHookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If code = HC_ACTION Then CopyMemory Msg, lParam, LenB(Msg) Select Case wParam Case WM_SYSKEYDOWN, WM_KEYDOWN:
If Msg.message = 13 And (Msg.paramH And Alt_Down) <> 0 Then 'ALT+回车键 '------------------------------- End If End Select
End If If code <> 0 Then CallHookProc = CallNextHookEx(0, code, wParam, lParam) End IfEnd Function
‘声明----'获得窗体文本 Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long '获得窗体内容长度 Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long '获得光标处的句柄 Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPublic 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 Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long) Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As LongType EVENTMSG message As Long paramL As Long paramH As Long time As Long hwnd As Long End Type'获得光标的位置 Public Type POINTAPI X As Long Y As Long End TypePublic Const WH_KEYBOARD_LL = 13 Public Const Alt_Down = &H20 Public Const WH_MOUSE = 7'消息 Public Const HC_ACTION = 0 Public Const HC_SYSMODALOFF = 5 Public Const HC_SYSMODALON = 4Public Const WM_KEYDOWN = &H100 Public Const WM_KEYUP = &H101 Public Const WM_SYSKEYDOWN = &H104 Public Const WM_SYSKEYUP = &H105Public Msg As EVENTMSG Public lHook As Long
Private Sub Form_KeyPress(KeyAscii As Integer)
Form1.KeyPreview = True
If KeyAscii = 13 Then (事件) End If
End Sub 还是这种办法方便、实用一些
首先设置窗体的KeyPreview属性为True 然后再 KeyPress or KeyDown如果你要在整个程序,而不是某个窗体那么就只有用键盘钩子程序了不过你可以设置热键(但是设Enter键...)设置热键的话用 RegisterHotKey详细用法如下: RegisterHotKey The RegisterHotKey function defines a system-wide hot key. BOOL RegisterHotKey( HWND hWnd, // window to receive hot-key notification int id, // identifier of hot key UINT fsModifiers, // key-modifier flags UINT vk // virtual-key code );
Parameters hWnd Handle to the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop. id Specifies the identifier of the hot key. No other hot key in the calling thread should have the same identifier. An application must specify a value in the range 0x0000 through 0xBFFF. A shared dynamic-link library (DLL) must specify a value in the range 0xC000 through 0xFFFF (the range returned by theGlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier. fsModifiers Specifies keys that must be pressed in combination with the key specified by the nVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values. Value Meaning MOD_ALT Either alt key must be held down. MOD_CONTROL Either ctrl key must be held down. MOD_SHIFT Either shift key must be held down. MOD_WIN Either WINDOWS key was held down. These keys are labeled with the Microsoft Windows logo. vk Specifies the virtual-key code of the hot key. Return Values If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, callGetLastError.Res When a key is pressed, the system looks for a match against all hot keys. Upon finding a match, the system posts the WM_HOTKEY message to the message queue of the thread that registered the hot key. This message is posted to the beginning of the queue so it is removed by the next iteration of the message loop. This function cannot associate a hot key with a window created by another thread. RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key. If the window identified by the hWnd parameter already registered a hot key with the same identifier as that specified by the id parameter, the new values for the fsModifiers and vk parameters replace the previously specified values for these parameters. Windows CE: Windows CE versions 2.0 and later support an additional flag, called MOD_KEYUP, for the fsModifiers parameter. If you set the MOD_KEYUP flag, the window will be sent a WM_HOTKEY message on a key up event as well as on a key down event.RegisterHotKey can be used to register hot keys across threads.
Form1.KeyPreview = True
If KeyAscii = 13 Then
(事件)
End If
End Sub
If App.PrevInstance = False Then '避免重载
lHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf CallHookProc, App.hInstance, 0)
End If
End SubPrivate Sub Form_Unload(Cancel As Integer)
If lHook <> 0 Then
UnhookWindowsHookEx lHook
End If
End Sub'-------
'模块
Public Const WH_KEYBOARD_LL = 13
'*************************************************************************
'**函 数 名:CallHookProc
'**输 入:ByVal code(Long) -
'** :ByVal wParam(Long) -
'** :ByVal lParam(Long) -
'**输 出:(Long) -
'**功能描述:键盘钩子函数
'**全局变量:
'**调用模块:
'**作 者:
'**日 期:
'**修 改 人:
'**日 期:
'**版 本:版本1.0
'*************************************************************************
Public Function CallHookProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If code = HC_ACTION Then
CopyMemory Msg, lParam, LenB(Msg)
Select Case wParam
Case WM_SYSKEYDOWN, WM_KEYDOWN:
If Msg.message = 13 And (Msg.paramH And Alt_Down) <> 0 Then 'ALT+回车键
'-------------------------------
End If
End Select
End If
If code <> 0 Then
CallHookProc = CallNextHookEx(0, code, wParam, lParam)
End IfEnd Function
‘声明----'获得窗体文本
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'获得窗体内容长度
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'获得光标处的句柄
Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPublic 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 Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)
Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As LongType EVENTMSG
message As Long
paramL As Long
paramH As Long
time As Long
hwnd As Long
End Type'获得光标的位置
Public Type POINTAPI
X As Long
Y As Long
End TypePublic Const WH_KEYBOARD_LL = 13
Public Const Alt_Down = &H20
Public Const WH_MOUSE = 7'消息
Public Const HC_ACTION = 0
Public Const HC_SYSMODALOFF = 5
Public Const HC_SYSMODALON = 4Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105Public Msg As EVENTMSG
Public lHook As Long
Form1.KeyPreview = True
If KeyAscii = 13 Then
(事件)
End If
End Sub
还是这种办法方便、实用一些
然后再
KeyPress or KeyDown如果你要在整个程序,而不是某个窗体那么就只有用键盘钩子程序了不过你可以设置热键(但是设Enter键...)设置热键的话用
RegisterHotKey详细用法如下:
RegisterHotKey
The RegisterHotKey function defines a system-wide hot key. BOOL RegisterHotKey(
HWND hWnd, // window to receive hot-key notification
int id, // identifier of hot key
UINT fsModifiers, // key-modifier flags
UINT vk // virtual-key code
);
Parameters
hWnd
Handle to the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.
id
Specifies the identifier of the hot key. No other hot key in the calling thread should have the same identifier. An application must specify a value in the range 0x0000 through 0xBFFF. A shared dynamic-link library (DLL) must specify a value in the range 0xC000 through 0xFFFF (the range returned by theGlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier.
fsModifiers
Specifies keys that must be pressed in combination with the key specified by the nVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values. Value Meaning
MOD_ALT Either alt key must be held down.
MOD_CONTROL Either ctrl key must be held down.
MOD_SHIFT Either shift key must be held down.
MOD_WIN Either WINDOWS key was held down. These keys are labeled with the Microsoft Windows logo.
vk
Specifies the virtual-key code of the hot key.
Return Values
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, callGetLastError.Res
When a key is pressed, the system looks for a match against all hot keys. Upon finding a match, the system posts the WM_HOTKEY message to the message queue of the thread that registered the hot key. This message is posted to the beginning of the queue so it is removed by the next iteration of the message loop. This function cannot associate a hot key with a window created by another thread. RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key. If the window identified by the hWnd parameter already registered a hot key with the same identifier as that specified by the id parameter, the new values for the fsModifiers and vk parameters replace the previously specified values for these parameters. Windows CE: Windows CE versions 2.0 and later support an additional flag, called MOD_KEYUP, for the fsModifiers parameter. If you set the MOD_KEYUP flag, the window will be sent a WM_HOTKEY message on a key up event as well as on a key down event.RegisterHotKey can be used to register hot keys across threads.