KeyPressEventArgs.KeyChar Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define o carácter correspondente à tecla pressionada.
public:
property char KeyChar { char get(); };
public:
property char KeyChar { char get(); void set(char value); };
public char KeyChar { get; }
public char KeyChar { get; set; }
member this.KeyChar : char
member this.KeyChar : char with get, set
Public ReadOnly Property KeyChar As Char
Public Property KeyChar As Char
Valor de Propriedade
O carácter ASCII que é composto. Por exemplo, se o utilizador pressionar SHIFT + K, esta propriedade devolve um K maiúsculo.
Exemplos
O exemplo seguinte cria um TextBox controlo. O keypressed método usa a KeyChar propriedade para verificar se a tecla ENTER foi pressionada. Se a tecla ENTER for pressionada, a Handled propriedade é definida para true, o que indica que o evento é tratado.
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
public ref class Form1: public Form
{
public:
Form1()
{
// Create a TextBox control.
TextBox^ tb = gcnew TextBox;
this->Controls->Add( tb );
tb->KeyPress += gcnew KeyPressEventHandler( this, &Form1::keypressed );
}
private:
void keypressed( Object^ /*o*/, KeyPressEventArgs^ e )
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.
// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if ( e->KeyChar == (char)13 )
e->Handled = true;
}
};
int main()
{
Application::Run( gcnew Form1 );
}
using System;
using System.Windows.Forms;
public class Form1: Form
{
public Form1()
{
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(keypressed);
}
private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.
// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Public Sub New()
' Create a TextBox control.
Dim tb As New TextBox()
Me.Controls.Add(tb)
AddHandler tb.KeyPress, AddressOf keypressed
End Sub
Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
' The keypressed method uses the KeyChar property to check
' whether the ENTER key is pressed.
' If the ENTER key is pressed, the Handled property is set to true,
' to indicate the event is handled.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
e.Handled = True
End If
End Sub
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
Observações
Use a KeyChar propriedade para amostrar as teclas em tempo de execução e para modificar as teclas em circunstâncias especiais de execução. Por exemplo, pode desativar KeyChar pressionamentos de teclas não numéricas quando o utilizador insere um código postal, alterar todas as pulsações alfabéticas para maiúsculas num campo de introdução de dados, ou monitorizar o teclado ou outro dispositivo de introdução de teclas para combinações específicas de tecla.
Pode obter ou definir as seguintes chaves:
A-Z, A-Z.
CTRL.
Sinais de pontuação.
Teclas numéricas, tanto no topo do teclado como no teclado numérico.
ENTRE.
Não pode obter ou definir as seguintes chaves:
A tecla TAB.
INSERIR e ELIMINAR.
CASA.
FIM.
PÁGINA PARA CIMA E PÁGINA PARA BAIXO.
F1-F2.
ALT.
Teclas de seta.
Note
Para informações sobre como detetar qualquer uma das chaves que não sejam de carácter mencionadas acima, veja a KeyEventArgs classe.