11887.html
来自「以电子书的形式收集了VB一些常见问题解决方法,可以很方便的查找自己需要解决的问题」· HTML 代码 · 共 26 行
HTML
26 行
<html>
<head>
<title>真的没人能帮我吗?!!!</title>
</head>
<body bgcolor="#FFFFFF" vlink="#808080">
<center>
<h1>真的没人能帮我吗?!!!</h1>
</center>
<hr size=7 width=75%>
<hr size=7 width=75%><p>
Posted by wo ! wo ! wo ! on April 12, 1999 at 13:07:32:<p>
<br>如何在 Form 隐藏时得知某个键盘按键被按下?<p><p>我只知道要用这个API 但不只如何使用?<br>SetWindowsHook ’安装钩子过程 <br>SetWindowsHookEx ’安装钩子过程<br> <p>另有Microsoft一篇文章 我分析不明白 请帮帮我吧!<br>1. Start a new Standard EXE project in Visual Basic. Form1 is created by <p> default.<p>2. Add a TextBox and a CommandButton to Form1. Set the TabIndex of the TextBox <br> to 0.<p>3. Add a module to the project. From the Project menu, click Add Module. <br>4. Copy the following code to the Code window of Module1: <p><br> Public Declare Function CallNextHookEx Lib "user32" _<br> (ByVal hHook As Long, _<br> ByVal nCode As Long, _<br> ByVal wParam As Long, _<br> ByVal lParam As Long) As Long<p> Public Declare Function UnhookWindowsHookEx Lib "user32" _<br> (ByVal hHook As Long) As Long<p> Public Declare Function SetWindowsHookEx Lib "user32" _<br> Alias "SetWindowsHookExA" _<br> (ByVal idHook As Long, _<br> ByVal lpfn As Long, _<br> ByVal hmod As Long, _<br> ByVal dwThreadId As Long) As Long<p> Public Declare Function PostMessage Lib "user32" _<br> Alias "PostMessageA" _<br> (ByVal hwnd As Long, _<br> ByVal wMsg As Long, _<br> ByVal wParam As Long, _<br> ByVal lParam As Long) As Long<p> Public Const WH_KEYBOARD = 2<br> Public Const KBH_MASK = &H20000000<br> Public Const WM_LBUTTONDOWN = &H201<br> Public Const WM_LBUTTONUP = &H202<p> Global hHook As Long<p> Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, _<br> ByVal lParam As Long) As Long<br> If nCode >= 0 Then<br> 'Process keys you want to filter<br> If wParam = Asc("C") And (lParam And KBH_MASK) <> 0 Then<br> If (lParam And &HC0000000) = 0 Then<br> Form1.Command1.SetFocus<br> Call PostMessage(Form1.Command1.hwnd, _<br> WM_LBUTTONDOWN, 0, &H20002)<br> Call PostMessage(Form1.Command1.hwnd, _<br> WM_LBUTTONUP, 0, &H20002)<br> KeyboardProc = 1<br> Exit Function<br> End If<br> End If<br> End If<br> KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)<br> End Function<p>5. Copy the following code to the Code window of the Form1 form: <p> Option Explicit<p> Private Sub Command1_Click()<br> Debug.Print "Command1_Click"<br> End Sub<p> Private Sub Command1_GotFocus()<br> Debug.Print "Command1_GotFocus"<br> End Sub<p> Private Sub Form_Load()<br> hHook = SetWindowsHookEx(WH_KEYBOARD, _<br> AddressOf KeyboardProc, 0&, App.ThreadID)<br> End Sub<p> Private Sub Form_Unload(Cancel As Integer)<br> Call UnhookWindowsHookEx(hHook)<br> End Sub<p> Private Sub Text1_LostFocus()<br> Debug.Print "Text1_LostFocus"<br> End Sub<p>6. Press the F5 key to run the program. <br>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: <p><br> Text1_LostFocus<br> Command1_GotFocus<br> Command1_Click<p>Set the focus to the text box and press the ALT+C keys and note that the same events occur. <p><br>Notes<br>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. <p>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. <p><br>(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Arsenio Locsin, Microsoft Corporation <p><br>Additional query words: subclass <p><br>Keywords : VBKBWinAPI kbVBp500 kbVBp600 <br>Version : WINDOWS:5.0,6.0<br>Platform : WINDOWS<br>Issue type : kbhowto<br> <p><br> <p><br>
<br>
<br><hr size=7 width=75%><p>
<a name="followups">Follow Ups:</a><br>
<ul><!--insert: 11887-->
<!--top: 11918--><li><a href="11918.html">请先参考</a> <b>cww</b> <i>11:24:46 4/13/99</i>
(<!--responses: 11918-->0)
<ul><!--insert: 11918-->
</ul><!--end: 11918-->
</ul><!--end: 11887-->
<br><hr size=7 width=75%><p>
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?