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

Detecting the Ctrl button + another button pressed... 1

Status
Not open for further replies.

tjtindal

Programmer
Mar 22, 2004
66
US
How can you make Visual Basic 6.0 determine if the Control button is being held while one of the arrow keys is pressed?
 
First do a help search on key codes (they will look like vbKeyReturn, etc.); the keypress event is a good place to trap these as it already returns a keyascii variable. If you aren't comfy with the VB constants, you can refer to keyascii directly with numeric values....

I have great faith in fools; self-confidence my friends call it.
-Poe
 
I'll do that search before I go any further, thanks.
 
Um, not keypress, keydown. Keydown exposes a Shift argument that will tell you if the control key is held down; keypress has no such capability. Now, the Shift argument is an integer value from 0 to 7. If you think about it, 7 is binary 111. If you translate the Shift argument's value from decimal to binary, you will find that it evaluates to three flags. The LSD is shift, then control, and the MSD is Alt. So, 7 means all three keys are down, 2 (010) means the control key is down, 5 (101) means shift and alt are down, and so on.

HTH

Bob
 
Right on Bob! Also I should mention that the keypreview property of the form needs to be set to true.

I have great faith in fools; self-confidence my friends call it.
-Poe
 
Assuming the OP wants consistent behavior throughout the form, that's definitely a good idea.
 
Ya'll lost me on this one with all tech-talk and no example code :(
 
Here is code I added to a form that opens a simple text document; OpenDocument is a user-defined function elsewhere in the form, so this is just an illustration that captures the F1 button when it is pressed, and executes a simple line of code:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode
Case vbKeyF1
OpenDocument "C:\Program Files\DMI\DMI2.txt"
End Select

End Sub


Hope this helps! Sorry if too tech-y - I forget that we all have different skills & tools!

I have great faith in fools; self-confidence my friends call it.
-Poe
 
And for something that might be closer to what you need, I use this to prevent the user from pasting from the clipboard into a form's text boxes. (At times, there are good reasons for doing this.) Note the added penalty of dumping the clipboard's contents, which also has a good reason.
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyV And (Shift And vbCtrlMask) > 0 Then
        KeyCode = 0
        Clipboard.Clear
        MsgBox "You cannot paste ... into the text boxes."
    End If
End Sub
 
Worth pointing out that that only works if you've got a user who doesn't use the original cut and paste key sequences rather than the new-fangled "we'll try and make it a bit like a Mac even though we haven't got the Mac key" version
 
Yeah, and you need to use an API to disable pasting from the context menu, but that wasn't the point. Just wanted to show how to catch a CTRL+someOtherKey. But thanks for making my example complete!
 
<Ya'll lost me on this one with all tech-talk and no example code :(

All right, tj. Do this:

1. Create a new project. Set Form1's keypreview property to true.
2. Add the following code:
Code:
Option Explicit

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Then
    If Shift = 2 Then
        MsgBox "Ctrl Right has been pressed"
    End If
End If
End Sub
Run the project. Press Ctrl-Right.

That should demonstrate the concept.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top