无窗口.NET应用程序中的全局热键 [英] Global hotkeys in windowless .NET app

查看:81
本文介绍了无窗口.NET应用程序中的全局热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读过有关此问题的类似问题,包括 使用C#设置全局热键的最佳方法? .我还研究了NuGet软件包全局热键,它似乎还处于起步阶段.

I've read similar questions around this issue, including Best way to tackle global hotkey processing in C#? and Set global hotkeys using C#. I've also investigated a NuGet package Global Hotkeys which appears to be in its infancy.

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

The problem here is, most of them seem to be designed for Winforms, or could probably run in WPF. The P/Invoke they're using seems to require a window handle. I'm thinking of having a windoless application here, i.e running without a main form or window, except when a certain key combo is pressed, so there might not actually be a handle.

那么,将P/Invoke的Window句柄传递为厚脸皮的0会导致它不寻找用于处理按键的窗口吗?还是我最好的选择是使用不可见的无法聚焦的窗口?

So, would passing a cheeky 0 as the Window handle for the P/Invoke cause it to not look for a window to process the keypress on? Or is my best bet here to use an invisible unfocusable Window?

要添加一些背景信息,我正在制作一个无窗口应用程序,该应用程序可与TTS一起使用,以提供控制反馈,我的目标受众是盲人和视力障碍的用户.有时,必须输入一些内容,因此我希望能够在必要时启动表单,但是在大多数情况下,我希望没有窗口使事情变得混乱.

To add a little context, I'm making a windowless app to be used with TTS providing the feedback for control, my target audience here are blind and visually impaired users. Occasionally, things will have to be entered, so I want to be able to launch forms when necessary, but for the most part I'd like there to be no window cluttering things up.

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

Some sample code (I can't verify if this would work properly yet).

[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();
}

感谢您可能提供的帮助.

Thanks for any help you may be able to provide.

推荐答案

使用RegisterHotkey()是检测热键按下的样板.在此不再赘述,您可以轻松地从函数名称中搜索示例代码.

Using RegisterHotkey() is boilerplate to detect a hotkey press. I won't repeat it here, you can easily google sample code from the function name.

但是它确实需要一个窗口,尚无解决方法.它只是不必是一个可见的窗口.到目前为止,创建不可见窗口的最简单方法是像在任何Winforms应用程序中一样使用Form类,并设置ShowInTaskbar = False,WindowState = Minimized,FormBorderStyle = FixedToolWindow.在您的OnLoad()方法重写中调用RegisterHotkey(),您将需要Handle属性.轻松自在.

It does however require a window, no workaround for that. It just doesn't have to be a visible window. By far the simplest way to create an invisible window is to use a Form class, like you do in any Winforms application, and set ShowInTaskbar = False, WindowState = Minimized, FormBorderStyle = FixedToolWindow. Call RegisterHotkey() in your OnLoad() method override, you'll need the Handle property. Easy peasy.

这篇关于无窗口.NET应用程序中的全局热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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