控制台应用程序中的全局热键
有谁知道如何在控制台应用程序中使用 RegisterHotKey/UnregisterHotKey API 调用?我假设设置/删除热键是相同的,但是当按下该键时如何回调?
我看到的每个示例都是针对 Winforms 的,并使用 protected override void WndProc(ref Message m){...}
,这对我来说不可用。
update: what I have is below, but the event is never hit. I thought it could be because when you load ConsoleShell it does block further execution, but even if I put
SetupHotkey
into a different thread nothing happens. Any thoughts?class Program
{
static void Main(string[] args)
{
new Hud().Init(args);
}
}
class Hud
{
int keyHookId;
public void Init(string[] args)
{
SetupHotkey();
InitPowershell(args);
Cleanup();
}
private void Cleanup()
{
HotKeyManager.UnregisterHotKey(keyHookId);
}
private void SetupHotkey()
{
keyHookId = HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Control);
HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
}
void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
//never executed
System.IO.File.WriteAllText("c:\\keyPressed.txt", "Hotkey pressed");
}
private static void InitPowershell(string[] args)
{
var config = RunspaceConfiguration.Create();
ConsoleShell.Start(config, "", "", args);
}
}
Does anyone know how to use the RegisterHotKey/UnregisterHotKey API calls in a console application? I assume that setting up/removing the hotkey is the same, but how do I get the call back when the key was pressed?
Every example I see is for Winforms, and uses protected override void WndProc(ref Message m){...}
, which isn't available to me.
update: what I have is below, but the event is never hit. I thought it could be because when you load ConsoleShell it does block further execution, but even if I put
SetupHotkey
into a different thread nothing happens. Any thoughts?class Program
{
static void Main(string[] args)
{
new Hud().Init(args);
}
}
class Hud
{
int keyHookId;
public void Init(string[] args)
{
SetupHotkey();
InitPowershell(args);
Cleanup();
}
private void Cleanup()
{
HotKeyManager.UnregisterHotKey(keyHookId);
}
private void SetupHotkey()
{
keyHookId = HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Control);
HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
}
void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
//never executed
System.IO.File.WriteAllText("c:\\keyPressed.txt", "Hotkey pressed");
}
private static void InitPowershell(string[] args)
{
var config = RunspaceConfiguration.Create();
ConsoleShell.Start(config, "", "", args);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以做的是在控制台应用程序中创建一个隐藏窗口,用于处理热键通知并引发事件。
代码 HERE 演示了原理。 这里是一篇关于在控制台应用程序中处理消息的文章,您可以使用它应该能够增强 HotKeyManager 以在控制台应用程序中运行。
HotKeyManager 的以下更新创建了一个后台线程,该线程运行消息循环并处理 Windows 消息。
以下是从控制台应用程序使用 HotKeyManager 的示例
What you can do is Create a hidden window in your Console application which is used to handle the hotkey notification and raise an event.
The code HERE demonstrates the principal. HERE is an article on handling messages in a Console application, using this you should be able to enhance HotKeyManager to run in a Console Application.
The following update to the HotKeyManager creates a background thread which runs the message loop and handles the windows messages.
Here is an example of using HotKeyManager from a Console application
我只是想提供一个替代解决方案。
我正在为使用此脚本的人回答一个问题,我认为这可能会帮助其他无法设置全局密钥挂钩的人。
编辑:不要忘记添加对
System.Windows.Forms
的引用您可以通过选择
Project
I just wanted to offer an alternative solution.
I was answering a question for someone who was using this script and I figured this might help someone else who has trouble setting up a global key hook.
Edit: Don't forget to add a reference to
System.Windows.Forms
You can do this by selecting
Project
????Add Reference
and checkingSystem.Windows.Forms
我根据 Chris 的回答提出了一个使用 WPF 而不是 WinForms 的解决方案:
要使用它,您需要添加对PresentationFramework.dll 和 WindowsBase.dll 的引用。
I came up with a solution based on Chris' answer that uses WPF instead of WinForms:
To use this, you need to add references to PresentationFramework.dll and WindowsBase.dll.
更改了 HotKeyManager 类
Class HotKeyEventArgs:
和类:HotKeyEventArgs
Changed the HotKeyManager class
Class HotKeyEventArgs:
And class: HotKeyEventArgs