Sign in to follow this  

Automating Keystrokes in C#

This topic is 2485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I simulate a key-press event in C#? I am writing a program that will need to automatically press tab to get the cursor to the correct text field, and then copy a string into that field. But I'm not sure how to code the tabbing. I'll also need to code sequences of keystrokes, like alt, f, k, enter, and so on, but I'm assuming that the method for tabbing will be the same as for the others. Will I need to write a manager class to do this, or is there some C# function that already does it? Or would it be easiest to use something like SFML, which has a really easy input capturing system?

More details: I've recently begun learning some C# programming as a hobby. But at work, I've found an opportunity to apply my hobby practically, but I won't have time to study the language a lot more to be able to take advantage of it. My workplace uses a very old program called Medipac for storing lots of information, and I've proposed that my boss let me do some analysis of that information. But getting it through the Medipac interface is very, very slow, so I'm roughing out a utility that will automatically extract and format the information I need.

I've got the overall program flow figured out (I think, at least), but I'm not sure how to implement this particular bit.

Share this post


Link to post
Share on other sites
You are looking for SendKeys.Send(@"{tab}"). Will do exactly what you want to do if you want to use a tab key.

But if you are just trying to get the focus onto a certain field I would not suggest doing that.

Just do this.

correctTextField.Focus(); That will move the focus to that text field. You can do that with any control.

theTroll








Share this post


Link to post
Share on other sites
[quote name='TheTroll' timestamp='1298497283' post='4778146']
You are looking for SendKeys.Send(@"{tab}"). Will do exactly what you want to do if you want to use a tab key.

But if you are just trying to get the focus onto a certain field I would not suggest doing that.

Just do this.

correctTextField.Focus(); That will move the focus to that text field. You can do that with any control.

theTroll
[/quote]

Thanks for the advice. I can't use TextField.Focus() though, becasue I need my utility to be running in the background sending keyboard input to the other program, which isn't a windows window. It's a very old command line program, and tabbing is the easiest way I can think of to position the cursor.


[quote name='Fl4sh' timestamp='1298497931' post='4778152']
You can use the .net docs to find your answers for things like this easily.
[/quote]

My apologies. I have pulled up a lot of information from MSDN now, and it is very useful and easy to find.

Share this post


Link to post
Share on other sites
- Before you try to implement such a remote-control app yourself, I'd give [url="http://www.autoitscript.com/site/autoit/"]AutoIt[/url] a shot. ([b]Edit[/b]: Sorry, I misread, not sure if AutoIt can handle console apps)
- Ask yourself if there an easier way. Is there an underlying database which could provide enough information and you could access without the app?
- Make sure you don't violate the EULA, this sounds like reverse engineering.

Share this post


Link to post
Share on other sites
[quote name='unbird' timestamp='1298501821' post='4778177']
- Before you try to implement such a remote-control app yourself, I'd give [url="http://www.autoitscript.com/site/autoit/"]AutoIt[/url] a shot.
- Ask yourself if there an easier way. Is there an underlying database which could provide enough information and you could access without the app?
- Make sure you don't violate the EULA, this sounds like reverse engineering.
[/quote]

Thanks for the link, I'll take a look at AutoIt when I have a bit more time. There is certainly an underlyling database, but if this becomes a major interdepartmental project my boss probably won't let me keep on with it. I don't know anything about the database, so I can't fiddle around with it, and the only access to it is through this particular program. As for the EULA, it's certainly a non-issue in this case. I'm not trying to modify the program in any way, I just want to be able to access the information I need faster and automate the extraction of that information. What I want to do isn't different from hiring 20 transcribers to do it by hand, aside from being faster and cheaper.

Share this post


Link to post
Share on other sites
Okay, if you are going to SendKeys, make sure that before you send you make the Command Line window the active window.

You can use the Windows API FindWindow and SetActiveWindow to make sure that it is the active window.

Good luck.

Share this post


Link to post
Share on other sites

This topic is 2485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this