2

我已经阅读过有关此问题的类似问题,包括在 C# 中解决全局热键处理的最佳方法?使用 C# 设置全局热键。我还调查了一个 NuGet 包Global Hotkeys,它似乎处于起步阶段。

这里的问题是,它们中的大多数似乎是为 Winforms 设计的,或者可能在 WPF 中运行。他们使用的 P/Invoke 似乎需要一个窗口句柄。我正在考虑在这里有一个无窗口应用程序,即在没有主窗体或窗口的情况下运行,除非按下某个键组合,因此实际上可能没有句柄。

那么,传递一个厚颜无耻的 0 作为 P/Invoke 的窗口句柄会导致它不寻找一个窗口来处理按键吗?还是我最好的选择是使用不可见的不可聚焦的窗口?

为了添加一点上下文,我正在制作一个无窗口应用程序,与 TTS 一起使用,提供控制反馈,我的目标受众是盲人和视障用户。有时,必须输入一些东西,所以我希望能够在必要时启动表单,但在大多数情况下,我希望没有窗口把东西弄得乱七八糟。

一些示例代码(我无法验证这是否可以正常工作)。

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    ScreenReader.sapiEnable(true);
    ScreenReader.SayString("Launching application...");
    // bind hotkeys here
    Application.Run();
}

// called when the right keyboard shortcut is pressed
static void ExitApp()
{
    ScreenReader.SayString("Exiting program");
    Application.Exit();
}

感谢您提供的任何帮助。

4

3 回答 3

7

使用 RegisterHotkey() 是检测热键按下的样板。这里不再赘述,你可以很容易地从函数名中google示例代码。

但是,它确实需要一个窗口,没有解决方法。它只是不必是一个可见的窗口。到目前为止,创建不可见窗口的最简单方法是使用 Form 类,就像在任何 Winforms 应用程序中一样,并设置 ShowInTaskbar = False、WindowState = Minimized、FormBorderStyle = FixedToolWindow。在 OnLoad() 方法覆盖中调用 RegisterHotkey(),您将需要 Handle 属性。十分简单。

于 2013-06-16T13:17:59.600 回答
4

您也可以使用NativeWindow类。在下面的示例中,我还使用了ApplicationContext,这是启动“无窗口”应用程序的好方法:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyContext());
    }
}

public class MyContext : ApplicationContext
{

    private MyHotKey hotkeys = null;

    public MyContext()
    {
        hotkeys = new MyHotKey();
        hotkeys.HotkeyPressed += new MyHotKey.HotkeyDelegate(hotkeys_HotkeyPressed);
    }

    private void hotkeys_HotkeyPressed(int ID)
    {
        switch (ID)
        {
            case 1001:
                MessageBox.Show("Alt-1");
                break;

            case 1002:
                MessageBox.Show("Alt-2");
                break;

            case 1003: // Alt-Q
                Application.Exit();
                break;
            default:
                break;
        }
    }

}

public class MyHotKey : NativeWindow
{

    private const int WM_HOTKEY = 0x0312;
    private const int WM_DESTROY = 0x0002;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private List<Int32> IDs = new List<int>();

    public delegate void HotkeyDelegate(int ID);
    public event HotkeyDelegate HotkeyPressed;

    public MyHotKey()
    {
        this.CreateHandle(new CreateParams());
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

        RegisterCombo(1001, 1, (int)Keys.D1);
        RegisterCombo(1002, 1, (int)Keys.D2);
        RegisterCombo(1003, 1, (int)Keys.Q);
    }

    private void RegisterCombo(Int32 ID, int fsModifiers, int vlc)
    {
        if (RegisterHotKey(this.Handle, ID, fsModifiers, vlc))
        {
            IDs.Add(ID);
        }
    }

    private void Application_ApplicationExit(object sender, EventArgs e)
    {
        this.DestroyHandle();
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_HOTKEY:
                if (HotkeyPressed != null)
                {
                    HotkeyPressed(m.WParam.ToInt32());
                }
                break;

            case WM_DESTROY: // fires when "Application.Exit();" is called
                foreach (int ID in IDs)
                {
                    UnregisterHotKey(this.Handle, ID);
                }
                break;
        }
        base.WndProc(ref m);
    }

}
于 2013-06-16T17:47:14.720 回答
-1

这样创建全局 kbhook

于 2013-06-16T13:25:14.380 回答