C # Connecting console application to notepad

What I want to do is create a simple Windows application that will snap to NotePad and then simulate a keystroke. I have the process of opening a NotePad, bringing it to the fore, and then simulating the number 1 by pressing. However, if I clicked on a notebook, everything that is active will become what is typed.

How to attach this application to a notebook so that I can click and type anything, and this application will still enter commands into a notebook?

This is the DLL I use to simulate keystrokes :

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics; using WindowsInput; namespace NotePadTesting { class Program { [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); static void Main(string[] args) { Process[] processes = Process.GetProcessesByName("notepad"); if (processes.Length == 0) { Process.Start("notepad.exe"); processes = Process.GetProcessesByName("notepad"); } if (processes.Length == 0) { throw new Exception("Could not find notepad huh...."); } IntPtr WindowHandle = processes[0].MainWindowHandle; SetForegroundWindow(WindowHandle); for (int i = 0; i < 500; i++) { System.Threading.Thread.Sleep(100); InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_1); } } } } 
+7
c # process hook
source share
2 answers

If you already have a window handle that you want to enter, you can use the PostMessage function. You just need google virtual key codes.

+2
source share

You will need to interact with Notepad.exe through PostMessage . You will need to use the P/Invoke methods to call this from User32.dll :

 using System.Runtime.InteropServices; internal static class NativeMethods { // This method signature is derived from MSDN PostMessage declaration. [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr hwnd, uint msg, uint wParam, uint lParam); // Other p/invoke methods go here, such as FindWindow... } 

You can find Notepad using FindWindow , allowing you to get a window handle ( HWND ).

Once you do this, you can send “Keyboard Notifications” to this window. These notifications simulate keyboard input and are intended only for this window, even if the window is minimized or otherwise it is not a foreground window.

Messages are important WM_KEYDOWN , WM_KEYUP , and WM_CHAR . Many of them accept scan codes instead of virtual key codes , which means that you will need to move back and forth. This is done using MapVirtualKey . All WM commands take specific forms of their LPARAM and WPARAM values, so check the MSDN documentation for what it expects.

Is there a tool called Spy++ that (used for?) Comes with Visual Studio that allows you to peek into these posts. This is a great debugging / reverse engineering tool for this type of thing.

Using all of the Win32 APIs listed above, you can send keystrokes to an external window.

+2
source share

All Articles