0

I am doing an console application that is working with other apps. While the code works with the other apps the console does not have the focus so the console does not record the keyboard ... I would like to be able to set a "watcher" in background on the console app to pause (with space) and stop (with escape) while the code does what it have to do.

This does not work for console apps because it is not a Window form so the override of WndProc does not find any method to override

Grez
  • 59
  • 8
  • you should use Keyboard Hook , check this link [keyboard hook](https://stackoverflow.com/questions/46013287/c-sharp-global-keyboard-hook-that-opens-a-form-from-a-console-applicatio) – Marwen Jaffel Jan 10 '20 at 15:55
  • you need to make your console to work like a windows app (to register a window somehow) using the `Application.Run();` or by [registering](https://learn.microsoft.com/en-us/windows/win32/winmsg/using-window-classes) the `WndProc` manually. – oleksa Jan 10 '20 at 16:13

1 Answers1

1

You can use a keyboard hook, check this question for information.

If you don't like the idea of that. Use unity, i know it sounds stupid but Unity has a keyhook handler.

eg:

Input.GetKeyDown("space");

Its very useful.

Calum
  • 11
  • 1
  • 1
    Adding 6 GBs of dependencies to use a keyboard hook isn't good advice. –  Jan 10 '20 at 16:12
  • Adding unity is indeed a weird answer but adding a keyboard hook via winapi `SetWindowsHookEx` is correct, so you have my upvote. – Charles Jan 10 '20 at 16:23
  • 1
    Was talking just about the unityengine.dll not the entire unity runtime :) @Josh – Calum Jan 16 '20 at 17:53