Tecla de atalho global no aplicativo de console
Alguém sabe como usar as chamadas da API RegisterHotKey / UnregisterHotKey em um aplicativo de console? Presumo que a configuração / remoção da tecla de atalho seja a mesma, mas como recebo a chamada de volta quando a tecla foi pressionada?
Cada exemplo que vejo é para WinForms e usaprotected override void WndProc(ref Message m){...}
, que não está disponível para mim.
SetupHotkey
em um segmento diferente, nada acontece. Alguma ideia?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);
}
}