0

I am planning to make a simple program that I can use in my daily life.

My goal is to create a program that can sendkeys into 2 applications like Notepad and Word or notepad1 and notepad2 at the same time. I don't have yet a code because I don't know how and where should I start.

I would like to use .net or C# or C++.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0
// import the function in your class
[DllImport ("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);

Process[] ps = Process.GetProcessesByName("notepad");
foreach (Process p in ps)
{
    IntPtr h = p.MainWindowHandle;
    SetForegroundWindow(h);
    SendKeys.SendWait("TEXT TO SEND");
}

Source: C# using Sendkey function to send a key to another application

0liveradam8
  • 752
  • 4
  • 18
  • Can i ask one more questions? is this for windows form? if not may i know? Because i already tried to insert the code into windows form but there are some reds in the code. please help me. – Christian Vitor Aug 18 '17 at 20:19
  • What errors are you having in your code? You'll need to write the import and static declaration outside of any methods, and the rest in your method. – 0liveradam8 Aug 18 '17 at 20:27
  • You need to move this: `[System.Runtime.InteropServices.DllImport("user32.dll")]` `static extern int SetForegroundWindow(IntPtr point);` to before this: `private void Form1_Load(object sender, EventArgs e);` – 0liveradam8 Aug 18 '17 at 20:48
  • Thank you so much, most of the error are already gone now. but there is one left. the code Process[] ps = Process.GetProcessesByName("notepad"); foreach (Process p in ps), it says "The type or namespace name 'Process' Could not be found (are you missing a using directive or an assembly reference?)" – Christian Vitor Aug 18 '17 at 21:01
  • At the top of your file where you have your `using` statements, add `using System.Diagnostics`. – 0liveradam8 Aug 18 '17 at 21:09
  • Its already working, thanks to you. but i works on this way, i have 2 notepad, when the form load, it send keys to first notepad and when it finish it will send keys to the next notepad. my goal is to send keys in 2 notepad at the same time. is it possible? – Christian Vitor Aug 18 '17 at 21:18
  • All computer applications essentially run one line of code after another. This is as close to `at the same time` as is easily achievable. It is possible to get very close to doing it at the same time, however. See this post: https://stackoverflow.com/questions/16711521/run-two-codes-simultaneously-c-sharp – 0liveradam8 Aug 18 '17 at 21:20
  • Thanks for your help. one last thing, what if i want to send key stroke not text.? is there any other way? – Christian Vitor Aug 18 '17 at 21:37
  • Like alt tab. so it can transfer back into the first notepad? – Christian Vitor Aug 18 '17 at 21:39
  • To combine alt and tab, use `SendKeys.SendWait("%{TAB}"). If this answer fully answers your question, please mark it as the correct answer so that others do not need to post new answers. Source: https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys(v=vs.110).aspx – 0liveradam8 Aug 18 '17 at 21:42