![](/img/trans.png)
[英]How to detect combination of multiple modifier and non-modifier keys in VB.NET?
[英]Detect Numeric Keys in Textbox - VB.NET
我想在文本框按鍵事件中檢測數字鍵盤的數字鍵,實際上,我只想允許用戶從數字鍵盤而不是字母數字鍵盤輸入數字。
如果數字不是來自鍵盤的數字小鍵盤,則應取消事件。
除了GetAsyncKeyState()
以外,還有什么方法可以完成此任務?
目前,我正在嘗試此代碼。
If e.KeyChar = Chr(49) Then '48 to 57 are the char codes for "0 to 9" of alphanumeric keypad.
e.Handled = True
End I
使用GetAsyncKeyState()
是我的最后優先事項,因為它會降低我的應用程序的性能。
有兩種檢查方法
若要檢查特定的鍵而不是字符,請使用KeyDown / KeyUp事件的KeyCode屬性獲取Keys.NumPad0到Keys.NumPad9之間的值。 KeyPress不提供此屬性。
If (e.KeyCode >= Keys.NumPad0 and e.KeyCode <= Keys.NumPad9) Then ...
如果要檢查字符是否為數字,可以使用Char.IsDigit檢查字符是否為數字。 你可以寫:
If Char.IsDigit(e.KeyChar) Then ....
鍵盤0-9是鍵盤代碼96至105
KEY CODE
------------------
numpad 0 96
numpad 1 97
numpad 2 98
numpad 3 99
numpad 4 100
numpad 5 101
numpad 6 102
numpad 7 103
numpad 8 104
numpad 9 105
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.