Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please tell me the classes and methods I need to call when working in WPF. I tried with the following as someone suggested in http://stackoverflow.com/questions/17917447/wpf-send-keycode-to-active-application

HOWEVER, THIS DIDN'T WORK OUT WHILE WORKING IN VS 2012 and .NET 4.0.
C#
public partial class MainWindow : Window
{
    [DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        SetForegroundWindow(FindWindowByCaption(IntPtr.Zero, "Untitled - Notepad"));
        SendKeys.SendWait("A");
    }
Posted
Updated 23-Nov-15 20:42pm
v2
Comments
Tomas Takac 24-Nov-15 2:43am    
E-mail removed. Never post your e-mail on a public site.
Sergey Alexandrovich Kryukov 24-Nov-15 3:30am    
Why?
—SA

1 solution

There is no SendKeys form equivalent in WPF.
InputManager can allow you to implement a workaround for send keys - check this blog post[^].
 
Share this answer
 
Comments
Member 11886653 24-Nov-15 3:26am    
Thank You Sir for your valuable reply. However, the link you have attached is not available.
Abhinav S 24-Nov-15 6:42am    
Strange. The link worked for me - https://michlg.wordpress.com/2013/02/05/wpf-send-keys/.

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