VB.NET中如何使用热键?

如何响应热键,即使应用程序不是活动的。
---------------------------------------------------------------

我可以啊,给你拷贝过来,不过格式有些不太好看.

DM [@mailru.com]
Hi, can somebody give sample or link for complete keyboard
hook in vb7

Gary [@online.microsoft.com]
To set a hook, call SetWindowsHookEx API from User32.dll. This function
installs an application-defined hook procedure into the hook chain
associated with the hook. The following example demonstrates how to set a
mouse hook and monitor the mouse events.

Step-by-step example:

Create Windows Application "ThreadSpecificMouseHook" using VB.NET. Form1 is
added by default.

Add the following line of code at the begining of Form1.vb:
Imports System.Runtime.InteropServices

Public Delegate Function CallBack( _
ByVal nCode As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer

Add following code inside the Form1 class:

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

'Declare hook handle as int
static int hHook = 0;

'Declare mouse hook constant.
'For other hook types these values can be obtained from Winuser.h in
Microsoft SDK.
Dim WH_MOUSE As Integer = 7
Shared hHook As Integer = 0

'Need to keep the reference so that the delegate is not garbage
collected
Private hookproc As CallBack

'Import for SetWindowsHookEx

  1<dllimport("user32.dll", callingconvention:="CallingConvention.StdCall)" charset:="CharSet.Auto,"> _   
  2Public Overloads Shared Function SetWindowsHookEx _   
  3(ByVal idHook As Integer, ByVal HookProc As CallBack, _   
  4ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer   
  5End Function   
  6  
  7'Import for CallNextHookEx   
  8<dllimport("user32.dll", callingconvention:="CallingConvention.StdCall)" charset:="CharSet.Auto,"> _   
  9Public Overloads Shared Function CallNextHookEx _   
 10(ByVal idHook As Integer, ByVal nCode As Integer, _   
 11ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer   
 12End Function   
 13  
 14'Import for UnhookWindowsHookEx   
 15<dllimport("user32.dll", callingconvention:="CallingConvention.StdCall)" charset:="CharSet.Auto,"> _   
 16Public Overloads Shared Function UnhookWindowsHookEx _   
 17(ByVal idHook As Integer) As Boolean   
 18End Function   
 19  
 20'Point Structure declaration   
 21<structlayout(layoutkind.sequential)> Public Structure Point   
 22Public x As Integer   
 23Public y As Integer   
 24End Structure   
 25  
 26'MouseHookStruct declaration   
 27<structlayout(layoutkind.sequential)> Public Structure MouseHookStruct   
 28Public pt As Point   
 29Public hwnd As Integer   
 30Public wHitTestCode As Integer   
 31Public dwExtraInfo As Integer   
 32End Structure   
 33  
 34Add a button to the form and the following code in the button1_click   
 35procedure:   
 36  
 37Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As   
 38System.EventArgs) Handles Button1.Click   
 39  
 40If hHook.Equals(0) Then   
 41hookproc = AddressOf MouseHookProc   
 42hHook = SetWindowsHookEx(WH_MOUSE, _   
 43hookproc, _   
 44IntPtr.Zero, _   
 45  
 46AppDomain.CurrentDomain.GetCurrentThreadId())   
 47If hHook.Equals(0) Then   
 48MsgBox("SetWindowsHookEx Failed")   
 49Return   
 50Else   
 51Button1.Text = "UnHook Windows Hook"   
 52End If   
 53Else   
 54Dim ret As Boolean = UnhookWindowsHookEx(hHook)   
 55  
 56If ret.Equals(False) Then   
 57MsgBox("UnhookWindowsHookEx Failed")   
 58Return   
 59Else   
 60hHook = 0   
 61Button1.Text = "Set Windows Hook"   
 62Me.Text = "Mouse Hook"   
 63End If   
 64End If   
 65  
 66End Sub   
 67  
 68Add the following code for MouseHookProc function in Form1 class:   
 69  
 70Public Shared Function MouseHookProc( _   
 71ByVal nCode As Integer, _   
 72ByVal wParam As IntPtr, _   
 73ByVal lParam As IntPtr) As Integer   
 74Dim MyMouseHookStruct As New MouseHookStruct()   
 75  
 76Dim ret As Integer   
 77  
 78If (nCode &lt; 0) Then   
 79Return CallNextHookEx(hHook, nCode, wParam, lParam)   
 80End If   
 81  
 82MyMouseHookStruct = CType(Marshal.PtrToStructure(lParam,   
 83MyMouseHookStruct.GetType()), MouseHookStruct)   
 84  
 85Dim tempForm As Form   
 86tempForm = Form.ActiveForm()   
 87  
 88Dim strCaption As String   
 89strCaption = "x = " &amp; MyMouseHookStruct.pt.x &amp; " y = " &amp;   
 90MyMouseHookStruct.pt.y   
 91  
 92tempForm.Text = strCaption   
 93Return CallNextHookEx(hHook, nCode, wParam, lParam)   
 94  
 95End Function   
 96  
 97Press F5 to run the project. Click the button on the form and it will set   
 98the hook. You will notice the mouse coordinates on the form caption bar   
 99when mouse pointer moves on the form. Click the button again to remove the   
100hook.   
101  
102*Global Hook Not Supported in .NET*   
103  
104Global hooks cannot be implemented in .NET. To install a global hook, a   
105hook needs to have a native dll export to inject itself into another   
106process which requires a valid, consistent function to call into. This   
107requires a DLL export, which .NET doesn't support. Managed code has no   
108concept of a consistent value for a function pointer because these function   
109pointers are proxies that are built dynamically.   
110  
111Gary   
112  
113This posting is provided "AS IS", with no warranties, and confers no   
114rights. Enjoyed ASP.NET? http://www.asp.net</structlayout(layoutkind.sequential)></structlayout(layoutkind.sequential)></dllimport("user32.dll",></dllimport("user32.dll",></dllimport("user32.dll",>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus