1

I'm trying to bring a Windows console application to the foreground using

SetForegroundWindow(handle);
ShowWindow(handle, (int)ShowWindowCommands.Show);

It's all working fine but after bringing the window to the foreground I want the user to confirm by pressing the enter key. But unfortunately the focus is not actually in the application but on it. So when pressing enter nothing will happen.

How can I set the focus in the console application to be able to hit enter straight away?

Here's my code:

SetForegroundWindow(handle);
ShowWindow(handle, (int)ShowWindowCommands.Show);

Console.WriteLine("Please press ENTER to confirm...");

if (Console.ReadKey().Key == ConsoleKey.Enter)
{
    // do stuff
}

Please don't just ask why...

theknut
  • 2,533
  • 5
  • 26
  • 41
  • [This thread](http://stackoverflow.com/q/688337/21567) is about using the Win32 API, but I recommand you read it anyway. – Christian.K Aug 27 '13 at 10:29
  • 1
    How did you retrieve the `handle` of the `Console` window? – King King Aug 27 '13 at 10:30
  • Am using Jon Skeets example code from http://stackoverflow.com/a/213582/809277 – theknut Aug 27 '13 at 11:13
  • @theknut Did you actually solve the problem? With .NET Version 4.6.1 I can't reproduce the the focus issue. Does it depend on what application is focused before the console windows gets the focus? - For me, `ENTER` just works fine... – ventiseis Aug 09 '16 at 20:33

0 Answers0