VB6 KeyDown Fails to Cancel the Keypress - vb6

I'm trying to detect when the user presses certain keys in a text box and then I want to increase or decrease the value in the text according to their keypress and cancel out the key they pressed. The cancel part isn't working. I'm accustomed to just putting in KeyCode = 0 if I want to cancel their keypress, like I do in MS Access. However, this isn't working for me in VB6. The plus sign gets into the textbox.
Any suggestions?

I'd say ignore the KeyDown event and just use the KeyPress event:
Private Sub RTB_KeyPress(KeyAscii As Integer)
' "Cancel" the keystroke
KeyAscii = 0
End Sub
The best way to do what you're doing, though, would be to set your form's KeyPreview property to True, and then add a handler for the form's KeyPress event - this means the form gets a chance to handle any keystrokes first. By handling it here and cancelling as above, you can change the text in the textbox however you like.
I used Visual Basic for the first 10 years of my career, but I had to google for the above code. I recommend at least moving on to VB.NET - sometimes you can even import VB6 projects into .NET (sometimes).

Private Sub Text1_KeyUp(KeyCode As MSForms.ReturnInteger, Shift As Integer)
If KeyCode = 27 Then
'CONDITION
End If
End Sub

Related

VB6: How do I force the cursor into a textbox but release for button press

I asked a similar question here: How to delay the LostFocus Event in VB6. It was suggested to use DoEvents, which didn't work for me because it was reloading all my forms.
What I need to do is to force the cursor into a textbox for a barcode read. However, I need to allow the user the ability to click 1 of 3 buttons on the form. So, what needs to happen is, possibly on hover over one of these buttons, release the cursor long enough to click a button.
Can this be done?
Here is a way of accomplishing what you need. In the button Click events, do the needed work and then SetFocus back to the textbox. You will not respond to the LostFocus event as in the previous post.
Option Explicit
Private Sub Text1_LostFocus()
'do nothing
End Sub
Private Sub Command1_Click()
'do your work
Text1.SetFocus
End Sub
Private Sub Command2_Click()
'do your work
Text1.SetFocus
End Sub
Private Sub Command3_Click()
'do your work
Text1.SetFocus
End Sub
Keep in mind that the textbox won't gain focus until the work is complete.

Is it possible to prohibit pasting the clipboard's textual content into a TextBox control?

If the user tries to paste copied text via Ctrl-V, I can invoke KeyPress:
Private Sub cTextBox_KeyPress(KeyAscii As Integer)
If KeyAscii = 22 Then '22 is Ctrl-V.
Beep
KeyAscii = 0
End If
End Sub
But what do I do when she uses the (admittedly less common) Shift-Insert combination? KeyPress won't receive this key. KeyDown and KeyUp don't let us cancel the input.
Is sub-classing the only possibility?
And what about the option 'Paste' in the popup menu? Can I get rid of it by setting a windows style in SetWindowLong? Which?
Perhaps you could set a check in the textbox's Change event to compare to clipboard contents. If they match, disallow the change (i.e., set to prior value, default, blank, etc.)
The advantage of this approach is that it is independent of the numerous methods of pasting clipboard content.

How do I prevent a double-click on an Open File dialog from registering a click on the form under it?

I have a custom control that's essentially a drawing canvas, and a program that uses it for editing files. When a new file is opened, though, something very strange can happen.
If the user double-clicks on the file in the Open File dialog (standard TOpenDialog control) instead of selecting an item and hitting ENTER, the canvas underneath registers a click event and ends up executing a draw action at the position of the cursor immediately after loading is complete.
Obviously this is not the intended behavior for this. I've noticed before that when you double-click the mouse, the double-click message arrives before the second click message. I think the dialog box might be closing from the double-click, and then the second click message arrives and gets sent to whatever's at the appropriate coordinates now that it's gone.
Is there any way I can make this stop happening? I can't tell my code "after loading, just eat the next click," because it could have been opened with the 'ENTER' key instead, and then it would miss the first legitimate click. Can anyone think of a better way to handle this? (Using Windows 7, in case it makes a difference.)
If there's a "second click message," there's something wrong. (For one thing, Windows doesn't have "click" messages, just mouse-up and mouse-down messages.) A double click goes like this: mouse down, mouse up, double click, mouse up. The dialog disappears between the double-click message and the second mouse-up message. If your control receives the mouse-up message and treats it as a full click, then that explains the issue and you need to stop; a click is always a pair of mouse-down and mouse-up messages. If you haven't gotten both, then it's not a click.
In fact, it is The 2nd mouse-up event got fired on the picture box, which leads to the event handler invocation. This seems an OpenFileDialog bug. Need to add check for the IsMouseCaptured for the mouse up event, one click is mouse down and mouse up, instead of only a mouse up.
I solved it that way (it is of course an work-around):
CFileDialog my_file_dialog(...);
if ( my_file_dialog.DoModal()!=IDOK )
return;
CString fileName= my_file_dialog.GetPathName();
//...
CSelectItemsDlg dlg;
// Avoid that the double-click on the CFileDialog sends the WM_LBUTTONUP message to the next window causing the Unselect of an item that is under the mouse cursor.
// http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_10287063.html#a2476475
MSG msg;
while(PeekMessage(&msg,0,WM_LBUTTONUP,WM_LBUTTONUP,PM_REMOVE));
int DoModalRes = dlg.DoModal();
And you can believe that I have put a big smile on the face of my boss :)
Private IsMouseDown As Boolean
Private Sub picNenIn_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles picNenIn.MouseDown
IsMouseDown = True
'Code
End Sub
Private Sub picNenIn_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles picNenIn.MouseMove
If IsMouseDown Then
'Code
End If
End Sub
Private Sub picNenIn_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles picNenIn.MouseUp
IsMouseDown = False
'Code
End Sub

How can I capture key ups/downs no matter what control on my form is the target?

I want to capture ctrl/alt/etc key ups and downs, no matter which control on my form gets the keyup or keydown event. Since I have about 100 controls on my form, it would be really ugly if I were to add code to each individual control. How can I accomplish this without having to do that?
PS: What's the difference between SetWindowsHook and SetWindowsHookEx?
You need to set the KeyPreview property of each Form to True. Subsequently, you can catch the keyboard events at the form level, in addition to the individual control level:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "Form_KeyDown"
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Debug.Print "Form_KeyPress"
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print "Form_KeyUp"
End Sub
Essentially, the form gets a "preview" of each keyboard event before the control, e.g.
Form_KeyDown
Control_KeyDown
Form_KeyUp
Control_KeyUp
As for SetWindowsHook & SetWindowsHookEx, the former is the original Win16 API call, and the latter is the Win32 and Win64 API call. SetWindowsHook is deprecated, and isn't in the current MSDN library, as far as I know.

Disable HotKeys When Typing - VB6

In my VB6 program, I have tons of hotkeys such as X, A, D... ETC . I also have a chat system in it, where everytime I use the characters X or A it will do the actions of those hotkeys. For example, if X was to close the application (not that it really does), when I am typing "fiXing" into my chat textbox, it will close the application. Can anyone tell me how to disable the hotkeys when typing EXCEPT the Enter Key?
thanks,
Kevin
In the chat TextBox's GotFocus event set a flag to disable your hotkeys. Then re-enable them in the TextBox's LostFocus event.
I don't know how you trap your hotkeys, but the code to set the flag is pretty simple:
Private suppressHotkeys As Boolean
Private Sub txtChat_GotFocus()
suppressHotkeys = True
End Sub
Private Sub txtChat_LostFocus()
suppressHotkeys = False
End Sub
Then in the code that traps the hotkeys, just check the flag:
If (Not suppressHotkeys) Then
//process hotkey
End If
It would probably be better to use a key combination for your hot keys. It is more common to press say Ctrl+X or Alt+X. You would test for them in either the KeyDown or KeyUp events.
If KeyCode = vbKeyX And (Shift And vbCtrlMask = vbCtrlMask) Then
' Do something
End If

Resources