全局键盘钩e.Handled = true;不起作用 [英] Global keyboard hook e.Handled = true; does not working

查看:101
本文介绍了全局键盘钩e.Handled = true;不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的C#全局低级键盘挂钩 [ ^ ]



这是我用于钩子键的东西。它有一个方法e.Handled防止一个键完成一个特定的任务,如显示F1键的帮助窗口。



这里是我用过的代码..



A Simple C# Global Low Level Keyboard Hook[^]

This is what I use for hook keys. And it has a method e.Handled to prevent a key completing a particular task such as showing the help window for F1 key.

and here is the code I have used..

globalKeyboardHook myhook = new globalKeyboardHook();
            
            myhook.HookedKeys.Add(Keys.F1);
            myhook.HookedKeys.Add(Keys.F2);
            myhook.HookedKeys.Add(Keys.F3);
            myhook.HookedKeys.Add(Keys.F4);
            myhook.HookedKeys.Add(Keys.F5);
            myhook.HookedKeys.Add(Keys.F6);
            myhook.HookedKeys.Add(Keys.F7);
            myhook.HookedKeys.Add(Keys.F8);
            myhook.HookedKeys.Add(Keys.F9);
            myhook.HookedKeys.Add(Keys.F10);
            myhook.HookedKeys.Add(Keys.F11);
            myhook.HookedKeys.Add(Keys.F12);
            
            myhook.KeyDown += new KeyEventHandler(myhook_KeyDown);
            myhook.KeyUp +=new KeyEventHandler(myhook_KeyUp);







void myhook_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.F1)
            {
                if (checkBox1.Checked)
                {
                    e.Handled = true;
                }
            }
         }





如果选中checkBox1并且用户按F1键全部我想要从Windows中阻止使用F1键。 (防止显示帮助窗口)



这是第一次使用。如果检查了checkBox1并且用户按下F1键,则该程序将阻止显示帮助窗口。

但是如果我取消选中checkBox1并再次检查它,则该程序不会执行任何操作。弹出帮助窗口。



我的代码出了什么问题?

还有其他方法可以实现我的目标吗? />


请不要使用先进的方法。

我是新手C#



If checkBox1 is checked and user press the F1 key all I wanted is preventing F1 key using from windows. (Prevent showing the help window)

It works for the first time. If checkBox1 is checked and user pressed the F1 key this program would prevent showing the help window.
But if I uncheck the checkBox1 and check it again, this program doesnot do anything. The help window would popup.

Whats the wrong with my code ?
Is there some other way to accomplish what I want ?

And please no advanced methods please.
Im a newbie to C#

推荐答案

开始编辑#2 ...



这是一个可能的解决方案,确实使用了您使用的StormySpike 2007 CP文章中的代码:



我修改了Form1设计时视图,如下所示:



1.设置Dock属性lstLog列表框为无。



2.将lstLog控件重新调整为表格顶部的开放空间。



3.在表格顶部添加了一个CheckBox,checkBox1,文本:Key Handle On / Off。



以下是该文章的Form1.cs文件的修改代码:
begin edit #2 ...

Here's a possible solution that does use the code from the 2007 CP article by StormySpike you used:

I modified the Form1 design-time view like this:

1. set the Dock Property of the lstLog ListBox to "None."

2. re-sized the lstLog Control to open-up room at the top of the Form.

3. added a CheckBox, checkBox1, with the Text: "Key Handle On/Off" at the top of the Form.

Here's the modified code for that articles's Form1.cs file:
using System;
using System.Windows.Forms;
using Utilities;

// code from: CP article "A Simple C# Global Low Level Keyboard Hook"
// by StormySpike, 31 May 2007
// http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook

// modified Jan. 2014 by bw

namespace key_preview
{
    public partial class Form1 : Form
    {
        globalKeyboardHook myHook = new globalKeyboardHook();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            checkBox1.Checked = true;

            myHook.HookedKeys.Add(Keys.F1);
            myHook.HookedKeys.Add(Keys.F2);
            myHook.HookedKeys.Add(Keys.F3);
            myHook.HookedKeys.Add(Keys.F4);
            myHook.HookedKeys.Add(Keys.F5);
            myHook.HookedKeys.Add(Keys.F6);
            myHook.HookedKeys.Add(Keys.F7);
            myHook.HookedKeys.Add(Keys.F8);
            myHook.HookedKeys.Add(Keys.F9);
            myHook.HookedKeys.Add(Keys.F10);
            myHook.HookedKeys.Add(Keys.F11);
            myHook.HookedKeys.Add(Keys.F12);

            myHook.KeyDown += myHook_KeyDown;
            myHook.KeyUp += myHook_KeyUp;
        }

        // note no reporting done here if KeyUp is handled
        private void myHook_KeyUp(object sender, KeyEventArgs e)
        {
            if (checkBox1.Checked && myHook.HookedKeys.Contains(e.KeyCode))
            {
                e.Handled = true;
                return;
            }

            lstLog.Items.Add("Up\t" + e.KeyCode.ToString());
        }

        private void myHook_KeyDown(object sender, KeyEventArgs e)
        {
            if (checkBox1.Checked && myHook.HookedKeys.Contains(e.KeyCode))
            {
                switch(e.KeyCode)
                {
                    case Keys.F1:
                        lstLog.Items.Add("handled F1\t" + e.KeyCode.ToString());
                        break;

                    // .... add case statements for all other F# keys here

                    case Keys.F12:
                        lstLog.Items.Add("handled F12\t" + e.KeyCode.ToString());
                        break;
                }

                e.Handled = true;
                return;
            }

            lstLog.Items.Add("Key Down passed through\t" + e.KeyCode.ToString());
        }
    }
}

我在使用FrameWork 4.5编译的VS 2013中测试了此代码。在Win 8/64检查和取消检查,CheckBox几次并测试F1和F12键没有问题。



结束编辑#2。 ..



开始编辑#1 ...



我使用了George Mamaladze最新的HookManager源代码+演示应用程序[ ^ ],在VS 2013中打开它,让VS将其转换为使用FrameWork 4.5。



fyi:下载并运行George最新的 demo (v.2)他的钩子管理器:在Win 8中始终失败,但他的原始演示工作正常。我将向George报告错误详细信息。



我通过更改一种方法验证我可以修改他的KeyDown行为,仅停止处理F1键。 ,通过所有其他按键:

I tested this code in VS 2013 compiling with FrameWork 4.5. No problems in Win 8/64 checking, and un-checking, the CheckBox several times and testing the F1 and F12 keys.

end edit #2 ...

begin edit #1 ...

I took George Mamaladze's latest HookManager source + demo app [^], opened it in VS 2013, let VS convert it to use FrameWork 4.5.

fyi: downloading and running George's latest demo (v. 2) of his hook manager: consistently fails in Win 8, but his original demo works fine. I'll report the error details to George.

I verified that I could modify his KeyDown behavior, by altering one method, to stop handling the F1 key-down only, passing through all other key-downs:

private void HookManager_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.F1)
    {
        e.Handled = true;
        return;
    }

    textBoxLog.AppendText(string.Format("KeyDown - {0}\n", e.KeyCode));
    textBoxLog.ScrollToCaret();
}

我确认多次检查和取消检查CheckBox的'KeyDown导致F1键被处理,或按预期传递给Windows,工作正常。



此解决方案表明安装和卸载钩子EventHandlers以使钩子按预期工作是



结束编辑#2 ...



开始编辑#1 ...



当未选中KeyDown拦截CheckBox时,George的代码删除 EventHandler,并且安装/重新安装检查时它。



假设:您需要为ComboBox的CheckedChanged事件编写EventHandler,并卸载或安装基于EventHandler的事件是否检查CheckBox。



您是否可以修改您最初使用的代码(不是George的解决方案)是我没有看过的。



结束编辑#1 ...



我建议您在CP上切换到使用George Mamaladze的Global Hook代码,该代码最近已更新比你现在使用的代码:[ ^ ]。



使用C#进行全局钩子调用的本质是复杂的,涉及使用WinApi和Platform Invoke工具。您需要的功能可能并不简单,如果您是C#的初学者,这可能是个问题。

I verified that checking and un-checking the CheckBox for 'KeyDown several times resulted in the F1 key being handled, or passed on to Windows as expected, worked properly.

This solution suggests that it is not necessary to install and un-install the hook EventHandlers for the hook to work as expected.

end edit #2 ...

begin edit #1 ...

George's code removes the EventHandler when the KeyDown intercept CheckBox is unchecked, and installs/re-installs it when it is checked.

Hypothesis: you need to write an EventHandler for the CheckedChanged Event of the ComboBox, and un-install or install the EventHandler based on whether the CheckBox is checked, or not.

Whether you can modify the code you were working with originally (not George's solution) is something I haven't looked at.

end edit #1 ...

I suggest you switch to using George Mamaladze's Global Hook code here on CP which has been updated more recently than the code you are using now: [^].

The nature of using global hook calls from C# is inherently complex, involving use of WinApi and Platform Invoke facilities. The functionality you require may not be simple, and if you are a beginner in C# this may be a problem.


https://www.nuget.org/packages/MouseKeyboardActivityMonitor/ [ ^ ]



这个有效..:)
https://www.nuget.org/packages/MouseKeyboardActivityMonitor/[^]

This one works.. :)


这篇关于全局键盘钩e.Handled = true;不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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