简体   繁体   English

关键侦听器不触发(MouseKeyHook)

[英]Key listener not firing (MouseKeyHook)

I'm starting out in C#, coded a lot in Java but having some trouble here. 我从C#开始,用Java编写了很多代码,但是这里有些麻烦。 I'm trying to learn how to use MouseKeyHook for an application I'm developing. 我正在尝试学习如何对正在开发的应用程序使用MouseKeyHook I cannot get the actual listener to fire off an event. 我无法让实际的监听器触发事件​​。 Here's my listener code: 这是我的监听器代码:

using System;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;

namespace ChromaHeatmap
{
    class keyListener
    {
        private IKeyboardMouseEvents m_GlobalHook;

        public void Subscribe()
        {
            // Note: for the application hook, use the Hook.AppEvents() instead
            m_GlobalHook = Hook.GlobalEvents();
            m_GlobalHook.KeyPress += GlobalHookKeyPress;
        }

        private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
        {
            Console.WriteLine("blah");
        }

        public void Unsubscribe()
        {
            m_GlobalHook.KeyPress -= GlobalHookKeyPress;

            //It is recommened to dispose it
            m_GlobalHook.Dispose();
        }
    }
}

And here's the part of my application code where I attempt to do something with the listener. 这是我的应用程序代码中的部分,我在其中尝试使用侦听器进行操作。 If anyone can let me know what the best way is to loop here and wait for events, I'd appreciate it. 如果有人可以告诉我最好的方法是在这里循环并等待事件,那我将不胜感激。

    //Listen for key presses
    keyListener heyListen = new keyListener();
    heyListen.Subscribe();

    while(true)
    {
    }
    while(true) {}

This is a hold-and-catch-fire statement, the thread will burn 100% core and cannot execute the hook callback. 这是一个hold-and-catch-fire语句,该线程将消耗100%的内核,并且无法执行钩子回调。 You'll notice that the machine goes dead for 5 seconds when you press a key, the operating system is waiting for an opportunity to invoke the callback. 您会注意到,当您按下一个键时,计算机将死机5秒钟,操作系统正在等待调用回调的机会。 But it won't wait forever and unceremoniously will destroy the hook so you regain control over the machine. 但是它不会永远等待,并且毫不客气地破坏了钩子,因此您可以重新控制计算机。 Also the kind of mishap that will occur when you try to debug your event handler. 当您尝试调试事件处理程序时,也会发生这种情况。

Windows needs an opportunity to safely call the hook callback. Windows需要机会安全地调用挂钩回调。 That requires your program to be "idle", not executing any code. 这要求您的程序“空闲”,不执行任何代码。 The technical term for this is "pumping the message loop", your program must wait for a notification from the operating system that something interesting happened. 这个技术术语是“泵送消息循环”,您的程序必须等待操作系统通知发生了有趣的事情。

A very simple way is to use the Winforms project template as-is, you'll also get a window. 一种非常简单的方法是按原样使用Winforms项目模板,您还将获得一个窗口。 Note how the Main() method in the project makes the call that you need instead of the while() loop. 注意项目中的Main()方法如何进行所需的调用,而不是while()循环。 You must call Application.Run() . 您必须调用Application.Run()

Check this post for code that avoids displaying a window. 这篇文章中查看避免显示窗口的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM