I'm developing a console application in which I need to detect several hotkeys such as Ctrl+N, Ctrl+O and Ctrl+S. Here's a part of the code that I use to recognize these hotkeys:
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)
{
//...
}
}
The code above works without any issues for Ctrl+N and Ctrl+O. But, I can't get it to work for Ctrl+S. After doing some quick testing, I found out that pressing Ctrl+S doesn't even do anything (meaning that the program is still waiting for the user to type something).
This issue only occurs with Ctrl+S. Using some simple if
statements for any other modifier (Shift for example) and key (N, O, etc) works fine.
Why does this happen? Is there a special meaning to the Ctrl+S combination? Is it possible to make this work? If yes, how?
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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)