|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Sending keystrokes c#
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
News Fiend
|
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.
|
||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
News Fiend
|
I got what I needed so you can ignore this now. Unless someone wants to share some input about the subject. Thats always appreciated.
__________________
|
||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
News Fiend
|
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}");
}
}
}
|
||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|