Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET sendkeys to calculator

Tags:

c#

sendkeys

The sendkeys code below works well for Notepad but it doesn't work for Calculator. What is the problem? (It's another problem compared to what I sent here Sendkeys problem from .NET program)

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
    IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator");
    //IntPtr calculatorHandle = FindWindow("Notepad", "Untitled - Notepad");
    if (calculatorHandle == IntPtr.Zero)
    {
        MessageBox.Show("Calculator is not running.");
        return;
    }
    SetForegroundWindow(calculatorHandle);
    System.Threading.Thread.Sleep(1000);
    SendKeys.SendWait("111*11=");
    //SendKeys.SendWait("{ENTER}");
    //cnt++;
    SendKeys.Flush();
}
like image 656
user397232 Avatar asked Feb 27 '23 05:02

user397232


1 Answers

I'll tell you how you can figure out how to send keystorkes to calc.exe.

Use spy++ to monitor the messages on the calc.exe window process as you're using it. To do this go into spy++ and click on the log messages toolbar button. Drag the cursor onto the calc.exe window. The instructions I gave are for VS2008, they may differ slightly for the Spy++ included in other VS versions. But the same functionality has always been available.

You will see exactly what messages are sent as you are entering text. You need to do the same.

Use the Win32 API SendMessage, LPARAM and WPARAM to your found window handle.

like image 118
Brian R. Bondy Avatar answered Mar 05 '23 06:03

Brian R. Bondy