如何截获系统热键

dashkoc 2002-06-29 12:19:29
比如,当程序在后台的时候,如何在用户按某个热键相应?

我试着找了一个ActiveX控件,但好像不能用在C#中。

请指教,谢谢
...全文
40 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
lkal4587 2002-09-06
  • 打赏
  • 举报
回复
如何截获系统热键

http://www.csdn.net/expert/topic/913/913577.xml?temp=.8866388

sdsxc 2002-09-06
  • 打赏
  • 举报
回复
up
junqiang 2002-09-05
  • 打赏
  • 举报
回复
我:junqiang@21cn.com
guobraveboy 2002-09-05
  • 打赏
  • 举报
回复
给我寄一份:guo_zhj@163.com ,多谢!

jinhu 2002-08-01
  • 打赏
  • 举报
回复
的确是空的,不知道是否可以麻烦邮寄到:
majinhu@163.com,多谢!
dashkoc 2002-07-31
  • 打赏
  • 举报
回复
不对啊,邮箱是空的,寄到我的信箱吧dashkoc@hotmail.com 谢谢
lkal4587 2002-07-30
  • 打赏
  • 举报
回复
.NET源代码放送在Sina的邮箱里

用户名 : lk4587_3

密码 : 2740614

下载成功,请留言.
dashkoc 2002-07-26
  • 打赏
  • 举报
回复
能不能提供完整的C#使用键盘钩子的代码……谢谢
Heineken 2002-07-24
  • 打赏
  • 举报
回复
C#可以在程序范围内用键盘钩子,我式过没问题,全局范围内最好用写好的VC的dll程序了。
cnnjyeziyin 2002-07-24
  • 打赏
  • 举报
回复
关注!
bearbaba 2002-07-24
  • 打赏
  • 举报
回复
搞什么啊解决不料问题就逃避啊!
bellflow 2002-07-24
  • 打赏
  • 举报
回复
谁知道?有一次玩电脑,打字时当按下字母键B和M时,会自动将屏幕最小化,那是怎么回事?谁知道?那位大虾能告诉我?先谢谢了`~
bellflow 2002-07-24
  • 打赏
  • 举报
回复
谁知道?有一次玩电脑,打字时当按下字母键B和M时,会自动将屏幕最小化,那是怎么回事?谁知道?那位大虾能告诉我?先谢谢了`~
DBXP 2002-07-24
  • 打赏
  • 举报
回复
up!Thanks!
dashkoc 2002-07-06
  • 打赏
  • 举报
回复
谢谢,我试了一下,没有成功。键盘事件还是没响应。
能不能详细解释一下,谢谢。
acptvc 2002-07-02
  • 打赏
  • 举报
回复
To dashkoc (Dash) :

在managed code中是不能处理系统热键的,因此,您所希望实现的功能仍然要调用unmanaged code实现,也就是说仍然需要使用以前VC 6.0中的键盘钩子。

在微软新闻组的.NET General区中有一篇微软工程师解答的关于如何在C#中使用键盘的钩子的例子代码,我将其转贴过来以供参考:

/***************** 转贴开始 **********************/

Subject: RE: System-wide Keyhandler
From: "Lion Shi" <lionshi@online.microsoft.com>
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.asp

You 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 Engineer

This 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" <jim.baker@milesREMOVETHISmedia.com>
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.general

I'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



/***************** 转贴结束 **********************/



- 微软全球技术中心 acptvc

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
tatalulu 2002-07-02
  • 打赏
  • 举报
回复
ultra
waki 2002-07-02
  • 打赏
  • 举报
回复
在菜单中,常用的系统热键可以很方便地加入。在属性中选择即可,不需任何代码。
maldini3328 2002-07-01
  • 打赏
  • 举报
回复
关注````````
lkal4587 2002-07-01
  • 打赏
  • 举报
回复
关注~~~~~~~
加载更多回复(6)

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧