私のコメントを説明SendKeys.SendWait
すると、WPF では機能しますが、機能しSendKeys.Send
ません。問題の 1 つは、A を別のアプリケーションに送信しようとしている場合、形式が間違っていることです。中かっこは、文字 A には存在しない特別なキーであることを示します。SendKeys.SendWait("A")
代わりに使用する必要があります。もう 1 つの問題は、Wpf アプリケーションをキーボードとして使用している場合、ボタンをクリックした時点でフォアグラウンド アプリケーションになることです。WinApi と Pinvoke のいくつかの関数、つまりFindWindow
とを詳しく調べる必要がありSetForegroundWindow
ます。
SendKeys.Send のリンクから:
別のアプリケーションをアクティブにするマネージ メソッドがないため、現在のアプリケーション内でこのクラスを使用するか、FindWindow や SetForegroundWindow などのネイティブ Windows メソッドを使用して、他のアプリケーションにフォーカスを強制することができます。
別のアプリケーションに切り替える方法を示すサンプル (例としてメモ帳を使用しています)
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");
}
}