1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include "main.h" #include <windows.h> //--------------------全局变量区--------------------------- #define APPNAME "CINZY_HOOK" LPCTSTR lpszAppName = APPNAME ; HINSTANCE ghInstance ; HWND ghDlg; //--------------------WndProc函数--------------------------- LRESULT __stdcall WndProc (HWND hDlg,UINT iMsg,WPARAM wParam,LPARAM lParam) { LRESULT lResult; BOOL fCallDefProc = FALSE ; switch ( iMsg ) { case WM_INITDIALOG: EnableWindow(GetDlgItem(hDlg,IDC_EDIT1),false); break; case WM_COMMAND: switch ( LOWORD(wParam) ) { case IDC_BUTTON1: ShellExecute(NULL, "open", "http://cinzy.com/#DemoKbdHook", NULL, NULL, SW_SHOWNORMAL); break; } break; case WM_CLOSE: PostMessage ( hDlg, WM_DESTROY, 0, 0 ) ; break; case WM_DESTROY: PostQuitMessage( 0 ) ; break ; default: fCallDefProc = TRUE ; break ; } if ( fCallDefProc ) { lResult = DefWindowProc ( hDlg, iMsg, wParam, lParam ) ; } return lResult ; } DWORD g_main_tid = 0; HHOOK g_kb_hook = 0; bool CALLBACK con_handler (DWORD) { PostThreadMessage (g_main_tid, WM_QUIT, 0, 0); return TRUE; }; LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l) { char buf[256]; static char buff[65535]=""; PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l; const char *info = NULL; if (w == WM_KEYDOWN||w == WM_SYSKEYDOWN) { sprintf (buf,"%C",p->vkCode); if(strlen(buff)<65533) strcat(buff,buf); SendDlgItemMessage(ghDlg,IDC_EDIT1,WM_SETTEXT,0,(LPARAM)buff); } return CallNextHookEx (g_kb_hook, code, w, l); }; int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow) { WNDCLASS wc ; ghInstance=hInstance; wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wc.lpfnWndProc = (WNDPROC)WndProc ; wc.cbClsExtra = 0 ; wc.cbWndExtra = DLGWINDOWEXTRA ; wc.hInstance = hInstance ; wc.hIcon = LoadIcon(ghInstance,MAKEINTRESOURCE(IDI_ICON2)); wc.hCursor = LoadCursor(NULL, IDC_ARROW) ; wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1) ; wc.lpszMenuName = NULL ; wc.lpszClassName = lpszAppName ; if ( IS_WIN95 ){}else if ( !RegisterClass(&wc)){return FALSE;} ghDlg = CreateDialog(hInstance, (LPCTSTR)MAIN_FRAME, 0, (DLGPROC)WndProc); g_main_tid = GetCurrentThreadId (); SetConsoleCtrlHandler ((PHANDLER_ROUTINE)&con_handler, TRUE); g_kb_hook = SetWindowsHookEx ( WH_KEYBOARD_LL, (HOOKPROC)&kb_proc, GetModuleHandle (NULL), 0); if (g_kb_hook == NULL) { fprintf (stderr, "SetWindowsHookEx failed with error %d\n", ::GetLastError ()); return 0; }; MSG msg; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); }; UnhookWindowsHookEx (g_kb_hook); return 0; }
|