# 真的没人能帮我吗？！！！

Posted by wo ! wo ! wo ! on April 12, 1999 at 13:07:32:

如何在 Form 隐藏时得知某个键盘按键被按下？

我只知道要用这个API 但不只如何使用？
SetWindowsHook ’安装钩子过程
SetWindowsHookEx ’安装钩子过程

另有Microsoft一篇文章 我分析不明白 请帮帮我吧！
1. Start a new Standard EXE project in Visual Basic. Form1 is created by

default.

2. Add a TextBox and a CommandButton to Form1. Set the TabIndex of the TextBox
to 0.

3. Add a module to the project. From the Project menu, click Add Module.
4. Copy the following code to the Code window of Module1:

Public Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long

Public 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 PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Const WH_KEYBOARD = 2
Public Const KBH_MASK = &H20000000
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202

Global hHook As Long

Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
If nCode >= 0 Then
'Process keys you want to filter
If wParam = Asc("C") And (lParam And KBH_MASK) <> 0 Then
If (lParam And &HC0000000) = 0 Then
Form1.Command1.SetFocus
Call PostMessage(Form1.Command1.hwnd, _
WM_LBUTTONDOWN, 0, &H20002)
Call PostMessage(Form1.Command1.hwnd, _
WM_LBUTTONUP, 0, &H20002)
KeyboardProc = 1
Exit Function
End If
End If
End If
KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function

5. Copy the following code to the Code window of the Form1 form:

Option Explicit

Private Sub Command1_Click()
Debug.Print "Command1_Click"
End Sub

Private Sub Command1_GotFocus()
Debug.Print "Command1_GotFocus"
End Sub

Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD, _
AddressOf KeyboardProc, 0&, App.ThreadID)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call UnhookWindowsHookEx(hHook)
End Sub

Private Sub Text1_LostFocus()
Debug.Print "Text1_LostFocus"
End Sub

6. Press the F5 key to run the program.
The text box should have the focus. Click the command button and note the sequence of events that occur as shown in the debug window:

Text1_LostFocus
Command1_GotFocus
Command1_Click

Set the focus to the text box and press the ALT+C keys and note that the same events occur.

Notes
Hooks do not always behave the same way in the IDE as they do in an EXE. Make certain that you test your solution in an EXE before you move on to other parts of your project.

It is possible to intercept all of the keyboard input from all of the applications running on a system, but not using "pure" Visual Basic. The hook must be placed in a standard DLL; while Visual Basic can create OLE DLLs, it cannot create standard DLLs.

(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Arsenio Locsin, Microsoft Corporation

Additional query words: subclass

Keywords : VBKBWinAPI kbVBp500 kbVBp600
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbhowto

Follow Ups:

- 请先参考 cww 11:24:46 4/13/99 (0)