Is there a way to have a label to show the name for the key that is pressed?
Like, lets say that the keycode is 13. Well, since that is the Enter key, is there something that can convert that 13 to say Enter in the labels caption?
Re: Is there a way to have a label to show the name for the key that is pressed?
Sure, a Select Case or If/Then structure.
Re: Is there a way to have a label to show the name for the key that is pressed?
I was afraid of that.... So there isn't another way?
Re: Is there a way to have a label to show the name for the key that is pressed?
This will get you started:
Code:
Option Explicit
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 13: Label1.Caption = "Enter key"
Case 8: Label1.Caption = "Backspace"
Case 27: Label1.Caption = "Escape"
Case 32: Label1.Caption = "Space bar"
Case 46: Label1.Caption = "Delete"
Case 65 To 90, 97 To 122: Label1.Caption = UCase$(Chr$(KeyCode)) '~~~ A to Z
Case 48 To 57: Label1.Caption = CStr(KeyCode - 48) '~~~ 0 to 9
Case Else: Label1.Caption = "Write the code for this !!! ;-)"
End Select
End Sub
Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Add a labelbox to your form and run the code...:wave:
Re: Is there a way to have a label to show the name for the key that is pressed?
"Enter" is a very generalized term. It is actually called a "Carriage Return"
Please see the link in my signature for Ascii Table. That should help...
Re: Is there a way to have a label to show the name for the key that is pressed?
Quote:
Originally Posted by
akhileshbc
This will get you started:
Code:
Option Explicit
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 13: Label1.Caption = "Enter key"
Case 8: Label1.Caption = "Backspace"
Case 27: Label1.Caption = "Escape"
Case 32: Label1.Caption = "Space bar"
Case 46: Label1.Caption = "Delete"
Case 65 To 90, 97 To 122: Label1.Caption = UCase$(Chr$(KeyCode)) '~~~ A to Z
Case 48 To 57: Label1.Caption = CStr(KeyCode - 48) '~~~ 0 to 9
Case Else: Label1.Caption = "Write the code for this !!! ;-)"
End Select
End Sub
Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Add a labelbox to your form and run the code...:wave:
akhileshbc made me curious...His post is better for letters but with the magic of cut, paste and Excel here are some more:
Code:
Case 1: Label1.Caption = "Left mouse button"
Case 2: Label1.Caption = "Right mouse button"
Case 3: Label1.Caption = "CANCEL key"
Case 4: Label1.Caption = "Middle mouse button"
Case 8: Label1.Caption = "BACKSPACE key"
Case 9: Label1.Caption = "TAB key"
Case 12: Label1.Caption = "CLEAR key"
Case 13: Label1.Caption = "ENTER key"
Case 16: Label1.Caption = "SHIFT key"
Case 17: Label1.Caption = "CTRL key"
Case 18: Label1.Caption = "MENU key"
Case 19: Label1.Caption = "PAUSE key"
Case 20: Label1.Caption = "CAPS LOCK key"
Case 27: Label1.Caption = "ESC key"
Case 32: Label1.Caption = "SPACEBAR key"
Case 33: Label1.Caption = "PAGE UP key"
Case 34: Label1.Caption = "PAGE DOWN key"
Case 35: Label1.Caption = "END key"
Case 36: Label1.Caption = "HOME key"
Case 37: Label1.Caption = "LEFT ARROW key"
Case 38: Label1.Caption = "UP ARROW key"
Case 39: Label1.Caption = "RIGHT ARROW key"
Case 40: Label1.Caption = "DOWN ARROW key"
Case 41: Label1.Caption = "SELECT key"
Case 42: Label1.Caption = "PRINT SCREEN key"
Case 43: Label1.Caption = "EXECUTE key"
Case 44: Label1.Caption = "SNAPSHOT key"
Case 45: Label1.Caption = "INS key"
Case 46: Label1.Caption = "DEL key"
Case 47: Label1.Caption = "HELP key"
Case 102: Label1.Caption = "6 key"
Case 103: Label1.Caption = "7 key"
Case 104: Label1.Caption = "8 key"
Case 105: Label1.Caption = "9 key"
Case 106: Label1.Caption = "MULTIPLICATION SIGN (*) key"
Case 107: Label1.Caption = "PLUS SIGN (+) key"
Case 108: Label1.Caption = "ENTER (keypad) key"
Case 109: Label1.Caption = "MINUS SIGN (-) key"
Case 110: Label1.Caption = "DECIMAL POINT(.) key"
Case 111: Label1.Caption = "DIVISION SIGN (/) key"
Case 112: Label1.Caption = "F1 key"
Case 113: Label1.Caption = "F2 key"
Case 144: Label1.Caption = "NUM LOCK key"
Case 65: Label1.Caption = "A key"
Case 66: Label1.Caption = "B key"
Case 67: Label1.Caption = "C key"
Case 68: Label1.Caption = "D key"
Case 69: Label1.Caption = "E key"
Case 70: Label1.Caption = "F key"
Case 71: Label1.Caption = "G key"
Case 72: Label1.Caption = "H key"
Case 73: Label1.Caption = "I key"
Case 74: Label1.Caption = "J key"
Case 75: Label1.Caption = "K key"
Case 76: Label1.Caption = "L key"
Case 77: Label1.Caption = "M key"
Case 78: Label1.Caption = "N key"
Case 79: Label1.Caption = "O key"
Case 80: Label1.Caption = "P key"
Case 81: Label1.Caption = "Q key"
Case 82: Label1.Caption = "R key"
Case 83: Label1.Caption = "S key"
Case 84: Label1.Caption = "T key"
Case 85: Label1.Caption = "U key"
Case 86: Label1.Caption = "V key"
Case 87: Label1.Caption = "W key"
Case 88: Label1.Caption = "X key"
Case 89: Label1.Caption = "Y key"
Case 89: Label1.Caption = "z key"
Case 96: Label1.Caption = "0 key"
Case 97: Label1.Caption = "1 key"
Case 98: Label1.Caption = "2 key"
Case 99: Label1.Caption = "3 key"
Case 100: Label1.Caption = "4 key"
Case 101: Label1.Caption = "5 key"
Case 114: Label1.Caption = "F3 key"
Case 115: Label1.Caption = "F4 key"
Case 116: Label1.Caption = "F5 key"
Case 117: Label1.Caption = "F6 key"
Case 118: Label1.Caption = "F7 key"
Case 119: Label1.Caption = "F8 key"
Case 120: Label1.Caption = "F9 key"
Case 121: Label1.Caption = "F10 key"
Case 122: Label1.Caption = "F11 key"
Case 123: Label1.Caption = "F12 key"
Case 124: Label1.Caption = "F13 key"
Case 125: Label1.Caption = "F14 key"
Case 126: Label1.Caption = "F15 key"
Case 127: Label1.Caption = "F16 key"
Re: Is there a way to have a label to show the name for the key that is pressed?
There is API that does this:
Convert a Character Code to a string describing the Keyboard Keys which must be pressed
http://www.vbaccelerator.com/home/vb...ng/article.asp
Add this code to demo:
Private Sub txtChar_KeyPress(KeyAscii As Integer)
Dim vKey As Long
Dim iShift As Long
Debug.Print GetKeyboardString(Chr(KeyAscii), vKey, iShift)
End Sub
Re: Is there a way to have a label to show the name for the key that is pressed?
Well, i was wanting to use the label as an example. But, actually, it is a combobox. If you look in my sig, it is for updating my GetAscynKeyState Button Configuration. I think i already got it though. However, i am going to look at that code DrUnicode posted. Thanks!