Кто-нибудь знает, как использовать вызовы RegisterHotKey/UnregisterHotKey API в консольном приложении? Я предполагаю, что настройка/удаление горячей клавиши одинакова, но как мне получить обратный вызов при нажатии клавиши?
Каждый пример, который я вижу для Winforms, и использует protected override void WndProc(ref Message m){...}
, который недоступен мне.
update: то, что у меня есть, ниже, но событие никогда не попадает. Я думал, что это может произойти из-за того, что когда вы загружаете 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);
}
}