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
Related
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 can I do a shortcut for a buttons so I can press it if I am in my desktop? I've already tried:
If e.Keycode = Keys.F2 Then
Stopclick.PerformClick()
End if
End Sub
I'm guessing that you mean you want to capture a key press when you minimize to your desktop or use a different program. The term that may help you out in your search is called Global Hotkeys. Basically what you need to do is register a hotkey globally within Windows so that when your application is minimized or no longer has focus, it is still able to intercept the messages.
You can search Google for '.Net Global Hotkeys'. You can also check out this link: http://www.vbforums.com/showthread.php?672702-Register-Global-HotKeys
If you are simply trying to catch the F2 event on your form, then try the following code:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData as Windows.Forms.Keys) As Boolean
If keyData = Keys.F5 then
'Take action here
End If
Return MyBase.ProcessCMDKey(msg, keyData)
End Function
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.
I need to subclass a VB6 form so that if the user hits the [ALT] key while left clicking the title/caption bar, I can do something custom (show dialog box, file io, whatever).
So far, I have been able to do the subclassing in my NewWndProc() to correctly trap the WM_NCLBUTTONDOWN message when wParam = HTCAPTION, but I don't know how to trap the [ALT] key at the same time.
I have used the SPY utility a bit to check for messages, but I still can't solve this. Thanks for any help.
Update:
Looks like I may have to use mouse and keyboard hooks?
As it's not sent as part of the message. but you can call GetKeyState(VK_MENU) to get whether it's pressed or not.
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