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

Cra*_*ett 2 .net keyboard-shortcuts

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

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

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

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

感谢您提供的任何帮助.

Han*_*ant 6

使用RegisterHotkey()是检测热键按下的样板.我不会在这里重复,你可以从函数名称轻松谷歌示例代码.

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


Idl*_*ind 5

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

}
Run Code Online (Sandbox Code Playgroud)