VB Key Functions

Visual Basic Modules Add comments

Here is a Visual Basic .bas module I wrote on May 31, 2006 containing 12 functions I've found to be very useful when dealing with VB KeyAscii and KeyCode values. I use this module extensively in my VB KeyAscii & KeyCode Finder program.

Download VBKeyFunctions.bas (May 31, 2006)

1.) ConstantFromKeyAscii(keyascii As Integer) As String
This function will take a KeyAscii value and will output the corresponding VB Constant value (if one exists). If one does not exist, the function will return "N/A".

2.) ConstantFromKeyCode(keycode As Integer) As String
This function will take a KeyCode value and will output the corresponding VB Constant value (if one exists). If one does not exist, the function will return "N/A".

3.) WriteCharFromKeyCode(keycode As Integer, shiftdown As Boolean) As String
This function will take a KeyCode value and will output it's respective character. It takes two arguments, one is the KeyCode number itself and the other is a boolean value called shiftdown. If the SHIFT key is being held down you would say true, and if it's not you would say false. This function will then return the approriate character.

4.) WriteCharFromKeyAscii(keyascii As Integer) As String
This function will take a KeyAscii value and will output it's respective character. It only takes one argument, the KeyAscii value (as an integer). This function will then return the corresponding character. In my opinion, this is much better than the built in Chr() function for two reasons. One is that for the non-printable and extended ASCII set Chr() will output garbage, whereas this function will output a useful text string. The second reason I like it better is because Chr() will crash if you pass it a number large than 255. With this function, if you pass it a number larger than 255 it will just return "N/A".

5.) WriteKeyAsciiFromKeyCode(keycode As Integer, shiftdown As Boolean) as String
This function will take a KeyCode value and will output it's equivalent KeyAscii value. It takes two arguments, one is the KeyCode number itself and the other is a boolean value called shiftdown. If the SHIFT key is being held down you would say true, and if it's not you would say false. It will return "N/A" if there is no equivalent KeyAscii value.

6.) WriteKeyCodeFromKeyAscii(keyascii As Integer) As String
This function will take a KeyAscii value and will output it's equivalent KeyCode value. It only takes one argument which is the KeyAscii value as an integer. It will return "N/A" if there is no equivalent KeyCode value.

7.) WriteKeyAsciiFromChar(someChar as String) as Integer
This function will take a character and will output it's equivalent KeyAscii value. It only takes one argument which is the character itself as a string. It will return "666" if it can't find an approriate KeyAscii value.

8.) WriteKeyCodeFromChar(someChar as String) as Integer
This function will take a character and will output it's equivalent KeyCode value. It only takes one argument which is the character itself as a string. It will return "666" if there is no equivalent KeyCode value.

9.) IsShiftCharacter(someChar as String) as Boolean
This function will return true if the SHIFT key has to be held down to create a certain character. For example to create the dollar sign ($) you have to press SHIFT + 4, so IsShiftCharacter("$") would return true.

10.) IsShiftDown(shift as integer) as Boolean
This function will return true if the shift key is being held down.

11.) IsCtrlDown(shift as integer) as Boolean
This function will return true if the CTRL key is being held down.

12.) IsAltDown(shift as integer) as Boolean
This function will return true if the ALT key is being held down.

Filelist
 VBKeyFunctions.bas

One Response to “VB Key Functions”

  1. i want to keyascii for txtbox for word only,string or character. what keyascii of the character or string which for "word only" not for " numbers only". thank you !!!!!!!!!

Leave a Comment

Entries RSS Comments RSS Log in