Share vb.net keypress

happygorilla253

New member
** Cách phát hiện Keypress trong vb.net **

Trong hướng dẫn này, bạn sẽ tìm hiểu cách phát hiện Keypresses trong VB.NET.Bạn sẽ học cách:

* Nhận mã khóa của máy ép khóa
* Nhận ký tự của máy ép phím
* Nhận trạng thái thay đổi của máy ép phím
* Nhận trạng thái kiểm soát của máy ép khóa
* Nhận trạng thái alt của máy ép khóa

## Nhận mã khóa của máy ép khóa

Mã khóa của máy ép khóa là một số đại diện cho khóa đã được nhấn.Bạn có thể nhận mã khóa của một lần nhấn phím bằng sự kiện `keydown`.Sự kiện `Keydown` được bắn khi nhấn phím.

Để lấy mã khóa của máy ép khóa, bạn có thể sử dụng thuộc tính `key` của đối tượng` keyEventArgs` được chuyển cho trình xử lý sự kiện `keydown`.Thuộc tính `key` trả về giá trị liệt kê` keys` đại diện cho khóa đã được nhấn.

Ví dụ: mã sau có mã khóa của khóa `A` khi sự kiện` keydown` được kích hoạt:

`` `VBNet
Text Private Subbox1_Keydown (Người gửi là đối tượng, E là KeyEventArss) Xử lý TextBox1.KeyDown
'Nhận mã khóa của khóa đã được nhấn.
Dim keycode As Keys = E.Key

'In mã khóa vào bảng điều khiển.
Console.WriteLine ("Mã khóa là {0}", KeyCode)
Kết thúc phụ
`` `

## Nhận nhân vật của một máy ép phím

Nhân vật của một máy ép phím là chữ cái hoặc biểu tượng được biểu thị bằng khóa được nhấn.Bạn có thể nhận được ký tự của một máy nhấn phím bằng thuộc tính `keychar` của đối tượng` keyEventArgs` được truyền cho trình xử lý sự kiện `keydown`.

Ví dụ: mã sau có ký tự của khóa `A` khi sự kiện` keydown` được kích hoạt:

`` `VBNet
Text Private Subbox1_Keydown (Người gửi là đối tượng, E là KeyEventArss) Xử lý TextBox1.KeyDown
'Nhận nhân vật của chìa khóa đã được nhấn.
Nhân vật mờ như char = e.keychar

'In ký tự vào bảng điều khiển.
Console.WriteLine ("ký tự là {0}", ký tự)
Kết thúc phụ
`` `

## Nhận trạng thái thay đổi của máy ép khóa

Trạng thái dịch chuyển của máy ép phím là giá trị boolean cho biết có nhấn phím thay đổi khi nhấn phím hay không.Bạn có thể nhận trạng thái dịch chuyển của một máy nhấn khóa bằng cách sử dụng thuộc tính `Shift` của đối tượng` keyEventArgs` được truyền cho trình xử lý sự kiện `keydown`.

Ví dụ: mã sau có trạng thái dịch chuyển của khóa `A` khi sự kiện` keydown` được kích hoạt:

`` `VBNet
Text Private Subbox1_Keydown (Người gửi là đối tượng, E là KeyEventArss) Xử lý TextBox1.KeyDown
'Nhận trạng thái thay đổi của khóa đã được nhấn.
Dim ShiftState như Boolean = E.Shift

'In trạng thái dịch chuyển vào bảng điều khiển.
Console.WriteLine ("Trạng thái dịch chuyển là {0}", ShiftState)
Kết thúc phụ
`` `

## Nhận trạng thái kiểm soát của máy ép khóa

Trạng thái điều khiển của máy ép phím là giá trị boolean cho biết có nhấn phím điều khiển khi nhấn phím hay không.Bạn có thể nhận trạng thái điều khiển của một máy nhấn khóa bằng cách sử dụng thuộc tính `control` của đối tượng` keyEventArgs` được truyền cho trình xử lý sự kiện `keydown`.

Ví dụ: mã sau có trạng thái điều khiển của khóa `A` khi sự kiện` keydown` được kích hoạt:

`` `VBNet
Text Private Subbox1_Keydown (Người gửi là đối tượng, E là KeyEventArss) Xử lý TextBox1.KeyDown
'Nhận trạng thái kiểm soát của khóa đã được nhấn.
Dim ControlState As Boolean = E.Control

'In trạng thái điều khiển vào bảng điều khiển.
Console.WriteLine ("Trạng thái điều khiển là {0}", ControlState)
Kết thúc phụ
`` `

## Nhận trạng thái alt của máy ép khóa
=======================================
**How to Detect Keypress in VB.NET**

In this tutorial, you will learn how to detect keypresses in VB.NET. You will learn how to:

* Get the key code of a key press
* Get the character of a key press
* Get the shift state of a key press
* Get the control state of a key press
* Get the alt state of a key press

## Getting the Key Code of a Key Press

The key code of a key press is a number that represents the key that was pressed. You can get the key code of a key press using the `KeyDown` event. The `KeyDown` event is fired when a key is pressed down.

To get the key code of a key press, you can use the `Key` property of the `KeyEventArgs` object that is passed to the `KeyDown` event handler. The `Key` property returns a `Keys` enumeration value that represents the key that was pressed.

For example, the following code gets the key code of the `A` key when the `KeyDown` event is fired:

```vbnet
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
' Get the key code of the key that was pressed.
Dim keyCode As Keys = e.Key

' Print the key code to the console.
Console.WriteLine("The key code is {0}", keyCode)
End Sub
```

## Getting the Character of a Key Press

The character of a key press is the letter or symbol that is represented by the key that was pressed. You can get the character of a key press using the `KeyChar` property of the `KeyEventArgs` object that is passed to the `KeyDown` event handler.

For example, the following code gets the character of the `A` key when the `KeyDown` event is fired:

```vbnet
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
' Get the character of the key that was pressed.
Dim character As Char = e.KeyChar

' Print the character to the console.
Console.WriteLine("The character is {0}", character)
End Sub
```

## Getting the Shift State of a Key Press

The shift state of a key press is a boolean value that indicates whether the shift key was pressed when the key was pressed. You can get the shift state of a key press using the `Shift` property of the `KeyEventArgs` object that is passed to the `KeyDown` event handler.

For example, the following code gets the shift state of the `A` key when the `KeyDown` event is fired:

```vbnet
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
' Get the shift state of the key that was pressed.
Dim shiftState As Boolean = e.Shift

' Print the shift state to the console.
Console.WriteLine("The shift state is {0}", shiftState)
End Sub
```

## Getting the Control State of a Key Press

The control state of a key press is a boolean value that indicates whether the control key was pressed when the key was pressed. You can get the control state of a key press using the `Control` property of the `KeyEventArgs` object that is passed to the `KeyDown` event handler.

For example, the following code gets the control state of the `A` key when the `KeyDown` event is fired:

```vbnet
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
' Get the control state of the key that was pressed.
Dim controlState As Boolean = e.Control

' Print the control state to the console.
Console.WriteLine("The control state is {0}", controlState)
End Sub
```

## Getting the Alt State of a Key Press
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top