トップ回答者
ボタンクリックしたときにESCキー押下と同じ操作をしたい。

質問
-
VisualStudio2017のC#でWPFアプリケーションを作成しています。
ButtonをクリックしたときにESCキー押下のイベントを発生させたくて下記のようにコードを書いてみましたがWindow_KeyDownイベントは発生しませんでした。
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.SendKeys.SendWait("{ESC}");
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
}どうすれば発生するようになるでしょうか。
- 編集済み yty0918 2018年11月21日 17:06
回答
-
WPFではInputManagerを使うと良いです。
(参考)
WPF – Send keys
https://michlg.wordpress.com/2013/02/05/wpf-send-keys/WindowのKeyDownイベントではなく、Commandで受け取りたい場合には、例えば次の記述をします。
<Window.InputBindings> <KeyBinding Key="Esc" Command="{Binding CatchESCCommand}" /> </Window.InputBindings>
★良い回答には質問者は回答済みマークを、閲覧者は投票を!
- 回答としてマーク yty0918 2018年11月22日 2:53
すべての返信
-
こんな
namespace WpfApp1 { using System; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void Button_Click(object sender, RoutedEventArgs e) { await Task.Run(new Action(() => { System.Windows.Forms.SendKeys.SendWait("{ESC}"); })); } private void Window_KeyDown(object sender, KeyEventArgs e) { } } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
-
WPFではInputManagerを使うと良いです。
(参考)
WPF – Send keys
https://michlg.wordpress.com/2013/02/05/wpf-send-keys/WindowのKeyDownイベントではなく、Commandで受け取りたい場合には、例えば次の記述をします。
<Window.InputBindings> <KeyBinding Key="Esc" Command="{Binding CatchESCCommand}" /> </Window.InputBindings>
★良い回答には質問者は回答済みマークを、閲覧者は投票を!
- 回答としてマーク yty0918 2018年11月22日 2:53