Click here to Skip to main content
15,906,328 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a Form with a button . I want to press the Button in form instead of "W" key on keyboard and the "W" key become active and write in notepad "W". That's very simply .

Are there any Class or Method for this action.
Posted
Updated 3-Jul-15 14:30pm
v4
Comments
[no name] 3-Jul-15 16:40pm    
What?
mehrdad72 3-Jul-15 16:56pm    
Have you ever seen On-Screen Keyboard ?
[no name] 3-Jul-15 17:07pm    
Yes I have. How does that make your not-a-question any clearer? What code have your written? What is the problem with the code that you have written? Have you done any research at all?
Afzaal Ahmad Zeeshan 3-Jul-15 20:30pm    
Is it very simple, then why not you create it yourself?

1 solution

C#
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

public const int WM_SETTEXT = 0x000c;
internal void SendText(string content)
{
     var notepads = System.Diagnostics.Process.GetProcessesByName("notepad");

     // if Applicable, it runs Notepad Process
     //if (notepads.Length == 0)
     //    notepads = new System.Diagnostics.Process[]  System.Diagnostics.Process.Start("notepad") };

     if (notepads.Length > 0 && notepads[0] != null)
     {
         IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
         SendMessage(child, WM_SETTEXT, 0, content);
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900