Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyPress VS KeyDown

Status
Not open for further replies.

xinyin

Programmer
Jan 16, 2003
81
HK
Hi everyone, I have 2 questions about keyboard events:
1) I want a text box to accept certain input keys only (say, numbers and +- signs only). This can be done easily: in the KeyPress event, use if-then-else cases to check the key input, if it is in the unwelcomed keys list, use the code "KeyAscii = 0" to "neutralize" the input, so nothing goes to the textbox. Unfortunately the "Del" key is also in the unwelcomed list - and too bad KeyPress does not support "Del". I was so happy when I discovered that using KeyDown instead will support all keys, but later I found out the trick "KeyCode = 0" does not apply in KeyDown... a dilemma. Does anyone here know a way to make KeyPress knowing "Del", or KeyDown knowing "KeyCode = 0"? (I try not to use input mask textbox because I don't like it putting those _ all over the place, plus I don't know how to make it to allow +- signs only in the 1st character.)
2. Which one (KeyPress or KeyDown) do you use more? I noticed that they are different not only in the number of keys supported, but also the values of KeyAscii and KeyCode, so I want to pick a more popular one.
 
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
MsgBox "I hit Delete"
End If
End Sub

KeyDown fires before KeyPress and supports all the keys while keyPress supports characters mostly
 
This is how to disable it:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
KeyCode = vbKeyEscape
End If
End Sub
 
Thanks for your code. After testing, KeyCode = vbKeyEscape works for all "non-ascii" keys (home, end, del, etc), but it cannot restrict those "ascii keys" (a~z, 0~9, etc)... so it is an opposite to KeyAscii = 0...
Is the only way I can do now is to use KeyPress and KeyDown together? If possible I will not do this because I already wrote one public function which contains a very detail process for all key inputs (not just checking, but also what to do when which key is pressed) so any textbox's keyboard event can call it. If I use both KeyPress and KeyDown then I have to split this function apart and make the coding more complicated.
 
Since it is quite hard to express in words, I just post the simplified code here:

Private Sub textboxX_KeyPress(KeyAscii As Integer)
KeyAscii = funKeyTester(textboxX, KeyAscii)
End Sub

Private Sub textboxY_KeyPress(KeyAscii As Integer)
KeyAscii = funKeyTester(textboxY, KeyAscii)
End Sub

Function funKeyTester(ByVal Box as Control, ByVal KeyAscii as Integer)
If KeyAscii = 8 Then Exit Function
'*Backspace can get through

If KeyAscii >= 48 And KeyAscii <= 57 Then
If Len(Box.text) <= 10 then
Exit Function
End If
End If
'*Numbers 0-9 can get through (After certain cases are met)

If '***KeyAscii = "Delete" then
If Len(Box.text) > 10 then
Exit Function
End If
'*You can press delete only when the text box is full (10 digits). This part is failed since there is no Ascii code for the Delete key

KeyAscii = 0
'*If reach this point, it means this input is prohibited and is "eaten", the user's typing will not go to the textbox

End Function

This function works fine. The only problem is it cannot block the delete key even when the circumstance is met. I changed the code arround (KeyAscii values to KeyCode values, KeyAscii = 0 to KeyCode = vbKeyEscape) and let KeyDown to call the function, now the delete part works but everything else is down.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top