Automating input

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there any way of getting VS2005 to 'type' the content of a file into the
currently open CS file?

I am trying to capture the VS2005 screen: I'd like to be able to record the
code (already stored in a file) being typed; doing this manually is fraught
with problems and I wondered whether I bring in the code from a file,
character by character.
 
Is there any way of getting VS2005 to 'type' the content of a file into the
currently open CS file?

I am trying to capture the VS2005 screen: I'd like to be able to record the
code (already stored in a file) being typed; doing this manually is fraught
with problems and I wondered whether I bring in the code from a file,
character by character.

Well, you could do it with another process calling SendKeys.Send/
SendWait.
It's crude, but it appears to work.

Jon
 
Easy enough to simulate - just use following as console exe and feed
it the file(s) as input args:

using System;
using System.IO;
using System.Windows.Forms;
using System.Threading;
static class Program {
static void Main(string[] args) {
Console.WriteLine("Select your app!");
Thread.Sleep(5000);
Console.WriteLine("Typing...");
foreach (string arg in args) {
using (StreamReader reader = File.OpenText(arg)) {
char[] escapeBuffer = { '{', '?', '}' };
while (!reader.EndOfStream) {
char c = (char)reader.Read();
string keys;
switch (c)
{
case '+': case '^':
case '%': case '~':
case '{': case '}':
case '[': case ']':
case '(': case ')':
escapeBuffer[1] = c;
keys = new string(escapeBuffer);
break;
case '\t':
keys = "{TAB}";
break;
case '\n':
keys = "{ENTER}";
break;
case '\r':
if (reader.Peek() == '\n') {
reader.Read();
}
keys = "{ENTER}";
break;
default:
keys = new string(c,1);
break;
}
try { SendKeys.SendWait(keys); }
catch (Exception ex) {
Console.Error.WriteLine(ex);
}
Thread.Sleep(100);
}
}
}
}
}
 
Thanks for the hint, Jon, and for the solution-which I am about to try
out-Marc.
 
Following makes large indents (lots of spaces) and comments (lots
of ------ etc) less painful:

default:
int count = 1;
while (reader.Peek() == c)
{
count++;
reader.Read();
}
keys = new string(c,count);
break;

Marc
 
Marc, thanks again for this.

I tried it out and it works; I needed to add a reference to
System.Windows.Forms to compile the code.

The file contents gets typed into the selected application.

Just one small point/refinement:

Having selected the application, it exe starts typing in that application's
window; howevr, if you switch to another window, the typing location changes
to that window: I can live with this but is there a way to 'lock' or 'fix'
the target window?
 
not using SendKeys; you'd have to use a different approach
unfortunately - and it isn't something I've had to do

I tried it a minute ago, and it also appears to fight VS when it comes
to leading whitespace - you could probably fix this by (if you know VS
is the target) keeping track if you have just started a new line (i.e.
nothing has been sent since the last {ENTER}), and disregard space
until something interesting appears. Maybe ;-p

Marc
 
Marc said:
[...] I tried it a minute ago, and it also appears to fight VS when it comes
to leading whitespace [...]

Isn't there a way to turn off the auto-indent stuff in VS?

I don't have a quick way to check it at the moment, but it would
surprise me if that was a behavior that is forced on the user.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top