在C#控制台应用程序,暂停键冻结显示输出。我可以禁用? [英] In a C# Console app, the Pause key freezes the display output. Can I disable that?

查看:638
本文介绍了在C#控制台应用程序,暂停键冻结显示输出。我可以禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个C#控制台应用程序,按暂停键冻结显示输出。我可以禁用?

In a C# Console app, pressing the Pause key freezes the display output. Can I disable that?

我希望像处理Ctrl + C键的 Console.CancelKeyPress 事件的处理程序输入。

I was hoping for a handler like the Console.CancelKeyPress event that handles Ctrl+C input.

推荐答案

在一段时间的请求出现从一个控制台程序挂接键每一次。该标准的事件,如 CTRL_C_EVENT CTRL_CLOSE_EVENT 不包括暂停事件。我试着这样做使用后台线程,但我似乎没有管理。然而,有一个不那么难。解决方法:使用一个额外的过程

Every once in a while a request comes up for hooking keys from a console program. The standard events like CTRL_C_EVENT and CTRL_CLOSE_EVENT do not include the Pause-event. I've tried doing so using a background thread, but I don't seem to manage. However, there's a not-so-hard workaround: use an extra process.

下载此易于使用的全球键盘钩子对C#。然后,当您打开项目,可采取下列代码,并把它放在Form1.cs的:

Download this easy-to-use global keyboard hook for C#. Then, when you open that project, take the following code and put it in the Form1.cs:

public partial class Form1 : Form {

    globalKeyboardHook globalKeyboardHook = new globalKeyboardHook();

    public Form1() {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {
        globalKeyboardHook.HookedKeys.Add(Keys.Pause);
        globalKeyboardHook.KeyDown += new KeyEventHandler(globalKeyboardHook_KeyDown);
        globalKeyboardHook.KeyUp += new KeyEventHandler(globalKeyboardHook_KeyUp);
    }

    void globalKeyboardHook_KeyUp(object sender, KeyEventArgs e)
    {
        // remove this when you want to run invisible
        lstLog.Items.Add("Up\t" + e.KeyCode.ToString());

        // this prevents the key from bubbling up in other programs
        e.Handled = true;
    }

    void globalKeyboardHook_KeyDown(object sender, KeyEventArgs e)
    {
        // remove this when you want to run without visible window
        lstLog.Items.Add("Down\t" + e.KeyCode.ToString());

        // this prevents the key from bubbling up in other programs
        e.Handled = true;
    }
}



然后,剩下的就简单:

Then, the rest becomes trivial:


  1. 更改程序启动上述程序,比正常运行

  2. 尝试键入暂停键

  3. 这将通过其他程序捕获

  4. 您的程序将不会被暂停。

  1. Change your program to start the above program and than run normally
  2. Try to type the pause key
  3. It'll be caught by the other program
  4. Your program will NOT be paused.

我试过上面我和它的工作原理

I tried the above myself and it works.

PS:我的意思不是说没有可能的方式直接从一个控制台程序。有很可能,我只是没有找到它,而上述全球keyhook库没有从控制台应用程序中工作。

PS: I don't mean that there isn't a possible way straight from a Console program. There may very well be, I just didn't find it, and the above global keyhook library didn't work from within a Console application.

这篇关于在C#控制台应用程序,暂停键冻结显示输出。我可以禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆