键盘挂钩alt-tab引起奇怪的行为? [英] Keyboard hooking alt-tab causing strange behavior?

查看:94
本文介绍了键盘挂钩alt-tab引起奇怪的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁止我的应用程序的用户使用alt-tab.我正在尝试通过使用低级键盘钩来解决此问题.

I want to disallow the user of my application from using alt-tab. I'm trying to solve this by using a low level keyboard hook.

设置它的工作原理非常好,当我按下组合键等时会触发回调过程.发生的怪异事情是我可以alt-tab退出应用程序,但是完全禁用了alt-tabbing.我用鼠标切换应用程序,然后尝试按alt键切换,但没有任何反应.我切换回我的应用程序并再次alt-tab,它切换了应用程序,但是只有一步.当退出我的应用程序时,alt-tab不再起作用.

Setting it up works perfectly, the callback procedure is triggered when I press the key combination etc. The weird thing that happens is that I can alt-tab out of the application but then alt-tabbing is disabled, completely. I switch applications with the mouse and try alt-tabbing but nothing happens. I switch back to my application and alt-tab again and it switches the application, but only one step. When out of my application the alt-tab isn't working anymore.

我尝试了不同的场景,首先我认为这与VS2010中的调试器有关,但没有,在没有调试器的情况下运行它会得到相同的结果.

I've tried different scenarios, first I thought it had something to do with the debugger in VS2010 but no, running it with out the debugger gives the same results.

我是否完全误解了该挂钩过程,是否只想捕捉应用程序不集中时发生的事情?

Have I completely misunderstood this hook procedure, is it meant to only catch stuff happening when the application isn't in focus?

osman.hpp:

osman.hpp :

static HHOOK m_hhook;

static LRESULT CALLBACK lowLevelKeyboardProc( int key, WPARAM wParam, LPARAM lParam );

osman.cpp:

osman.cpp :

HHOOK OSMan::m_hhook;

/*
* pseudo init code
*/
void OSMan::init()
{
     m_hHook = SetWindowsHookEx( WH_KEYBOARD_LL, (HOOKPROC)lowLevelKeyboardProc, 0, 0 );
}

LRESULT CALLBACK OSMan::lowLevelKeyboardProc( int key, WPARAM wParam, LPARAM lParam )
{
    KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;

    switch (key)
    {
    case HC_ACTION:
        {
           if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
                 return 1;

        }

    default:
         break;
    }
   return CallNextHookEx( m_hHook, key, wParam, lParam);
}

  • 添加了代码.

推荐答案

您正在安装系统范围的钩子,这就是为什么您在整个系统上禁用Alt-TAB的原因.

You are installing a System Wide hook, that is why you have disabled the use of Alt-TAB on the whole system.

您不能使用 WH_KEYBOARD_LL ,而必须使用 WH_KEYBOARD 挂钩,并使其具有针对性.

You can't use a WH_KEYBOARD_LL you must use a WH_KEYBOARD hook, and make it process specific.

如果您特定挂钩过程,则 SetWindowsHookEx 的参数将发生变化.

The params of the SetWindowsHookEx will change if you make your hook process specific.

以下是这些参数的概述:

Here is an overview of the params :

dwThreadId [输入] 类型: DWORD

与挂钩过程相关联的线程的标识符.

The identifier of the thread with which the hook procedure is to be associated.

如果此参数为零,则挂钩过程与与调用线程在同一桌面上运行的所有现有线程相关联.

PS:回复评论:

仅在进程遇到一个适当的事件之后才安装该挂钩.钩住键盘消息时,必须至少发送一条键盘消息,然后再安装钩子.

The hook only is installed after the process got one appropriate event. While hooking onto keyboard messages, you will have to send at least one keyboard message before the hook is installed.

变通

也许您想通过使用 ShowWindow 函数解决此问题?这是文档: http://www.pinvoke.net/default.aspx/user32.显示窗口

Maybe you want to work around this issue by using the ShowWindow function ? Here is the doc : http://www.pinvoke.net/default.aspx/user32.showwindow

cf: http://www.codeproject.com/Articles/4610/Three-Ways-to-Inject-Your-Code-into-Another-Proces

cf http://msdn.microsoft.com/zh-CN/library/windows/desktop/ms644990(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms644959(v=vs.85).aspx#wh_keyboardhook

这篇关于键盘挂钩alt-tab引起奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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