0

I am trying to make a simple menu.

My question is simple: Is there a way to detect if key is pressed directly from the keyboard? In such way, that even if the user has started typing something, if he presses "~" it would be detected and some action will follow?

I know that is not possible in the console. I could only use Console.ReadKey() but that accepts the whole character, and if its not '`' it is discarded. Possible fix could be to add that key to the string later, but that would require the first key to be "~" which is annoying.

That is why I need something to detect the keys as they are pressed. I hope you can help. Thanks.

RapGeneral
  • 13
  • 6
  • I have no idea what you are trying to do, and why `Console.ReadKey()` doesn't work for you - please be more clear. – gilmishal Jul 17 '18 at 18:26
  • 1
    You could use a keyboard listener to get every single input: Here's an example https://gist.github.com/Ciantic/471698 – Andre Motta Jul 17 '18 at 18:27
  • try this - https://stackoverflow.com/questions/8898182/how-to-handle-key-press-event-in-console-application – Ctznkane525 Jul 17 '18 at 18:27
  • I need to detect if a key is pressed no matter if the user has actually pressed enter to enter it in the console. For example as I am typing this string here, if I press "~" i want to detect it. – RapGeneral Jul 17 '18 at 18:27
  • Alternatively, if `Console.ReadKey()` discards your character, why not just use whatever variant of `Console.Write()` you use to write it back? – Chronicle Jul 17 '18 at 18:31
  • You could redirect the output of the Console temporarily using the `SetOut` method. I've been able to get it to work, but I hope there's a better solution. – Yuriy Faktorovich Jul 17 '18 at 18:31
  • [here's](https://stackoverflow.com/questions/32272886/capture-keyboard-shortcuts-and-forward) a good way to grab all keyhook on the computer but it's maybe more than you need. – Franck Jul 17 '18 at 19:36

0 Answers0