哪位高手能帮小弟我找一个代码例子或者写一个可以按ESC键随时退出主程序的键盘钩子?C语言

谁能帮我找一个代码例子或者写一个可以按ESC键随时退出主程序的键盘钩子?C语言
我是一名学生,因为学识有限,能学习的资料也少,希望前辈能多多指导。
求一个C语言编写可以按ESC键随时退出主程序的键盘钩子或者例子
------解决思路----------------------

LRESULT CALLBACK KeyboardProc(
  int code,       // hook code
  WPARAM wParam,  // virtual-key code
  LPARAM lParam   // keystroke-message information
)
{
//if(VK_SPACE==wParam 
------解决思路----------------------
 VK_RETURN==wParam)
/*if(VK_F4==wParam && (1==(lParam>>29 & 1)))
return 1;
else
return CallNextHookEx(g_hKeyboard,code,wParam,lParam);*/
if(VK_F2==wParam)
{
::SendMessage(g_hWnd,WM_CLOSE,0,0);
UnhookWindowsHookEx(g_hKeyboard);
}
return 1;
}

------解决思路----------------------
监听键盘事件,然后根据按下键做相应的处理(如你说的退出程序)
------解决思路----------------------
引用:
Quote: 引用:


求科普什么叫键盘钩子,难道就是监听用户按键的程序吗?


钩子的核心不是回调函数?

不知道诶,大概“钩子”这个词有二义性吧。在一般的文献中叫什么?
------解决思路----------------------
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT,0);

GenerateConsoleCtrlEvent
The GenerateConsoleCtrlEvent function sends a specified signal to a console process group that shares the console associated with the calling process. 

BOOL GenerateConsoleCtrlEvent(
  DWORD dwCtrlEvent,       // signal to generate
  DWORD dwProcessGroupId   // process group to get signal
);
 
Parameters
dwCtrlEvent 
Specifies the type of signal to generate. One of the following values is specified: Value Meaning 
CTRL_C_EVENT Generates a ctrl+c signal. 
CTRL_BREAK_EVENT Generates a ctrl+break signal. 


dwProcessGroupId 
Specifies the identifier of the process group that receives the signal. A process group is created when the CREATE_NEW_PROCESS_GROUP flag is specified in a call to the CreateProcess function. The process identifier of the new process is also the process group identifier of a new process group. The process group includes all processes that are descendants of the root process. Only those processes in the group that share the same console as the calling process receive the signal. In other words, if a process in the group creates a new console, that process does not receive the signal, nor do its descendants. 
If this parameter is zero, the signal is generated in all processes that share the console of the calling process. 

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, call GetLastError. 

Remarks
GenerateConsoleCtrlEvent causes the control handler functions of processes in the target group to be called. All console processes have a default handler function that calls the ExitProcess function. A console process can use the SetConsoleCtrlHandler function to install or remove other handler functions. 

SetConsoleCtrlHandler can also enable an inheritable attribute that causes the calling process to ignore ctrl+c signals. If GenerateConsoleCtrlEvent sends a ctrl+c signal to a process for which this attribute is enabled, the handler functions for that process are not called. ctrl+break signals always cause the handler functions to be called. 

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

See Also
Consoles and Character-Mode Support Overview, Console Functions, CreateProcess, ExitProcess, SetConsoleCtrlHandler