アプリ版:「スタンプのみでお礼する」機能のリリースについて

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/pre …
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/20 …
    xmlns:mc="http://schemas.openxmlformats.org/markup-compati …
    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開発者向けのお勧めのサイトがあれば是非教えてください。

以上です。よろしくお願いいたします。

A 回答 (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 用のキーだから使わないほうがいいって聞くがね。

この回答への補足

回答ありがとうございます。

補足しますと、F10を押下した場合だけ「Window_PreviewKeyDown」のロジック自体認識しませんでした。

「F10 って Windows 用のキーだから使わないほうがいいって聞くがね。」については、
 Windows 標準のショートカットキーだから使わない方が良いと聞きますが、F10をどうしても
 必要としています。

補足日時:2012/12/20 08:42
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!


このQ&Aを見た人がよく見るQ&A