无窗口.NET应用程序中的全局热键

时间:2013-06-16 13:06:02

标签: .net keyboard-shortcuts

我已经阅读了有关此问题的类似问题,包括Best way to tackle global hotkey processing in C#?Set global hotkeys using C#。我还研究了一个似乎还处于起步阶段的NuGet包Global Hotkeys

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

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

为了添加一些上下文,我正在制作一个无窗口的应用程序与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();
}

感谢您提供的任何帮助。

3 个答案:

答案 0 :(得分:5)

使用RegisterHotkey()是检测热键按下的样板。我不会在这里重复,您可以轻松地从函数名称中搜索示例代码。

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

答案 1 :(得分:3)

您也可以使用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);
    }

}

答案 2 :(得分:0)

只需创建像this

这样的全局kbhook