win32程式測試鍵盤鉤子

來源:互聯網
上載者:User
// Test_Hook.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include <Windows.h>#include <stdio.h>#include <stdarg.h>#include <ctype.h>#include <WinError.h>// Some global variablesHINSTANCE g_Instance;   // Handler of current instanceHHOOK     g_Hook;       // Handler of hookBOOL SetHook();BOOL UnSetHook();// The hook function (will be called by other processes)static LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);////////////////////////////////////////////////////////////////////////////odprintf -- debug functionvoid __cdecl odprintf(const char* fmt, ...){char buf[4096], *p = buf;va_list args;va_start(args, fmt);p += vsnprintf_s(p, sizeof(buf), _TRUNCATE, fmt, args);va_end(args);while ( p > buf  &&  isspace(p[-1]) )*--p = '\0';*p++ = '\r';*p++ = '\n';*p   = '\0';OutputDebugStringA(buf);//output as ANSI string//OutputDebugString}LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam){KBDLLHOOKSTRUCT* pkh = (KBDLLHOOKSTRUCT*)lParam;//HC_ACTION: wParam 和lParam參數包含了鍵盤按鍵訊息if (nCode == HC_ACTION) {if (::GetAsyncKeyState('1') & 0x8000){odprintf("111111111111111111");}//判斷函數調用時指定虛擬鍵的狀態//BOOL bCtrlKey =::GetAsyncKeyState(VK_CONTROL) & 0x8000;BOOL bCtrlKey =::GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);if ((pkh->vkCode == VK_ESCAPE && bCtrlKey) ||// Ctrl+Esc 開始菜單(pkh->vkCode == VK_TAB    && pkh->flags & LLKHF_ALTDOWN) ||// Alt+TAB 螢幕切換菜單(pkh->vkCode == VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN) ||// Alt+Esc 螢幕切換 (pkh->vkCode == VK_LWIN   || pkh->vkCode==VK_RWIN)// 左右Windows鍵){odprintf("ok...i'am come here!");//這裡會出現兩次, 因為return 1; }}// Call next hook in chainreturn ::CallNextHookEx(g_Hook, nCode, wParam, lParam);}BOOL SetHook(){if (g_Instance || g_Hook)// Already hooked!return TRUE;g_Instance = (HINSTANCE)::GetModuleHandle(NULL);g_Hook     = ::SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyboardProc, g_Instance, 0);if (!g_Hook){odprintf("SetHook error, error code=%ld", ::GetLastError());//error codereturn FALSE;}return TRUE;// Hook has been created correctly}BOOL UnSetHook(){if (g_Hook) {// Check if hook handler is valid::UnhookWindowsHookEx(g_Hook);// Unhook is done hereg_Hook = NULL;// Remove hook handler to avoid to use it again}return TRUE;// Hook has been removed}int _tmain(int argc, _TCHAR* argv[]){odprintf("Test of KeyboardProc, starting...");if (!SetHook())return -1;MSG msg;while(::GetMessage(&msg, NULL, 0, 0) > 0){TranslateMessage(&msg);DispatchMessage(&msg);}//clean:UnSetHook();return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.