anoop tamiya

anoop tamiya

  • NA
  • 60
  • 8.7k

How to send keystroke to 3rd party desktop application

Oct 4 2019 11:42 PM
I am facing problem  to automate a desktop aplication this code  work's only for 2 or 3 days after this send error is access denied but forground windows is focused. and activated ....i run this as admin.and also tried sendkeys.send,sendkey.sendwait.. but problem remains same....
 
namespace WindowsFormsApplication8 
{ public partial class Form1 : Form 
{ public Form1()
 {
 InitializeComponent();
 } 
[DllImport("User32.dll", SetLastError = true)] 
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
 private void button1_Click(object sender, EventArgs e)
 {
 try
 { 
Process myproc= Process.GetProcessesByName("name").FirstOrDefault(); 
if (myproc!= null)
 { var ffCondition = new PropertyCondition(AutomationElement.ProcessIdProperty, myproc.Id); 
var ffWindows = AutomationElement.RootElement.FindAll(TreeScope.Children, ffCondition);
 var soWindow = ffWindows.Cast<AutomationElement>().FirstOrDefault(w => w.Current.Name.Contains("title of window ")); 
if (soWindow != null) 
{
 var soWindowPattern = soWindow.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
 if (soWindowPattern != null) 
{
 // Restore window (activating it). 
    soWindowPattern.SetWindowVisualState(WindowVisualState.Normal);
 Thread.Sleep(100);
 // Close window. //soWindowPattern.Close(); 
SendKeys.Send("{f1}");
 //System.Windows.Forms.SendKeys.SendWait("{f1}");
 //string send = System.Windows.Input.Key.F1.ToString();
 //SendKeys.SendWait(send);
 var soWindow1 = ffWindows.Cast<AutomationElement>().FirstOrDefault(w => w.Current.Name.Contains("another child window ")); 
}
 }
 }
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.ToString());
 }
 }
 }
 }
 

Answers (1)