模块: Option Explicit Public Const VK_CONTROL = &H11 Public Const WH_KEYBOARD = 2 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_CONTROL) And &HF0000000) And wParam = Asc("A") Then 'show the result MsgBox "Äã°´ÏÂÁËCtrl-A" '»»³ÉÄãµÄ´úÂë ElseIf (GetKeyState(VK_CONTROL) And &HF0000000) And wParam = Asc("C") Then 'show the result MsgBox "Äã°´ÏÂÁËCtrl-C" '»»³ÉÄãµÄ´úÂë End If 'call the next hook KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) End If End Function窗体: Option ExplicitPrivate Sub Form_Load() hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID) End SubPrivate Sub Form_Unload(Cancel As Integer) 'remove the windows-hook UnhookWindowsHookEx hHook End Sub 应该就是这个意思
参考: http://www.intelbase.cn/read.asp?NewsID=402
直接注册热键即可: 'Example Name:RegisterHotkey Private 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 Type Private 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 'minimize the form WindowState = vbMinimized End If 'let the operating system process other events DoEvents Loop End Sub Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: [email protected] Dim ret As Long bCancel = False 'register the Ctrl-F hotkey ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF) 'show some information Me.AutoRedraw = True Me.Print "Press CTRL-F to minimize this form" 'show the form and Show 'process the Hotkey messages ProcessMessages End Sub Private Sub Form_Unload(Cancel As Integer) bCancel = True 'unregister hotkey Call UnregisterHotKey(Me.hWnd, &HBFFF&) End Sub
参考下这个and表达方式: Private Sub Text1_KeyDown(KeyCode As Integer, _ Shift As Integer) If KeyCode = vbKeyA And Shift = 2 Then MsgBox "You Ctrl + A key." End Sub
将顶级菜单的caption属性设为""
如果是想当本窗体活动时,在任何控件是按键都先处理一下,可以设置Form 的KeyPreview属性为True,然后在其KeyDown或KeyUp事件中处理。
如果是想实现在本进程范围内,拦截键盘消息,可以用进程钩子。
如果是想实现全局(整个系统)内,拦截键盘消息,可以用全局钩子,但要用Link手动连接.obj文件,把钩子函数放到.dll文件中。下面是KeyPreview的说明:KeyPreview 属性
返回或设置一个值,以决定是否在控件的键盘事件之前激活窗体的键盘事件。键盘事件为:KeyDown、KeyUp 和 KeyPress。语法object.KeyPreview [= boolean]KeyPreview 属性语法有以下组成部分:部分 描述
Object 对象表达式,其值是“应用于”列表中的一个对象。
Boolean 布尔表达式,指定如何接收事件。Setting 中有其说明。
设置值 boolean 的设置值为:设置值 描述
True 窗体先接收键盘事件,然后是活动控件接收事件。
False (缺省值)活动控件接收键盘事件,而窗体不接收。
说明可以用该属性,生成窗体的键盘处理程序,例如,应用程序利用功能键时,需要在窗体级处理击键,而不是为每个可以接收击键事件的控件编写程序。如果窗体中没有可见和有效的控件,它将自动接收所有键盘事件。若要在窗体级处理键盘事件、而不允许控件接收键盘事件时,在窗体的 KeyPress 事件中设置 KeyAscii 为 0,在窗体的 KeyDown 事件中设置 KeyCode 为 0。 注意 一些控件能够拦截键盘事件,以致窗体不能接收它们。这样的例子有:CommandButton 控件有焦点时的 ENTER 键、以及焦点在 ListBox 控件上时的方向键。
Option Explicit
Public Const VK_CONTROL = &H11
Public Const WH_KEYBOARD = 2
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_CONTROL) And &HF0000000) And wParam = Asc("A") Then
'show the result
MsgBox "Äã°´ÏÂÁËCtrl-A" '»»³ÉÄãµÄ´úÂë
ElseIf (GetKeyState(VK_CONTROL) And &HF0000000) And wParam = Asc("C") Then
'show the result
MsgBox "Äã°´ÏÂÁËCtrl-C" '»»³ÉÄãµÄ´úÂë
End If
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End If
End Function窗体:
Option ExplicitPrivate Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End SubPrivate Sub Form_Unload(Cancel As Integer)
'remove the windows-hook
UnhookWindowsHookEx hHook
End Sub
应该就是这个意思
http://www.intelbase.cn/read.asp?NewsID=402
'Example Name:RegisterHotkey
Private 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 Type
Private 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
'minimize the form
WindowState = vbMinimized
End If
'let the operating system process other events
DoEvents
Loop
End Sub
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim ret As Long
bCancel = False
'register the Ctrl-F hotkey
ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
'show some information
Me.AutoRedraw = True
Me.Print "Press CTRL-F to minimize this form"
'show the form and
Show
'process the Hotkey messages
ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
'unregister hotkey
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub
触发键盘的一共就那么几个事件
KEYDOWN不行,那就试一下KEYPRESS或者KEYUP事件好了
估计是热键选择的问题吧?
你可以选几个冷僻键子来用用!
你可以不在键盘事件里写
在其他事件里,判断按下的键子不就行了吗?
每个键子都有ARCII码的
对应上就OK了!
你说呢?
随便说的哈,表丢我
Private Sub Text1_KeyDown(KeyCode As Integer, _
Shift As Integer)
If KeyCode = vbKeyA And Shift = 2 Then MsgBox "You Ctrl + A key."
End Sub