有谁知道如何在控制台应用程序中使用RegisterHotKey / UnregisterHotKey API调用?我假设设置/删除热键是相同的,但是当按下键时如何回拨电话?
我看到的每个示例都是针对Winforms的,并使用protected override void WndProc(ref Message m){...},对我来说不可用。
更新:下面是我所拥有的,但从未成功过。我认为可能是因为加载ConsoleShell时,它确实阻止了进一步的执行,但是即使我放入SetupHotkey了另一个线程也没有任何反应。有什么想法吗?
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);
}
}
相关分类