A little problem about 「How to: Simulate Mouse and Keyboard Events in Code」

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="border-collapse:collapse; font-family:arial,sans-serif; font-size:13px
<span style="color:#26252a; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif <span style="font-size:14px; line-height:16px Im a student from Taiwan and trying to implement this:
<span style="color:#26252a; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif <span style="font-size:14px; line-height:16px <br/>

<span style="color:#26252a; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif <span style="font-size:14px; line-height:16px http://technet.microsoft.com/zh-tw/library/ms171548.aspx" target="_blank How
to: Simulate Mouse and Keyboard Events in Code <br/>

This example helps me a lot and it works ,too !
But recently there is a little problem I encounter when i change the target application to another one( ex: Second Life) instead of Microsoft Calculator.

My program shows this warning message in running time.

「SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method.」
<br/>

As a result,the Key message cannot be sent to the application.
Could anyone help me to solve this little issue or provide another better way to implement the same thing??
Many thanks!!

This is the code:

<div style="color:black; background-color:white
<pre><span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.Windows;
<span style="color:blue using System.Windows.Controls;
<span style="color:blue using System.Windows.Data;
<span style="color:blue using System.Windows.Documents;
<span style="color:blue using System.Windows.Input;
<span style="color:blue using System.Windows.Media;
<span style="color:blue using System.Windows.Media.Imaging;
<span style="color:blue using System.Windows.Navigation;
<span style="color:blue using System.Windows.Shapes;
<span style="color:blue using System.Runtime.InteropServices;
<span style="color:blue using System.Drawing;
<span style="color:blue using System.Windows.Forms;

<span style="color:blue namespace WpfApplication3
{
<span style="color:gray /// <span style="color:gray <summary>
<span style="color:gray ///<span style="color:green MainWindow.xaml 的互動邏輯
<span style="color:gray /// <span style="color:gray </summary>
<span style="color:blue public <span style="color:blue partial <span style="color:blue class MainWindow : Window
{
<span style="color:green // Get a handle to an application window.
[DllImport(<span style="color:#a31515 "USER32.DLL")]
<span style="color:blue public <span style="color:blue static <span style="color:blue extern IntPtr FindWindow(<span style="color:blue string lpClassName,
<span style="color:blue string lpWindowName);

<span style="color:green // Activate an application window.
[DllImport(<span style="color:#a31515 "USER32.DLL")]
<span style="color:blue public <span style="color:blue static <span style="color:blue extern <span style="color:blue bool SetForegroundWindow(IntPtr hWnd);


<span style="color:blue public MainWindow()
{
InitializeComponent();
}

<span style="color:blue private <span style="color:blue void button1_Click(<span style="color:blue object sender, RoutedEventArgs e)
{
<span style="color:green // Get a handle to the Calculator application. The window class
<span style="color:green // and window name were obtained using the Spy++ tool.
IntPtr calculatorHandle = FindWindow(<span style="color:#a31515 "Second Life", <span style="color:#a31515 "Second Life ");

<span style="color:green // Verify that Calculator is a running process.
<span style="color:blue if (calculatorHandle == IntPtr.Zero)
{
System.Windows.MessageBox.Show(<span style="color:#a31515 <span style="color:#a31515 "Second Life<span style="color:#a31515 is not running.");<br/> <span style="color:blue return;
}

<span style="color:green // Make Calculator the foreground application and send it
<span style="color:green // a set of calculations.
SetForegroundWindow(calculatorHandle);
SendKeys.Send(<span style="color:#a31515 "{ENTER}");
<span style="color:green //keybd_event();


}
}
}

[/code]


<span style="border-collapse:collapse; color:#666666; font-family:arial,sans-serif; font-size:13px - My programming environment : Windows 7 32-bits and use Visual Studio 2010 to write this sample with C# WPF application.<span style="border-collapse:collapse; font-family:arial,sans-serif; font-size:13px

<br/>

View the full article
 
Back
Top