我正在开发一个控制台应用程序,我需要在其中检测多个热键,例如 Ctrl+N、Ctrl+ O 和 Ctrl+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+N 和 Ctrl+O 没有任何问题。但是,我无法让它为 Ctrl+S 工作。在做了一些快速测试后,我发现按 Ctrl+S 甚至没有做任何事情(这意味着程序仍在等待用户键入内容)。
此问题仅在 Ctrl+S 时出现。对任何其他修饰符(例如 Shift)和键(N、O 等)使用一些简单的 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/