关于c语言如何监控全局键盘按键

qq_32308751 2018-07-13 02:05:22
如题,在下是一名初学c语言的辣鸡,现在在写一些小游戏练手。
然后在写代码的时候发现有一个想写的功能似乎触到我的知识盲区了,故来此提问,望dalao解答。

现在问题是:我有两个函数,一个是开始播放音乐一个是结束播放音乐的,然后我希望它能够在游戏运行的同时,只要我按下某个特定的按键就能够触发,我希望它就是可以不管是在游戏封面或者游戏中或者游戏的结算画面,只要按下按键就能够触发。

我自己写的代码目前只能在游戏中触发,但我希望能够通过一个函数就把这个问题解决了,还请各位大佬多多指教...
...全文
684 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-07-13
  • 打赏
  • 举报
回复
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


杀意已决 2018-07-13
  • 打赏
  • 举报
回复
搜一下键盘钩子
老马何以识途 2018-07-13
  • 打赏
  • 举报
回复
在其他時期,沒有捕捉鍵盤信號?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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