VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A KeyCode to Strings Converter

by Alexander Chia (3 Submissions)
Category: VB function enhancement
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)

It is a function that converts keycodes like those used in _KeyDown events, to strings like "Ctrl", "Alt", "F", "/", which are recognizable to the end-user. Great for game programmers.

Inputs
KeyCode as Integer
Assumes
If the user presses Shift+3, the function will not return "#", but will return "3" instead. It also uses easily recognizable symbols to represent certain keys, e.g "~" instead of "`", "\" instead of "|", and "Pg Dn", "Enter", "Space", to represent other keys.
Code Returns
KeyStr as String
Side Effects
If a key inputted does not match with any of the strings listed, the string returned will be "!".

Rate A KeyCode to Strings Converter

Public Function KeyStr(KeyCode As Integer) As String
 'Copyright Alexander Chia Yan Sheng
 Select Case KeyCode
 Case 65 To 90
  KeyStr = Chr(KeyCode)
 Case 48 To 57
  KeyStr = Chr(KeyCode)
 Case 13
  KeyStr = "Enter"
 Case 9
  KeyStr = "Tab"
 Case 112 To 123
  KeyStr = "F" & LTrim(Str(KeyCode - 111))
 Case 27
  KeyStr = "Esc"
 Case 192
  KeyStr = "~"
 Case 187
  KeyStr = "="
 Case 189
  KeyStr = "-"
 Case 219
  KeyStr = "["
 Case 220
  KeyStr = "\"
 Case 221
  KeyStr = "]"
 Case 186
  KeyStr = ";"
 Case 222
  KeyStr = "'"
 Case 188
  KeyStr = ","
 Case 190
  KeyStr = "."
 Case 191
  KeyStr = "/"
 Case 16
  KeyStr = "Shift"
 Case 20
  KeyStr = "Caps Lock"
 Case 144
  KeyStr = "Num Lock"
 Case 145
  KeyStr = "Scr Lock"
 Case 17
  KeyStr = "Ctrl"
 Case 18
  KeyStr = "Alt"
 Case 32
  KeyStr = "Space"
 Case 45
  KeyStr = "Ins"
 Case 46
  KeyStr = "Del"
 Case 33
  KeyStr = "Pg Up"
 Case 34
  KeyStr = "Pg Dn"
 Case 8
  KeyStr = "Back"
 Case 36
  KeyStr = "Home"
 Case 35
  KeyStr = "End"
 Case 37
  KeyStr = "Left Arrow"
 Case 38
  KeyStr = "Up Arrow"
 Case 39
  KeyStr = "Right Arrow"
 Case 40
  KeyStr = "Down Arrow"
 Case 106
  KeyStr = "* [Num Pad]"
 Case 107
  KeyStr = "+ [Num Pad]"
 Case 111
  KeyStr = "/ [Num Pad]"
 Case 109
  KeyStr = "- [Num Pad]"
 Case Else
  KeyStr = "!"
 End Select
End Function

Download this snippet    Add to My Saved Code

A KeyCode to Strings Converter Comments

No comments have been posted about A KeyCode to Strings Converter. Why not be the first to post a comment about A KeyCode to Strings Converter.

Post your comment

Subject:
Message:
0/1000 characters