在C全局钩子键盘 [英] global keyboard hooks in c

查看:221
本文介绍了在C全局钩子键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个全局键盘钩子禁止任务switching.When我用Google搜索,我发现了一大堆codeS在C#中,CPP(和Delphi),但我需要大约挂钩的一些基本概念(是的如果最好的例子是在C)。所以,请建议资源链接,可以帮助我了解C角度来看的东西。

i want to write a global keyboard hook to disallow task switching.When i googled i found a whole lot of codes in c#,cpp (and delphi), but i need some basic concepts about hooking (would be the best if examples are in C).So, kindly suggest the resources,links that can help me understand the thing in C's perspective.

PS:我发现一个很好的工作例如(工作在WinXP和旧版本),但是当我试图编译code它给了我:

PS: I found one good working example(works on winXP and older versions),but when i tried compiling the code it gives me:

我试图搜索中的所有标题的IDC_常数(默认的附带MinGW的GCC安装和开发商提供的那些),但没有运气...如果任何一个可以编译code和使其运行,请帮助我。我没有上传源本身作为这里有几头文件的依赖性,并在这种情况下,我不得不在这里发表的所有code。

And i tried searching the "IDC_" constants in all the headers(default ones that come with MinGW gcc installation and the ones provided by developer),but no luck...If any one can compile the code and make it run please help me.I have not uploaded the source itself here as there are a few header file dependencies and in that case i'd have to post all the code here.

winXP的是目标环境,但会更好,如果我得到它也运行Win7的。

winXP is the target environment but would be better if i get it to run Win7 also.

推荐答案

我就出在这里肢体假设你是在Windows和你想捕捉全球按键。要做到这一点的一种方法是使用LowLevelHooks。请看下面的例子:

I will go out on a limb here assuming you are on Windows and you want to capture global keystrokes. A way to do this is to use LowLevelHooks. Look at the following example:

在code某处定义这个回调函数:

Define this callback function somewhere in your code:

//The function that implements the key logging functionality
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
{
   char pressedKey;
   // Declare a pointer to the KBDLLHOOKSTRUCTdsad
   KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
   switch( wParam )
   {
       case WM_KEYUP: // When the key has been pressed and released
       {
          //get the key code
          pressedKey = (char)pKeyBoard->vkCode;
       }
       break;
       default:
           return CallNextHookEx( NULL, nCode, wParam, lParam );
       break;
   }

    //do something with the pressed key here
      ....

   //according to winapi all functions which implement a hook must return by calling next hook
   return CallNextHookEx( NULL, nCode, wParam, lParam);
}

然后你的主函数中的某个地方,你会设置挂钩,像这样:

And then somewhere inside your main function you would set the hook like so:

 //Retrieve the applications instance
 HINSTANCE instance = GetModuleHandle(NULL);
 //Set a global Windows Hook to capture keystrokes using the function declared above
 HHOOK test1 = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, instance,0);

有关钩子的一般信息可以发现<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms644960%28v=vs.85%29.aspx#installing_releasing\">here.
您也可以捕获其他全球性事件具有完全相同的方式只能按照<给定方向href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx\">SetWindowsHooksEX文档。

这篇关于在C全局钩子键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆