views:

167

answers:

3

I need to programmatically send a Page Down key press to a console application and I'm lost. I tried Console.SetIn whith a StreamReader over a MemoryStream, then wrote 34 (Page Down in ConsoleKey enum) and nothing happenned.

+1  A: 
SendKeys.Send("{PGDN}");
phoebus
Make sure you are either fully qualifying it or added a reference to System.Windows.Forms. It does work in console apps.
phoebus
A: 

Can you use StreamWriter and System.Windows.Forms.Keys.PageDown ?

lod3n
A: 

You can use the "WriteConsoleInput" function, which is Windows API, to write to the console directly. Alternatively, "SendKeys", "keybd_event" or the "SendInput API" should all work, but will require that the console is in the foreground.

http://msdn.microsoft.com/en-us/library/ms687403%28VS.85%29.aspx - WriteConsoleInput

msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx - keybd_event

msdn.microsoft.com/en-us/library/ms646310(VS.85).aspx - SendInput

The latter two are equivalent to using "SendKeys", which was derived from VB5s SendKeys API. Unfortunately the latter two methods require that the target Window (Console Window or otherwise) be the Active Window, or have thier Input Queue attached to the Input Queue of the Active Window.

WriteConsoleInput would be the most appropriate way, avoiding the need to have the COnsole Window visible or active. Also, writing to standard in/out on a Console window is not the same as sending Input via the Console API or other input-injection API (such as SendInput/keybd_event or the VB/.NET SendKeys Wrapper).

Windows Console Applications are not the same as a Unix or DOS 6.x (or earlier) "Console" application in that it is not a true pipes-based implementation, in many cases mucking about with STDIN/STDOUT will have no effect, and you may find that some frameworks integrate with the Console API instead of STDIN/STDOUT. One could almost say STDIN is dead, especially considering the "object pipes" present in Windows PowerShell.

Alternatively you can CreateProcess API (or System.Diagnostics.Process) and provide your own stdin/stdout/stderr streams and bypass the Windows Console for these streams, but again unless the target application explicitly works via STDIN/STDOUT/STDERR it may not help.

Shaun Wilson