英語で読む

次の方法で共有


方法: フォーム レベルでキーボード入力を処理する

Windows フォームは、メッセージがコントロールに到達する前に、フォーム レベルでキーボード メッセージを処理する機能を提供します。 このトピックでは、このタスクを実行する方法について説明します。

フォーム レベルでキーボード メッセージを処理するには

  • スタートアップ フォームの KeyPress イベントまたは KeyDown イベントを処理し、フォームのコントロールに到達する前にキーボード メッセージがフォームで受信されるように、フォームの KeyPreview プロパティを true に設定します。 次のコード例では、すべての数値キーを検出し、'1'、'4'、および '7' を使用して、KeyPress イベントを処理します。

    // Detect all numeric characters at the form level and consume 1,
    // 4, and 7. Note that Form.KeyPreview must be set to true for this
    // event handler to be called.
    void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar >= 48 && e.KeyChar <= 57)
        {
            MessageBox.Show("Form.KeyPress: '" +
                e.KeyChar.ToString() + "' pressed.");
    
            switch (e.KeyChar)
            {
                case (char)49:
                case (char)52:
                case (char)55:
                    MessageBox.Show("Form.KeyPress: '" +
                        e.KeyChar.ToString() + "' consumed.");
                    e.Handled = true;
                    break;
            }
        }
    }
    

次のコード例は、上記の例のアプリケーション全体です。 アプリケーションには、TextBoxからフォーカスを移動できる他のいくつかのコントロールと共に TextBox が含まれています。 メイン FormKeyPress イベントは '1'、'4'、および '7' を使用し、TextBoxKeyPress イベントは、残りのキーを表示するときに '2'、'5'、および '8' を使用します。 数値キーを押したときに MessageBox 出力を比較し、フォーカスが他のコントロールの 1 つにあるときに、TextBoxMessageBox 出力とフォーカスを持つときに、数値キーを押した場合の出力を比較します。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace KeyboardInputForm
{
    class Form1 : Form
    {
        TextBox TextBox1 = new TextBox();

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
        {
            this.AutoSize = true;

            FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.AutoSize = true;
            panel.FlowDirection = FlowDirection.TopDown;
            panel.Controls.Add(TextBox1);
            this.Controls.Add(panel);

            this.KeyPreview = true;
            this.KeyPress +=
                new KeyPressEventHandler(Form1_KeyPress);
            TextBox1.KeyPress +=
                new KeyPressEventHandler(TextBox1_KeyPress);
        }

        // Detect all numeric characters at the form level and consume 1,
        // 4, and 7. Note that Form.KeyPreview must be set to true for this
        // event handler to be called.
        void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Form.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)49:
                    case (char)52:
                    case (char)55:
                        MessageBox.Show("Form.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }
        }

        // Detect all numeric characters at the TextBox level and consume
        // 2, 5, and 8.
        void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Control.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)50:
                    case (char)53:
                    case (char)56:
                        MessageBox.Show("Control.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }
        }
    }
}

コードのコンパイル

この例では、次のものが必要です。

  • System、System.Drawing、System.Windows.Forms アセンブリへの参照。

関連項目


その他のリソース

ドキュメント

トレーニング

モジュール

Develop keyboard-accessible products - Training

Learn how to develop keyboard-accessible products with our comprehensive module. This course covers essential guidelines, design considerations, implementation strategies, and testing methods to ensure your products are accessible to all users, including those with disabilities.