Control.IsInputKey(Keys) Метод
Определение
Определяет, является ли заданная клавиша обычной клавишей ввода или специальной клавишей, нуждающейся в предварительной обработке.Determines whether the specified key is a regular input key or a special key that requires preprocessing.
protected:
virtual bool IsInputKey(System::Windows::Forms::Keys keyData);
protected virtual bool IsInputKey (System.Windows.Forms.Keys keyData);
abstract member IsInputKey : System.Windows.Forms.Keys -> bool
override this.IsInputKey : System.Windows.Forms.Keys -> bool
Protected Overridable Function IsInputKey (keyData As Keys) As Boolean
Параметры
Возвращаемое значение
Значение true
, если указанная клавиша является стандартной клавишей ввода; в противном случае — значение false
.true
if the specified key is a regular input key; otherwise, false
.
Примеры
В следующем примере кода показано, как переопределить IsInputKey метод для TextBox элемента управления.The following code example shows you how to override the IsInputKey method for a TextBox control. В этом примере TabTextBox
класс обрабатывает клавишу TAB.In this example, the TabTextBox
class handles the TAB key. Когда TabTextBox
фокус переключается и пользователь нажимает клавишу TAB, в точку вставки текста добавляется четыре пробела, заменяя любой выделенный текст.When the TabTextBox
has the focus and the user presses the TAB key four spaces are added at the text insertion point, replacing any selected text. По умолчанию TextBox элемент управления обрабатывает клавишу TAB, перемещая фокус ввода на следующий элемент управления.By default, the TextBox control handles the TAB key by moving the input focus to the next control. В этом случае нажатие клавиши никогда не достигает OnKeyDown переопределения метода.In this case, the keypress never reaches the OnKeyDown method override. Чтобы предотвратить это поведение по умолчанию, IsInputKey Переопределение метода возвращает, true
когда пользователь нажимает клавишу TAB.To prevent this default behavior, the IsInputKey method override returns true
when the user presses the TAB key. Для всех других нажатий клавиш IsInputKey Переопределение метода возвращает результат вызова версии базового класса метода.For all other keypresses, the IsInputKey method override returns the result of calling the base-class version of the method.
using System.Windows.Forms;
public class Form1 : Form
{
public Form1()
{
FlowLayoutPanel panel = new FlowLayoutPanel();
TabTextBox tabTextBox1 = new TabTextBox();
tabTextBox1.Text = "TabTextBox";
panel.Controls.Add(tabTextBox1);
TextBox textBox1 = new TextBox();
textBox1.Text = "Normal TextBox";
panel.Controls.Add(textBox1);
this.Controls.Add(panel);
}
}
class TabTextBox : TextBox
{
protected override bool IsInputKey(Keys keyData)
{
if (keyData == Keys.Tab)
{
return true;
}
else
{
return base.IsInputKey(keyData);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyData == Keys.Tab)
{
this.SelectedText = " ";
}
else
{
base.OnKeyDown(e);
}
}
}
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Public Sub New()
Dim panel As New FlowLayoutPanel()
Dim tabTextBox1 As New TabTextBox()
tabTextBox1.Text = "TabTextBox"
panel.Controls.Add(tabTextBox1)
Dim textBox1 As New TextBox()
textBox1.Text = "Normal TextBox"
panel.Controls.Add(textBox1)
Me.Controls.Add(panel)
End Sub
End Class
Class TabTextBox
Inherits TextBox
Protected Overrides Function IsInputKey( _
ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Tab Then
Return True
Else
Return MyBase.IsInputKey(keyData)
End If
End Function
Protected Overrides Sub OnKeyDown( _
ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyData = Keys.Tab Then
Me.SelectedText = " "
Else
MyBase.OnKeyDown(e)
End If
End Sub
End Class
Комментарии
Вызовите IsInputKey метод, чтобы определить, является ли ключ, заданный keyData
параметром, ключом ввода, который требуется элементу управления.Call the IsInputKey method to determine whether the key specified by the keyData
parameter is an input key that the control wants. Этот метод вызывается во время предварительной обработки сообщения окна, чтобы определить, следует ли предварительно обработать указанный входной ключ или отправить его непосредственно в элемент управления.This method is called during window message preprocessing to determine whether the specified input key should be preprocessed or sent directly to the control. Если IsInputKey возвращает true
, указанный ключ отправляется непосредственно в элемент управления.If IsInputKey returns true
, the specified key is sent directly to the control. Если IsInputKey возвращает false
, указанный ключ предварительно обрабатывается и отправляется в элемент управления только в том случае, если он не занят этапом предварительной обработки.If IsInputKey returns false
, the specified key is preprocessed and only sent to the control if it is not consumed by the preprocessing phase. К предварительно обработанным ключам относятся TAB, RETURN, ESC и стрелка вверх, стрелка вниз, стрелка влево и клавиши со СТРЕЛКАми вправо.Keys that are preprocessed include the TAB, RETURN, ESC, and the UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW keys.