• 締切済み

WPF C#でF10のイベント取得方法について

WPF C#でファンクションキーF10のイベント取得の事で わかる方がおられたら教えてください。 今日1日ネットで調べたのですが、わかりませんでした。 よろしくお願いいたします。 開発環境 OS :WindowsXP、      Tool:Visual Studio 2010      NET Framework3.5 です。 やりたい事は、WPFの画面でF1~F12までの ファンクションを押下してイベントを認識 できるかの調査を行いましたが、F10だけが イベントを認識してくれません。 下記がコーディング内容です。 「XAML」 <Window x:Class="MUT3SEFunction.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" PreviewKeyDown="Window_PreviewKeyDown" mc:Ignorable="d"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     d:DesignHeight="338" d:DesignWidth="478" SizeToContent="WidthAndHeight"> <TextBox Height="63" Name="textBox1" Width="253" Text="" FontSize="20"/> </Window> 「C#」 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Input; namespace MUT3SEFunction { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { textBox1.Text = e.Key.ToString() + "が押されました。"; } } } ネットで調べた中で、『ProcessKeyPreviewメソッド』をオーバーライドしれば できるとの事でためしてみましたがWPF画面では、ProcessKeyPreviewがオーバーライド できるメソッドが無いとの事でビルドエラーが発生してしまいました。 C#のアプリケーション開発では、うまく動作しました。 下記のロジックです。 「C#」 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Forms; namespace MUT3SEFunction { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } // F10キーが処理されたかどうかのフラグを設定します。 bool isF10_Processed = false; // ProcessKeyPreviewメソッドをオーバーライドします。 protected override bool ProcessKeyPreview(ref Message m) { if (m.WParam == (IntPtr)(Keys.F10)) { // F10が処理されていない場合、処理を実行します。 if (!isF10_Processed) { if (m.Msg == 0x105)   // "0x105"は"WM_SYSKEYUP"を表します。 { // カスタマ処理を実行します。 ProcessCustomLogic((Keys)m.WParam); } } } // フラグを常にクリアします。 isF10_Processed = false; return ProcessKeyPreview(ref m); } private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.F10) { // F10が処理されたことを記録します。 isF10_Processed = true; } // カスタマ処理を実行します。 ProcessCustomLogic(e.KeyData); } private void ProcessCustomLogic(Keys key) { // カスタマ処理を実装します。 textBox1.Text = key.ToString() + " が押されました。"; } } } どうかわかる方がおれば、是非ご教授宜しく願いいたします。 初めての投稿で、雑な内容の質問で申し訳ありません。 最後にWPF開発者向けのお勧めのサイトがあれば是非教えてください。 以上です。よろしくお願いいたします。

みんなの回答

  • x-1919
  • ベストアンサー率52% (91/173)
回答No.1

private void Window_PreviewKeyDown(object sender, KeyEventArgs e) {   string pushedKey = "";   switch (e.Key)   {     case Key.F1:     case Key.F2:     case Key.F3:     case Key.F4:     case Key.F5:     case Key.F6:     case Key.F7:     case Key.F8:     case Key.F9:     case Key.F11:     case Key.F12:       pushedKey = e.Key.ToString();       break;     case Key.System:       if (e.SystemKey == Key.F10)       {         pushedKey = "F10";       }       break;   }   textBox1.Text = pushedKey; } とか? F10 って Windows 用のキーだから使わないほうがいいって聞くがね。

ytsutomu
質問者

補足

回答ありがとうございます。 補足しますと、F10を押下した場合だけ「Window_PreviewKeyDown」のロジック自体認識しませんでした。 「F10 って Windows 用のキーだから使わないほうがいいって聞くがね。」については、  Windows 標準のショートカットキーだから使わない方が良いと聞きますが、F10をどうしても  必要としています。

関連するQ&A

  • 〔C#〕WPFのディープコピーをするには?

    オブジェクトのディープコピーは、以下のURLを参考にして作成しました。 http://d.hatena.ne.jp/tekk/20100131/1264913887 WPF(UserControl)で作成したViewに対して上記URLのDeepCopy()関数を実行すると、 実行時に次のエラーが出てしまいます。 「'System.Windows.UserControl'はシリアル化可能として設定されていません。」 調べますと、Sezializable属性を付ければ良いとあるのですが、 該当のWPF(xaml)のcsファイル(XXX.xaml.cs)のクラスの宣言の上に以下のように付けましたが、 変わらずエラーが出てしまいます。 [Serializable] public partical class XXX { ・・・ どのようにしたら、上記のエラーを解決し、Viewのディープコピーができるのでしょうか? とても困っています。 ご教授よろしくお願いいたします。

  • ユーザーコントロールを動的に切り替えたい

    visual C# 2010 Express を使用しています。 例えばWPFで新しいプロジェクトを立ち上げるとMainWindow.xamlがあって そのウインドウの中にはGridが内装されていると思います。 そこで、UserControl1.xamlとUserControl2.xamlを追加して 最初はMainWindowのGridにはUserControl1を貼っておきます。 そしてMainWindowのボタンかなにかをクリックしたら プログラムで動的にUserControl2に貼り替えたいのですが、 MainWindowクラス内でthis.(ここでプロパティやメンバを)候補を探しても gridなどが見つからないのです・・・ どうやったら(なにで)動的にこのような切り替えをすることができるのでしょうか?

  • WPF C# PointToClient

    お世話になります。 宜しくお願いいたします。 以下のコードでエラーが出ます。 System.Drawing.Point p = this.PointToClient(System.Windows.Forms.Cursor.Position); エラー内容 'クラス名' に 'PointToClient' の定義が含まれておらず、型 'クラス名' の最初の引数を受け付ける拡張メソッドが見つかりませんでした。using ディレクティブまたはアセンブリ参照が不足しています。 使用中のクラスは、たしかに System.Windows.Controls.UserControl を継承しています。 さまざまな方法を試しましたが、全く理解できません。 ・解決方法または、WPFでマウス座標を取得する方法について ご教授お願いいたします。

  • WPFのDatagridの縦スクロール

    VisualBasic2010を利用してWindows Formアプリケーションを 開発しております。 その際にForm上にElementHostを配置しWPFで構築した DataGridを設定しグリッドにデータを表示しております。 データ表示自体は問題なく行えているのですが、 Datagridの縦スクロールをWindows Form上に配置する ボタンから制御したいと考えております。 このような制御の方法をご存知の方がいらっしゃいましたら ご教授頂けますと幸いです。 WPF側 <UserControl x:Class="Grid_Main" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:_01_AAAA" mc:Ignorable="d" d:DesignHeight="200.373" d:DesignWidth="300" Height="Auto" Width="Auto"> <DataGrid ItemsSource="{Binding}" ScrollViewer.CanContentScroll="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" ScrollViewer.IsDeferredScrollingEnabled="True" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility="None" Margin="0,0,0,0" BorderBrush="{x:Null}"> <DataGrid.Columns> <DataGridTemplateColumn > <!-- 表示時のセルのテンプレートを指定 --> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <!-- 表示時のセルのテンプレートに同プロジェクト内のクラスを指定 --> <local:Grid_Detail /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <!-- 編集時のセルのテンプレートを指定 --> <!-- 表示専用であれば定義不要 --> </DataGrid.Columns> </DataGrid> </UserControl>

  • WPFでのモーダル/モードレスダイアログの同時操作

    WPFでメインウインドウからモードレスダイアログとモーダルダイアログを表示し、 それぞれのダイアログに対して操作をしたいと思っています。 1.メインウインドウのボタンをクリックしてモードレスダイアログを表示 2.メインウインドウのボタンをクリックしてモーダルダイアログを表示 上記2の処理を行うと、上記1で表示したモードレスダイアログが一切操作を受け付けなく なるのですが、対応方法はありますでしょうか? 仕様上、片方のダイアログは必ずモーダル表示にします。 ソースコードは以下のような感じです。 private Window2 window2; private void button1_Click(object sender, RoutedEventArgs e) { window2 = new Window2(); window2.Show(); } private void button2_Click(object sender, RoutedEventArgs e) { Window window3 = new Window3(); window3.ShowDialog(); } よろしくお願いいたします。

  • DataContextについて

    visual C# 2010 Express を使用しています。 UserControl1とUserControl2をFooクラスのメンバAuthorizedによって 切り替えるということをしたいと思い、 MainWindow.xamlの内容を次のようにしました。 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1"> <Window.DataContext> <my:Foo/> </Window.DataContext> <Grid> <my:UserControl1 HorizontalAlignment="Left" Margin="140,74,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="44" Width="82" Visibility="{Binding Path=NotAuthorized, Converter={StaticResource BooleanToVisibilityConverter1}}" /> <my:UserControl2 Height="45" HorizontalAlignment="Left" Margin="288,65,0,0" x:Name="userControl21" VerticalAlignment="Top" Width="77" Visibility="{Binding Path=Authorized, Converter={StaticResource BooleanToVisibilityConverter1}}" /> <Button Content="Button" Height="38" HorizontalAlignment="Left" Margin="38,109,0,0" Name="button1" VerticalAlignment="Top" Width="80" Click="button1_Click" /> <TextBox Height="40" HorizontalAlignment="Left" Margin="46,34,0,0" Name="textBox1" VerticalAlignment="Top" Width="88" /> </Grid> </Window> そしてFooクラス [Foo.cs] class Foo { private bool _authorized = false; public bool Authorized { get { return _authorized; } set { _authorized = value; } } public bool NotAuthorized { get { return !Authorized; } } } そして、MainWindow.xaml.csのbutton1_Click内で、 Foo f = new Foo(); f.Authorized = true; とやったのですが、切り替わらないのです。 なんとなく自分でもこれだけでは切り替わらないだろうなと想像はつくのですが、 でも下の図のプロパティでは [ソース: (Datacontext - Foo)] とありDatacontextはFooクラスだと認識してますよね? なのでこの記述 Visibility="{Binding Path=NotAuthorized, Converter={StaticResource BooleanToVisibilityConverter1}}" だけでも認識してくれるのかなと期待したのですが、駄目でした・・・ この場合はどう記述すれば良いのでしょうか?またなぜそれが必要なのかという理由も教えて頂けないでしょうか?

  • OutLookのメール受信通知 (C#)

    教えてください。C#の勉強中でわからないところがあります。 [環境] C# 2.0 windowsXP Outlook2003 [内容] アプリケーションを起動後、 Outlookでメールを受信したら、アプリケーションでFormの表示のラベルを変更したいのでが、うまくいきません。教えてくださいませ。 ちなみに、下記のソースはMessageBoxで作成したものです。 MessageBoxだとうまくいくのですが・・・ using System; using System.Windows.Forms; using OutLook = Microsoft.Office.Interop.Outlook; [ソース] using System; using System.Windows.Forms; using OutLook = Microsoft.Office.Interop.Outlook; [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmMessage());  MessageBox.Show("待機中"); MessageStand(); } /// <summary> /// OutLook待機処理 /// </summary> private static void OneSightMessageStand() { OutLook.ApplicationClass outLookApp = new OutLook.ApplicationClass(); outLookApp.NewMailEx += new OutLook.ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx); } /// <summary> /// イベント処理 /// </summary> private static void outLookApp_NewMailEx(string EntryIDCollection) { MessageBox.Show("受信しました。" + EntryIDCollection, "NOTE", MessageBoxButtons.OK); # 通常ならメッセージボックスではなくイベントが発生したら、Formのラベルにメッセージを表示したい。 frmMessage objfm = new frmMessage(); objfm.label.txt = "受信しました。"; }

  • c#のプログラムについて。

    c#のプログラムについて。 お世話になります。 c#初心者です。 下記の様なコードを記述してますが、 「 Form1.Button1.Visible == false;」の部分で 「'WindowsFormApplication1.Form1'にButton1'の定義がありません。」 と言うエラーが出てしまいます。 どの様に定義して良いのかが分からず、悩んでいます。 どなたかお教え頂きたく宜しくお願い致します。        記 using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication3 { static class Program { /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); string result = System.Environment.MachineName; if (result == "xxxxxxx") { Form1.Button1.Visible == false; } } } }

  • KeyUpイベントにて

    テキストボックスのKeyUpイベントにて『Enter』または『Tab』が押されたら処理をさせようと Private Sub Text_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Text.KeyUp If e.KeyCode = Keys.Enter or e.KeyCode = Keys.Tab Then   処理コード End If End Sub としたのですが『Enter』のときしか処理コードが反映されません ご教授お願いします

  • イベント内で別イベントを呼ぶ方法

    早速質問させていただきます。 Form上にDataGridView(dgv)があります。 dgvのセルをマウスでダブルクリックした時に処理を入れております。 Private Sub dgv_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellDoubleClick   If e.RowIndex = -1 Then     Exit Sub   End If   '//処理 End Sub このdgv上でEnterキーを押した際に、ダブルクリックしたのと同じ処理を 行いたいのですが、このような場合、どのような実装をするのが一般的なのでしょうか? 例えば、下記の様にCall dgv_CellDoubleClick(sender, e)をするとEventArgsが合わないためエラーになってしまいます。 適切な方法をご教授お願い致します。 Private Sub dgv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgv.KeyDown   If e.KeyCode = Keys.Enter Then     Call dgv_CellDoubleClick(sender, e)   End If End Sub

専門家に質問してみよう