为了账号安全,请及时绑定邮箱和手机立即绑定

控制台应用程序中的全局热键

控制台应用程序中的全局热键

C#
心有法竹 2019-10-18 13:47:01
有谁知道如何在控制台应用程序中使用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);    }}
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 1108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信