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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restricting the keys a user presses...

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
I know I can do something like this:

Private Sub Text1_KeyPress(KeyAscii As Integer)
if keyascii=120 then
keyascii=121
end if
end sub

and this will replace a users input of an 'x' with a 'y'.

However I need to stop a value of keyascii being reproduced for the backspace and perform something else instead. For example you would imagine you could do this:

Private Sub Text1_KeyPress(KeyAscii As Integer)
if keyascii=8 then
keyascii=121
end if
end sub

Which I would like to covert a backspace into a 'y'. But this won't work. I assume its because 8 isn't a standard ascii character.

So how else can I do it?

I'm not trying to just limit what the user enters as I must perform some sort of action when this occurs too.

Any ideas?

elziko

 
Odd. There's nothing special about ASCII 8; it certainly ought to work (and does here)
 
You need to respond in the key_down or key_up events
 
Hi,
You are right you can not do just in KeyPress event.
In the KeyPress event capture what's the previous text when BAckSpace is keyed and in KeyUp event compare the texts before keyed and after BackSpace key is used.

Hope this helps,
Cheers, Rau
 
Follow this:
- Use the keydown event instead of keypress(you will have a better control also over the CTRL ALT and SHIFT keys)
- Use predefined vb constants instead numbers (for example vbKeyF1...) - this will make your applic independent of keyboard standards
- if you want to capture events for the controls of a form in a form event make sure to put KeyPreview propety of the form to True.

Hope this helps,
s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
This is all very odd. Everyone else seems to think it can't be done in the keypress event. yet here, under VB6 on NT4, it works fine.

Single form with a textbox on it and this code:

[tt]Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then
KeyAscii = 121
End If
End Sub[/tt]

causes a 'y' to be typed when you press the backspace key. Am I really the only one that it works for?
 
I double checked and it really doesn't work for me. The backspace operation occurs BEFORE the first line of code in the KeyPress event is executed.

I am using a Rich Text Box if that has some odd quirk, I dont know.

Thanks guys, I'm using the KeyDown event with much more success!

elziko
 
Yep, that'll be it. Rich Text Box behaves differently to a normal text box. Nice little inconsistency there, then...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top