vb6 Intercept keystroke

ttobin333
ttobin333 used Ask the Experts™
on
Dear Experts,

I am looking for the best way to intercept a keyboard input, and decide whether to process it or present a messagebox depending on the character typed. For example, if a certain character is typed, a message box is shown and the character is not printed in the textbox.

Thanks!  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David RobitailleAnalyst Programmer

Commented:
From what you are saying, for a textbox, put your code in the textbox`s KeyPress event the parameter is (KeyAscii As Integer) so you could test on that. set the KeyAscii to 0 to cancel the key.  
the other events are keyUp and  KeyDown, witch the parameter are (KeyCode As Integer, Shift As Integer). with this one, you get the phisical key that has been pressed plus the shift key (shift, alt and ctrl).

 
David RobitailleAnalyst Programmer

Commented:

Author

Commented:
Thanks, davrob60! That works well.

Can you assist with another related detail: how can I grab the character in the textbox just to the left and the right of the insertion point, when I click anywhere within the textbox?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
To clarify, when I say "grab" I mean just identify, not actually copy or cut.
You can use the SELSTART function..
Eg. CurPosition = MyTextBox.SelStart
If CurPosition > 1 Then  ToMyLeft$ = Mid(MyTextBox.Text , CurPosition -1,1)
If CurPosition > 0 Then  ToMyRight$ = Mid(MyTextBox.Text , CurPosition +1,1)

Author

Commented:
KeyPress works great except for the delete key, which I can capture with KeyDown but I can't block it similar to using KeyAscii=0 in KeyPress. Is there a way?
David RobitailleAnalyst Programmer

Commented:
for the delete key you need to check it in the keyUp and/or  KeyDown event using the vbKeyDelete constant.
check the "Key Code Constants" help topic for others codes...
 

Author

Commented:
Yes, I have done that but how do I block the delete command once I catch it? KeyDown does not let me assign a zero value.
David RobitailleAnalyst Programmer

Commented:
try setting the set keypreview  property to true.
I will test that tomorrow...
David RobitailleAnalyst Programmer

Commented:
Analyst Programmer
Commented:
I just try this code and it work well....
Are you talking about the Delete key or the backspace? because backspase is a ascii character...

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyDelete Then
        KeyCode = 0
    End If

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyBack Then
        KeyAscii = 0
    End If
End Sub

Open in new window

Author

Commented:
Is there a way to detect and block the delete keydown event without subclassing?
David RobitailleAnalyst Programmer

Commented:
As i said, I tryed the above code and it worked. The delete key was blocked.... you could try to set the from`s keypreview propety to true, but it was working with both values when i tryed it.

Author

Commented:
I think you are refering to the link to the other question/answer that you mentioned earlier. I am not saying it does not work, but it uses subclassing, which I prefer to avoid if possible.  
David RobitailleAnalyst Programmer

Commented:
no, I`m referring to this specific cone, witch I tested in a completely new project; I just added a texbox on the default form. (no subclassing or anything...)
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)  
   If KeyCode = vbKeyDelete Then  
        KeyCode = 0  
    End If  
  
End Sub  
  
Private Sub Text1_KeyPress(KeyAscii As Integer)  
    If KeyAscii = vbKeyBack Then  
        KeyAscii = 0  
    End If  
End Sub

Open in new window

Author

Commented:
Sorry, missed the code snippet on Mobile EE!

It works very well. Thanks!

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial