New Posts  All Forums:Forum Nav:

Sending keystrokes c#

post #1 of 4
Thread Starter 
So im trying to make a macros program and I am trying to send keystrokes to the active window.

I have so far pulling the the application to focus, and got the threading down, but just need to know how to send the active window keys. Any help would be appreciated.

I did build a program like this a while back but since then lost the HD and all the data. I remember using a very old method of importing the user32.dll and calling a function from that. Buts thats all I remember. I have tried using the newer method SendKeys but thats not working for me.
post #2 of 4
Thread Starter 
I got what I needed so you can ignore this now. Unless someone wants to share some input about the subject. Thats always appreciated.
post #3 of 4
Quote:
Originally Posted by kyotejones View Post
I got what I needed so you can ignore this now. Unless someone wants to share some input about the subject. Thats always appreciated.
Hi

I am also working on similar app. So please give some hint regarding how to send
keystrokes to some other application.
post #4 of 4
Thread Starter 
Should have checked back faster. Well all I ended up doing was using SendKeys.SendWait(). The reason it wasn't working for me before was because I was trying to send keys to a window that was still being pulled to focus. So I added a thread delay of 1 second before I sent the keys.

example:
Code:
using System.Diagnostics;
using System.Threading;

public partial class CHOPPA
{

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private Thread thrTyping;

private void startThread()
{
    ThreadStart ts = new ThreadStart(sendKeys);
    thrTyping = new Thread(ts);
    thrTyping.Start();
}

private void sendKeys()
{
    while (condition)
    {
        //find and bring the application i need to focus
        Process[] processes = Process.GetProcessesByName("App_Name");
        foreach (Process proc in processes)
        {
            SetForegroundWindow(proc.MainWindowHandle);
        }

        //sleep the thread before I send it the keys for at least 1 second
        Thread.Sleep(1000);
        SendKeys.SendWait("{F4}");
    }
}
}
sendKeys is the method that sends my application the keys. Now obviously there are probably some mistakes here but its an example, so be gentle if you have comments.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming