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

Trying to Capture Ctrl+RightArrow 1

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
US
I'm trying to capture Ctrl+RightArrow and Ctrl+RightArrow but nothing happens. If I test for the Ctrl Key also it fires, so I know the keyDown event is being triggered.

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim CtrlDown As Integer

   CtrlDown = (Shift And vbCtrlMask) > 0
   
   If CtrlDown And KeyCode = vbKeyRight Then
      MsgBox "Ctrl+RightArrow pressed."
   ElseIf CtrlDown And KeyCode = vbKeyLeft Then
      MsgBox "Ctrl+LeftArrow pressed"
   elseif CtrlDown Then
      MsgBox "Ctrl Key pressed" 
   End If
End Sub

I also have the KeyPreview = True.

Can anyone tell me what I need to do?

dpdoug
 
I think I remember another post like this, I believe the problem is that when you test for key1 and key2, to get it to fire you pretty much have to press them at the same instant, I regretfully forgot the fix, but I know it's here, so a little searching might be worth it.
 
Hi,

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

CtrlDown = (Shift And vbCtrlMask) > 0
If CtrlDown Then
If KeyCode = vbKeyRight Then
MsgBox "Ctrl+RightArrow pressed."
ElseIf KeyCode = vbKeyLeft Then
MsgBox "Ctrl+LeftArrow pressed"
End If
End If
End Sub

Have a good one!
BK
 
In the line ...

CtrlDown = (Shift And vbCtrlMask) > 0

What is the purpose of the " > 0" ?

The code seems to work without it.
 
Hi,

vbrocks: There is no need for it but I didn't feel like hitting the backspace key. <g>

Have a good one!
BK
 
Unfortunately, that one doesn't work either.
Hey Fizzy, did you ever find that fix?

 
This works reasonably well
Code:
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
   Dim CtrlDown As Boolean

    CtrlDown = CBool(Shift And vbCtrlMask)
    If CtrlDown Then
         If KeyCode = vbKeyRight Then
             MsgBox &quot;Ctrl+RightArrow pressed.&quot;
         ElseIf KeyCode = vbKeyLeft Then
             MsgBox &quot;Ctrl+LeftArrow pressed&quot;
         End If
    End If
    End Sub

Main alterations are using a boolean type (probably not major one)
but using it in the KeyUp event seems ok



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Fizzy007, I think you are probably thinking of my code in thread222-580996. Here's a modificed version for this thread:
[tt]
Private Declare Function GetAsyncKeyState Lib &quot;user32&quot; (ByVal vKey As Long) As Integer

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = vbKeyControl Or KeyCode = vbKeyRight) And (GetAsyncKeyState(vbKeyRight) And GetAsyncKeyState(vbKeyControl)) = -32768 Then
MsgBox &quot;CTRL RightArrow pressed&quot;
End If
End Sub
 
I tried this too, but it doesn't work. I put a break point there and stepped through...

The problem is that it fires when you press the Ctrl Key and doesn't wait to get the key combination.

Another thing is it doen't even detect just the right or left arrows by themselves. It may be because they are navigation keys and are reserved, just a guess.

dpdoug

 
Odd. Strongm's code as posted works fine for me.

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
dpdoug, with the code you have posted, you cannot do this and expect it to work.

When the Ctrl key is pressed, the KeyDown fires immediately, before you even press the second key.
Therefore, your
elseif CtrlDown Then
will always fire.

The KeyDown event is NOT a mind reader and doesn't know if you are going to also press the arrow key, or any other key at all.

The only way around this is using the KeyUp event, but that isn't fired until you release the second arrow key first. And, released in the wrong order doesn't work. The arrow key MUST be released first.

So that is maybe not workable either.

If I understand right, you want to capture JUST the Control key being pressed, sometimes, and the Control AND Arrow keys at other times.

Well, this isn't effectively possible.

You'll have to decide what you want to capture: The Ctrl key only, or the Ctrl key in combination with another key.
The Ctrl is intended to be used in a key combination only, and not alone.
 
What about this ...

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 2 Then Debug.Print &quot;ctrl key pressed&quot;
If Shift = 2 Then If KeyCode = vbKeyRight Then MsgBox &quot;ctrl and rightarrow pressed&quot;
End Sub
 
Or this .....

Option Explicit

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 2 Then If KeyCode = vbKeyRight Then MsgBox &quot;ctrl and rightarrow pressed&quot;

End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyControl Then MsgBox &quot;ctrl key pressed&quot;
End Sub
 

From what I understand, dpdoug does not wants condition one (Shift = 2) executing if condition two (Shift = 2 and KeyCode = vbKeyRight ) exists.
But, if condition two exists in the KeyDown, then so will condition one.

 
In the KeyUp, the Ctrl key will still be captured as I mentioned previously, unless the release is in a certain order.
This is really senseless under the given condition.
Anyways, the standard windows action for Ctrl-RightArrow in an edit box is to move the cursor to the end of the word, and not something else...
 
I think this might work, it works for me .....

Option Explicit

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 2 Then
Static skip As Long
Select Case KeyCode
Case vbKeyRight
MsgBox &quot;Ctrl and Right arrow pressed&quot;
Case vbKeyLeft
MsgBox &quot;Ctrl and Left arrow pressed&quot;
End Select
skip = skip + 1
If skip > 10 And Shift = 2 Then
MsgBox &quot;Control key pressed&quot;
skip = 0
End If
End If
End Sub
 
I just discovered something! Since my app uses the browser control, the built-in functionality of Alt+RightArrow and Alt+LeftArrow does exactly what I want it to do without having to program anything.

Problem solved!

This is a much better solution anyway.

My personal thanks to:
vbrocks
CCLINT
Fizzy007
strongm
BlackKnight
mattKnight
AndyWatt

Thanks so much for all the time you all spent on my problem!

dpdoug

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top