user-0 提问: 谁能帮我找一个代码例子或者写一个可以按ESC键随时退出主程序的键盘钩子？C语言

回复:
user: user-0
content: #include <stdio.h> #include <conio.h> #include <windows.h> #include <process.h>  void max(a,b) {     if(a<b)     printf("%d",b);     else     printf("%d",a);  }   void thread_proc(void* arg) {         Sleep(100);         int k;         if(kbhit()) 	{ 		k=getch(); 		if(27==k)  		 	}               //_endthread();         //_endthreadex();      }  void main(void) {     int a,b;     max(a,b); //static unsigned pid; //  _beginthreadex(NULL,0,(unsigned (__stdcall*)(void*))thread_proc,0,0,0);     _beginthread(thread_proc,0,0);      getch();     } 前辈，我如果想把这个程序实现用那个线程控制主函数内各跳转函数返回主界面应该怎么改一下？这是我找的代码，然后自己研究改一下

user: user-1
content: 引用 5 楼 jacksonfan 的回复:[quote=引用 3 楼 baijiaheizhiganmao 的回复:]  求科普什么叫键盘钩子，难道就是监听用户按键的程序吗？  钩子的核心不是回调函数？[/quote] 不知道诶，大概“钩子”这个词有二义性吧。在一般的文献中叫什么？

user: user-2
content: 本来按Ctrl+Break就可以随时退出Console程序。

user: user-3
content: 引用 3 楼 baijiaheizhiganmao 的回复:求科普什么叫键盘钩子，难道就是监听用户按键的程序吗？  钩子的核心不是回调函数？

user: user-2
content: #include <conio.h> #include <windows.h> int main() {     int k;      while (1) {         if (kbhit()) {             k=getch();             if (0==k || 0xE0==k) k=k<<8|getch();             if (27==k) break;//按Esc键退出             cprintf("\r\n%04X\r\n",k);         }         Sleep(200);         cprintf(".");     }     return 0; }

user: user-2
content: 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

user: user-0
content: 代码我尝试了好多，查的网上的资料也好杂，所以我想在这里求前辈们给个例子什么的

user: user-1
content: 引用 1 楼 jacksonfan 的回复: 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; }  求科普什么叫键盘钩子，难道就是监听用户按键的程序吗？

user: user-4
content: 监听键盘事件，然后根据按下键做相应的处理（如你说的退出程序）

user: user-3
content: 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; }