Form.KeyPreview プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

/** @property */ public boolean get_KeyPreview () /** @property */ public void set_KeyPreview (boolean value)
フォームがすべてのキー イベントを受け取る場合は true。フォーム上で現在選択されているコントロールがキー イベントを受け取る場合は false。既定値は false です。

このプロパティを true に設定すると、KeyPress、KeyDown、KeyUp の各イベントをすべてフォームが受け取ります。フォームのイベントハンドラでキーストロークの処理が完了してから、フォーカスを持つコントロールにそのキーストロークが割り当てられます。たとえば、KeyPreview プロパティが true に設定され、現在選択されているコントロールが TextBox の場合は、キーストロークがフォームのイベント ハンドラで処理された後で、押されたキーを TextBox コントロールが受け取ります。キーボード イベントをフォームでだけ処理し、そのイベントをコントロールでは受け取らないようにする場合は、フォームの KeyPress イベント ハンドラの KeyPressEventArgs.Handled プロパティを true に設定します。
このプロパティを使用してアプリケーションのほとんどのキーストロークを処理し、フォームでキーストロークを処理するか、キーストロークを処理するために適切なコントロールを呼び出すことができます。たとえば、アプリケーションでファンクション キーが使用される場合は、キーストローク イベントを受け取るコントロールごとにコードを作成するのではなく、フォーム レベルでキーストロークを処理します。
![]() |
---|
フォームに表示できるコントロールまたは有効なコントロールがない場合は、すべてのキーボード イベントをフォームが自動的に受け取ります。 |

フォームの KeyPreview プロパティを true に設定して、フォーム レベルでキー イベントを処理するコード例を次に示します。この例を実行するには、次のコードを空のフォームに貼り付けます。
using namespace System::Windows::Forms; // This button is a simple extension of the button class that overrides // the ProcessMnemonic method. If the mnemonic is correctly entered, // the message box will appear and the click event will be raised. // This method makes sure the control is selectable and the // mnemonic is correct before displaying the message box // and triggering the click event. public ref class MyMnemonicButton: public Button { protected: bool ProcessMnemonic( char inputChar ) { if ( CanSelect && IsMnemonic( inputChar, this->Text ) ) { MessageBox::Show( "You've raised the click event " "using the mnemonic." ); this->PerformClick(); return true; } return false; } }; // Declare the controls contained on the form. public ref class Form1: public System::Windows::Forms::Form { private: MyMnemonicButton^ button1; public private: System::Windows::Forms::ListBox^ ListBox1; public: Form1() : Form() { // Set KeyPreview object to true to allow the form to process // the key before the control with focus processes it. this->KeyPreview = true; // Add a MyMnemonicButton. button1 = gcnew MyMnemonicButton; button1->Text = "&Click"; button1->Location = System::Drawing::Point( 100, 120 ); this->Controls->Add( button1 ); // Initialize a ListBox control and the form itself. this->ListBox1 = gcnew System::Windows::Forms::ListBox; this->SuspendLayout(); this->ListBox1->Location = System::Drawing::Point( 8, 8 ); this->ListBox1->Name = "ListBox1"; this->ListBox1->Size = System::Drawing::Size( 120, 95 ); this->ListBox1->TabIndex = 0; this->ListBox1->Text = "Press a key"; this->ClientSize = System::Drawing::Size( 292, 266 ); this->Controls->Add( this->ListBox1 ); this->Name = "Form1"; this->Text = "Form1"; this->ResumeLayout( false ); // Associate the event-handling method with the // KeyDown event. this->KeyDown += gcnew KeyEventHandler( this, &Form1::Form1_KeyDown ); } private: // The form will handle all key events before the control with // focus handles them. Show the keys pressed by adding the // KeyCode object to ListBox1. Ensure the processing is passed // to the control with focus by setting the KeyEventArg.Handled // property to false. void Form1_KeyDown( Object^ /*sender*/, KeyEventArgs^ e ) { ListBox1->Items->Add( e->KeyCode ); e->Handled = false; } }; [System::STAThreadAttribute] int main() { Application::Run( gcnew Form1 ); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からForm.KeyPreview プロパティを検索する場合は、下記のリンクをクリックしてください。

- Form.KeyPreview プロパティのページへのリンク