I'm building a little hotkey trainer for StarCraft2 (it's a C# Console application). But I'm struggling to capture Ctrl+ combinations, because certain Ctrl-combinations generate special characters.
For example, when pressing Ctrl+O, Console.ReadKey() won't capture the 'o' character, but rather a special starry '☼' character. How do I prevent this and ensure Ctrl just acts as a modifier key and nothing else?
PS: I know about handling Ctrl+C specifically, so I'm more concerned about the other keys.
Here is a simple code example. Try capturing and outputting Ctrl+O
private static void Main()
{
var pressed = Console.ReadKey();
Console.WriteLine(pressed.KeyChar);
Console.ReadLine();
}
With Console.ReadKey
you have everything you need using Key
instead of KeyChar
:
ConsoleKeyInfo keyInfo = Console.ReadKey();
Console.WriteLine(keyInfo.Key);
Console.WriteLine(keyInfo.Modifier);
As explained on MSDN , the KeyChar
is the representation of the key combination (key + modifiers), and Key
is the key the user pressed.
Additionally, if you don't want the KeyChar
to appear on the screen, you can intercept the key using Console.ReadKey(true)
.
You can get the information from the ConsoleKeyInfo
object returned by Console.ReadKey
For Ctrl + O, the Ctrl key will be the Modifier
property and the O key will be the Key
property.
If you are writing a "trainer" , reading StdIn is not going to cut it for you. This will only receive input when the Console App has focus. To read all keyboard input, ie when Start Craft 2 has focus, you need global hooking.
See this article for more info.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.