Mystical_Moon
09-04-2003, 03:41 PM
I asked a question earlier regarding this and found that using keyascii allows me to set which characters can be pressed but I can't use backspace or delete. How can I use them? Any advice is thanked for in advance!
I asked a question earlier regarding this and found that using keyascii allows me to set which characters can be pressed but I can't use backspace or delete. How can I use them? Any advice is thanked for in advance!
Use the KeyDown or the KeyUp events.
--Van^
Mystical_Moon
09-04-2003, 03:45 PM
How so? I am very new to the programming system as I am just taking in class. An example always works best for me so if someone could give an example it would be greatly appreciated.
passel
09-04-2003, 03:50 PM
I'm at work so don't have the Constant names to use, but you can
add those characters to your acceptance string like this.
(Chr$(8) is Backspace and Chr$(127) is delete).
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim a As String
a = "abc" & Chr$(8) & Chr$(127)
If InStr(a, Chr$(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
You could change the code to this.
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim a As String
a = "abc" & Chr$(8) & Chr$(127)
If InStr(a, Chr$(KeyAscii)) = 0 Then
debug.print KeyAscii
KeyAscii = 0
end if
End Sub
and it will printout the KeyAscii of each key it "rejects". If you want
that key then note it, and add it to your "accept" string.
Deadalus
09-04-2003, 04:16 PM
In case of delete, the KeyPress event will never fire, it's only caught in the KeyDown and KeyUp events, where you use keycodes and not keyascii values.
I assume the constant names Passel talks about are vbKeyBack (8) and vbKeyDelete (46), but those also represent keycodes.
Corrected the name of the first constant. Thanks to Passel for pointing it out.
passel
09-04-2003, 04:34 PM
No, ascii 9 is tab, 8 is backspace and 127 is delete,
but you are correct about the delete key. I thought I tested it, but the
delete key is not converted to Ascii 127 on the PC, or at least is not
sent to the KeyPress Event.
Of course, since the Delete Key doesn't come through KeyPress, then
it should still be working (not filtered by the code in KeyPress) so I
guess I allowed myself to be led down that path when he said it wasn't
working.
And Chr$(vbKeyBack) would have worked since the keycode is the
same as the Ascii for those keys (tab, backspace)
Mystical_Moon
09-04-2003, 05:43 PM
ok so just to check this again I use the keydown and keyup functions and not keypress unless it's like passel said? and thanks alot for that guys i didn't know what the code for those were... where can I find that out?
samus535
09-04-2003, 05:50 PM
ok so just to check this again I use the keydown and keyup functions and not keypress unless it's like passel said? and thanks alot for that guys i didn't know what the code for those were... where can I find that out?
You can use the VB constant vbKeyBack and VBKeyDelete
passel
09-04-2003, 07:56 PM
Basically in the Keydown routine, check for the keycodes and set
keycode to 0 for keys you don't want processed. To find the values or
constants, if you have the help files installed (MSDN), you should be
able to type in vbCrLf, for instance, put the cursor on it, and hit F1 (for
help). It should bring up the Miscellaneous Constants page. Click on the
"See Also" link and you should get a list of constant categories. Select
the "Visual Basic Constants" and you should get a list of Visual Basic
Constants categories. Select Keycode Constants, and you should get
the complete list of constants to be compared to "Keycode".
There you will find vbKeyBack and vbKeyTab, which we mentioned above
, but technically, Deadalus is right in that these are constants
associated with "KeyCode" and not "KeyAscii" even though they are the
same value in this instance.
Under Miscellaneous Constants are listed the "appropriate" constants
vbTab and vbBack.
In any case, in a pinch, I usually just add code to do a debug.print in
the keydown routine to print out the value of keycode, and then use the
literal number, with a comment, in a Select Case statement, or add them
to an array.
Sometimes, it is still expediant to convert the keycodes into Characters
so that you can use instr to quickly search a string, rather than have to
code a lot of cases, or write an array search routine, as long as you
know that the "characters" in the string don't represent Ascii values, but
is merely a collection of keycodes.
Another option, create a byte array with an index for each keycode, and
preset it with 1's or 0's for keys you want to accept or reject, and then
just index it with the keycode to decide.
Better yet, keycodes are an integer type, so create an array of integers
and preset the indexes you want to pass, with their value, and the keys
you want to reject, leave at 0. Then with one line of code, you can
pass or ignore those keys.
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
keycode = validkey(keycode)
End Sub
Anyway, you should have plenty of info now to decide how you want to
do it.