Form.KeyPreview 属性
Form.KeyPreview 属性
今天再做KeyDown 和 KeyUp 事件时,就是忘了设置,窗体的KeyPreview 属性,所以KeyDown 和 KeyUp 事件没有反应(这里说明一下,本人使用的是自定义控件,如果是窗体控件就没有问题的。具体原因,下面会有解释。)
获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
public bool KeyPreview { get; set; }
/** @property */ public boolean get_KeyPreview () /** @property */ public void set_KeyPreview (boolean value)
public function get KeyPreview () : boolean public function set KeyPreview (value : boolean)
属性值
如果窗体将接收所有键事件,则为 true;如果窗体上当前选定控件接收键事件,则为 false。默认为 false。备注
当此属性设置为 true 时,窗体将接收所有 KeyPress、KeyDown 和 KeyUp 事件。在窗体的事件处理程序处理完该击键后,然后将该击键分配给具有焦点的控件。例如,如果 KeyPreview 属性设置为 true,而且当前选定的控件是 TextBox,则在窗体的事件处理程序处理了击键后,TextBox 控件将接收所按的键。要仅在窗体级别处理键盘事件并且不允许控件接收键盘事件,请将窗体的 KeyPress 事件处理程序中的KeyPressEventArgs.Handled 属性设置为 true。
可以使用此属性处理应用程序中的大部分击键事件,并可以处理击键事件或调用适当的控件来处理击键事件。例如,当应用程序使用功能键时,可能希望在窗体级别处理这些击键,而不是为可能接收击键事件的每个控件编写代码。
![]() |
---|
如果窗体没有可见或启用的控件,则该窗体自动接收所有键盘事件。 |
![]() |
---|
可以对窗体上的控件进行编程,以取消它所接收的任何击键。由于控件从不向窗体发送这些击键,因此无论 KeyPreview 为何种设置,窗体永远都无法看到它们。 |
下面的代码示例演示如何将窗体的 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 ); }
龙腾一族至尊龙骑