在managed code中是不能处理系统热键的,因此,您所希望实现的功能仍然要调用unmanaged code实现,也就是说仍然需要使用以前VC 6.0中的键盘钩子。在微软新闻组的.NET General区中有一篇微软工程师解答的关于如何在C#中使用键盘的钩子的例子代码,我将其转贴过来以供参考:/***************** 转贴开始 **********************/Subject: RE: System-wide Keyhandler
From: "Lion Shi" <[email protected]>
Sent: 5/23/2002 6:29:51 AM
Hello Jim,You can use system hook to hook keyboard messages and then determine if the
key are pressed. This is a sample code demonstrating how to setup a system
hook in C#:public class Win32Hook
{ [DllImport("kernel32")]
public static extern int GetCurrentThreadId(); [DllImport( "user32",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(
HookType idHook,
HOOKPROC lpfn,
int hmod,
int dwThreadId); public enum HookType
{
WH_KEYBOARD = 2
}
public delegate int HOOKPROC(int nCode, int wParam, int lParam); public void SetHook()
{
// set the keyboard hook
SetWindowsHookEx(HookType.WH_KEYBOARD,
new HOOKPROC(this.MyKeyboardProc),
0,
GetCurrentThreadId());
} public int MyKeyboardProc(int nCode, int wParam, int lParam)
{
//Perform your process
return 0;
}
}And then you can install the hook procedure by the following code:Win32Hook hook = new Win32Hook();
hook.SetHook();In the MyKeyboardProc function, you can determine which key is pressed by
the wParam parameter. You also can identify the key is pressed or released
by the lParam parameter. Then you can set a flag when the Ctrl is pressed
and set another flag when the Alt is pressed. When both flag are set, stop
the program.For more information about these two parameters, please see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/hooks
_8k6b.aspYou can find all Virtual-Key Code Definitions from the link below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmeother/ke
ycnt_4fqw.asp I hope it is helpful.Best regards, Lion Shi, MCSE, MCSD
Microsoft Support EngineerThis posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
From: "Jim Baker" <[email protected]>
Subject: System-wide Keyhandler
Date: Mon, 20 May 2002 13:05:30 -0400
Lines: 7
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Message-ID: <#iGEpECACHA.1680@tkmsftngp04>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 65.208.95.3
Path: cpmsftngxa08!tkmsftngp01!tkmsftngp04
Xref: cpmsftngxa08 microsoft.public.dotnet.general:51404
X-Tomcat-NG: microsoft.public.dotnet.generalI'm looking to have a program grab a key combination (whether it has focus
or not) and run an event based on that. It's basically a system tray
application that I want to toggle hiding and showing a window. Any ideas?Jim/***************** 转贴结束 **********************/
From: "Lion Shi" <[email protected]>
Sent: 5/23/2002 6:29:51 AM
Hello Jim,You can use system hook to hook keyboard messages and then determine if the
key are pressed. This is a sample code demonstrating how to setup a system
hook in C#:public class Win32Hook
{ [DllImport("kernel32")]
public static extern int GetCurrentThreadId(); [DllImport( "user32",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(
HookType idHook,
HOOKPROC lpfn,
int hmod,
int dwThreadId); public enum HookType
{
WH_KEYBOARD = 2
}
public delegate int HOOKPROC(int nCode, int wParam, int lParam); public void SetHook()
{
// set the keyboard hook
SetWindowsHookEx(HookType.WH_KEYBOARD,
new HOOKPROC(this.MyKeyboardProc),
0,
GetCurrentThreadId());
} public int MyKeyboardProc(int nCode, int wParam, int lParam)
{
//Perform your process
return 0;
}
}And then you can install the hook procedure by the following code:Win32Hook hook = new Win32Hook();
hook.SetHook();In the MyKeyboardProc function, you can determine which key is pressed by
the wParam parameter. You also can identify the key is pressed or released
by the lParam parameter. Then you can set a flag when the Ctrl is pressed
and set another flag when the Alt is pressed. When both flag are set, stop
the program.For more information about these two parameters, please see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/hooks
_8k6b.aspYou can find all Virtual-Key Code Definitions from the link below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmeother/ke
ycnt_4fqw.asp I hope it is helpful.Best regards, Lion Shi, MCSE, MCSD
Microsoft Support EngineerThis posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
From: "Jim Baker" <[email protected]>
Subject: System-wide Keyhandler
Date: Mon, 20 May 2002 13:05:30 -0400
Lines: 7
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Message-ID: <#iGEpECACHA.1680@tkmsftngp04>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 65.208.95.3
Path: cpmsftngxa08!tkmsftngp01!tkmsftngp04
Xref: cpmsftngxa08 microsoft.public.dotnet.general:51404
X-Tomcat-NG: microsoft.public.dotnet.generalI'm looking to have a program grab a key combination (whether it has focus
or not) and run an event based on that. It's basically a system tray
application that I want to toggle hiding and showing a window. Any ideas?Jim/***************** 转贴结束 **********************/
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货