控制台程序注册的热键 Ctrl+Alt+Z 居然被QQ抢走了

KaKaKaKa 2012-05-22 11:49:07
见上贴:
http://topic.csdn.net/u/20120512/21/ebd09e43-d990-4633-b65e-45e50703f197.html

按照上贴方法,为控制台程序窗口注册了热键 Ctrl+Alt+Z

RegisterHotKey(
NULL,
1,
MOD_CONTROL| MOD_ALT,
'Z'))

而且确实注册成功了

但当我登录QQ后 发现这个热键竟然被QQ抢走了

一般的Windows窗口程序貌似不会,但控制台窗口的热键会

怎么肥事?

后来发现,即使不上QQ,自己的控制台程序先开一个运行,然后再开一个,居然热键注册成功了
而且是后面运行的程序抢走了前面的,等后面的程序退出后,热键居然还还给了前一个运行的程序

奇怪啊

我想想普通Windows窗体程序一样注册热键

即热键被注册过了 就返回失败 提示已经注册了
在控制台中可以么?
...全文
508 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbzjzsb123 2012-06-08
  • 打赏
  • 举报
回复

看起来有点象钩子
钩子就是链在一起的,起作用的是最后的那个
  • 打赏
  • 举报
回复
我的测试结果和你不一样,我的Win7 32位机器。
后面的不会抢走前面注册成功的热键!
KaKaKaKa 2012-06-02
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 的回复:]

后一个覆盖了前一个,在计算机中这很正常啊。
[/Quote]
晕 这是Hotkey,不是Hook
proorck6 2012-06-02
  • 打赏
  • 举报
回复
后一个覆盖了前一个,在计算机中这很正常啊。
  • 打赏
  • 举报
回复
过会儿研究下 嘿嘿
KaKaKaKa 2012-06-01
  • 打赏
  • 举报
回复
天呐
问题没解决 帖子却沉了
KaKaKaKa 2012-05-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]

不同的控制台程序
确实可以使用RegisterHotKey(
NULL,
1,
MOD_CONTROL| MOD_ALT,
'Z'))
对同一个热键进行注册,但只有最后运行的那个进程起作用。感觉在触发热键后,windows从热键链表中匹配第一个进程进行处理。

如果同一个进程需要运行多次,而且要求热键都要起作用,使用钩子吧。查一下SetWindowsHookEx的……
[/Quote]

就是因为它往后传递了,我就是需要它不要往后传递呀
我的程序如果发现热键被其它程序注册了 那自己就注册失败
如果自己注册成功了,那以后的程序请求注册这个热键就失败,包括自己的程序的第二个运行实例
SONG_CA 2012-05-27
  • 打赏
  • 举报
回复
不同的控制台程序
确实可以使用RegisterHotKey(
NULL,
1,
MOD_CONTROL| MOD_ALT,
'Z'))
对同一个热键进行注册,但只有最后运行的那个进程起作用。感觉在触发热键后,windows从热键链表中匹配第一个进程进行处理。

如果同一个进程需要运行多次,而且要求热键都要起作用,使用钩子吧。查一下SetWindowsHookEx的用法,注意在键盘钩子函数里处理完热键后,要记得传给钩子链表中的下一个。

QQ应该是使用的钩子技术来完成的热键功能。最担心的是它处理完CTRL+ALT+Z之后,有没有往后传递,要是没有往后传递,你还是换一个热键吧

KaKaKaKa 2012-05-26
  • 打赏
  • 举报
回复
没有人知道怎么回事么?
被经理问了好几天了 问题解决了没有
可是确实没什么资料
KaKaKaKa 2012-05-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

在google搜GetConsoleWindow
[/Quote]

GetConsoleWindow这个函数就是获取当前控制台程序的窗口句柄啊
我之前试过RegisterHotKey函数的第一个参数指定为GetConsoleWindow获取到的HWND的
但是这样热键注册不了

上一个帖子在这:
http://topic.csdn.net/u/20120512/21/ebd09e43-d990-4633-b65e-45e50703f197.html

还是没有找到什么原因啊
赵4老师 2012-05-26
  • 打赏
  • 举报
回复
在google搜GetConsoleWindow
KaKaKaKa 2012-05-25
  • 打赏
  • 举报
回复
顶 希望有朋友帮看看
KaKaKaKa 2012-05-24
  • 打赏
  • 举报
回复
程序连续运行两次
KaKaKaKa 2012-05-23
  • 打赏
  • 举报
回复
程序如下:

#include "stdio.h"
#include "windows.h"

void main()
{
MSG msg = {0};

if (RegisterHotKey(NULL, 1, MOD_CONTROL | MOD_ALT, 'Z'))
{
printf("注册热键 [Ctrl + Alt + Z] 成功!\n");
}
else
{
printf("注册热键 [Ctrl + Alt + Z] 失败!\n");
}

//....

while (GetMessage(&msg, NULL, 0, 0))
{
if(WM_HOTKEY == msg.message)
{
if(1 == msg.wParam && (MOD_CONTROL | MOD_ALT) == LOWORD(msg.lParam) && 'Z' == HIWORD(msg.lParam))
{
printf("触发热键了!\n");
}
}
}

//...

getchar();
}

KaKaKaKa 2012-05-23
  • 打赏
  • 举报
回复
普通的窗体程序都没问题的

唯一不同的就是控制台程序设置热键时RegisterHotKey函数第一个参数为NULL

可是为什么会被后来运行的程序抢走已经注册的热键呢

msdn上没说呀
赵4老师 2012-05-23
  • 打赏
  • 举报
回复
RegisterHotKey
The RegisterHotKey function defines a system-wide hot key.

BOOL RegisterHotKey(
HWND hWnd, // window to receive hot-key notification
int id, // identifier of hot key
UINT fsModifiers, // key-modifier flags
UINT vk // virtual-key code
);

Parameters
hWnd
Handle to the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.
id
Specifies the identifier of the hot key. No other hot key in the calling thread should have the same identifier. An application must specify a value in the range 0x0000 through 0xBFFF. A shared dynamic-link library (DLL) must specify a value in the range 0xC000 through 0xFFFF (the range returned by theGlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier.
fsModifiers
Specifies keys that must be pressed in combination with the key specified by the nVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values. Value Meaning
MOD_ALT Either alt key must be held down.
MOD_CONTROL Either ctrl key must be held down.
MOD_SHIFT Either shift key must be held down.
MOD_WIN Either WINDOWS key was held down. These keys are labeled with the Microsoft Windows logo.


vk
Specifies the virtual-key code of the hot key.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks
When a key is pressed, the system looks for a match against all hot keys. Upon finding a match, the system posts the WM_HOTKEY message to the message queue of the thread that registered the hot key. This message is posted to the beginning of the queue so it is removed by the next iteration of the message loop.

This function cannot associate a hot key with a window created by another thread.

RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key.

If the window identified by the hWnd parameter already registered a hot key with the same identifier as that specified by the id parameter, the new values for the fsModifiers and vk parameters replace the previously specified values for these parameters.

Windows CE: Windows CE versions 2.0 and later support an additional flag, called MOD_KEYUP, for the fsModifiers parameter. If you set the MOD_KEYUP flag, the window will be sent a WM_HOTKEY message on a key up event as well as on a key down event.

RegisterHotKey can be used to register hot keys across threads.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winuser.h.
Import Library: Use user32.lib.

See Also
Keyboard Input Overview, Keyboard Input Functions,GlobalAddAtom, UnregisterHotKey, WM_HOTKEY


KaKaKaKa 2012-05-23
  • 打赏
  • 举报
回复
是啊 msdn上的例子就是这样的
。。。。
ouyh12345 2012-05-23
  • 打赏
  • 举报
回复
看起来有点象钩子
钩子就是链在一起的,起作用的是最后的那个
KaKaKaKa 2012-05-23
  • 打赏
  • 举报
回复
自己顶

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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