c# - 在控制台中检测 Ctrl + S

标签 c# input console modifiers

我正在开发一个控制台应用程序,我需要在其中检测多个热键,例如 Ctrl+NCtrl+ OCtrl+S。下面是我用来识别这些热键的部分代码:

ConsoleKeyInfo input = Console.ReadKey(true);

if (input.Modifiers == ConsoleModifiers.Control)
{
    if (input.Key == ConsoleKey.N)
    {
        // ...
    }
    else if (input.Key == ConsoleKey.O)
    {
        // ...
    }
    else if (input.Key == ConsoleKey.S)
    {
        //...
    }
}

上面的代码对于 Ctrl+NCtrl+O 没有任何问题。但是,我无法让它为 Ctrl+S 工作。在做了一些快速测试后,我发现按 Ctrl+S 甚至没有做任何事情(这意味着程序仍在等待用户键入内容)。

此问题仅在 Ctrl+S 时出现。对任何其他修饰符(例如 Shift)和键(NO 等)使用一些简单的 if 语句) 工作正常。

为什么会这样? Ctrl+S 组合是否有特殊含义?有可能使这项工作吗?如果是,如何?

最佳答案

protected override  bool  ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (msg.Msg == 256)
    {
        //if the user pressed control + s
        if (keyData == (Keys.Control | Keys.S))
        {

        }
        //if the user pressed control + o 
        else if (keyData == (Keys.Control | Keys.O))
        {

        }
        //if the user pressed control + n 
        else if (keyData == (Keys.Control | Keys.N))
        {

        }
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

关于c# - 在控制台中检测 Ctrl + S,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39476157/

相关文章:

c# - X509Certificate2.验证行为

c# - 如何在C#中以编程方式控制VLC?

javascript - 谷歌浏览器 js 控制台显示细红线,没有打印错误文本

串行端口 WIN32 Comm API 的 C#/.NET 包装器

c# - 我如何知道word文档中特定文本的字体大小(例如)?

javascript - 如果名称包含 [ 和 ],如何选择具有给定名称的所有输入?

html - 如何使用背景图像向此输入重新添加背景颜色?

C++ - 正在释放的指针未分配错误

c++ - 如何发送信息

c# - 在 Microsoft Sync Framework 中获取更改摘要