Creating 'hot-keys' on a form

tfurnivall

Registered User.
Local time
Today, 04:54
Joined
Apr 19, 2012
Messages
81
Hi!

I have a tab control on a form, and I want to use "Hotkeys' to get from one page to another (or more specifically, to toggle the visibility of the pages).

So, I set up the tab control with the pages I want hidden set with visible=No. Then I enable the Form.KeyPreview, so that the form will get a chance to look at all the key presses.

Lastly I have a Form.KeyDown handler, that looks like this:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

MsgBox "Key pressed (1): Shift value is " & Format(Shift)
'   Detect Hot-keys for the pages in the MultiPage wizard, and make them Visible or not visible
'   P/D/X/S/R/H/C
If Shift = acAltMask + acShiftMask Then
   msgbox "Key pressed (2): Shift value is " & format(shift)
   If KeyCode = vbkeyP Then         '   Project Definition
      '     Can't be hidden!
   ElseIf KeyCode = vbkeyD Then     '   Source Definition
      Me.pgSourceDefinition.Visible = Not Me.pgSourceDefinition.Visible
   ElseIf KeyCode = vbkeyX Then     '   Extract Phase
      Me.pgExtract.Visible = Not Me.pgExtract.Visible
   ElseIf KeyCode = vbkeyS Then     '   Scoring Phase
      Me.pgScoring.Visible = Not Me.pgScoring.Visible
   ElseIf KeyCode = vbkeyR Then     '   Review Phase
      Me.pgReview.Visible = Not Me.pgReview.Visible
   ElseIf KeyCode = vbkeyH Then     '   Project History
      Me.pgHistory.Visible = Not Me.pgHistory.Visible
   ElseIf KeyCode = vbkeyC Then     '   Project Configuration
      Me.pgConfigure.Visible = Not Me.pgConfigure.Visible
   Else
   End If
Else
End If

End Sub
This is early on in the design iteration - more will happen with the pages, but this is an easy way of reviewing various aspects of a project.

So what happens?

I put a breakpoint at the first If statement, and sure enough, it picks up the Ctrl key or the Alt key, whenever they are pressed. (I need to use the mouse to clear the msgbox, naturally!) When I press both of them (Ctrl/Alt) I get the required value of 6, but I never seem to get to the second msgbox. In addition, if I comment out the first message box, I also never seem to get to the second msgbox (ie the point where the combination has been detected.

KeyDown obviously has to fire for each component of a HotKey combination, and the Shift parameter has been shown to be cumulative. The only thing I can think of is that somehow I need to turn off keypress processing somewhere else (Used to be possible to use Cancel to do this, I seem to recall).

Any ideas on how I can implement these Hotkey like functions? I think my approach is sound - but I'm more than willing to learn why if it is not!

Thanks,

Tony
 
I don't know much about KeyCode, but I was looking at MSDN, it showed something like..
Code:
If [B]Shift And 7 = 6[/B] Then
Or even
Code:
If Shift = [B]vb[/B]AltMask + [B]vb[/B]ShiftMask Then
Sorry if this is pointless, thought you might give this a try..
 
Not pointless! Thanks for your response.

The snippet you quote is simply another way of testing if the value of Shift is 6.

More accurately it tests to see if both Ctrl and Alt are being pressed. (It is possible to have not only those two keys but also the Shift key pressed, which would yield a Shift parameter value of 7 - easy way to get carpal tunnel contorting your hands like that!) My code tests to see that only Ctrl and Alt are pressed (no Shift key anywhere).

My underlying problem - I think - is what happens to all the KeyDown events before we get to deciding that both Ctrl and Alt are pressed, and now some other key is pressed? I'd be happy for the event to die naturally, but I guess something, somewhere is also going to see it...

Tony
 
Hot keys update!

I have changed the msgbox to a debug.print, and get the following:
Code:
Key pressed (1): Shift value is 2; KeyCode is 17()
Key pressed (1): Shift value is 6; KeyCode is 18()
Key pressed (1): Shift value is 6; KeyCode is 18()
Key pressed (1): Shift value is 6; KeyCode is 18()
Key pressed (1): Shift value is 6; KeyCode is 68(D)
Key pressed (1): Shift value is 6; KeyCode is 83(S)
Key pressed (1): Shift value is 6; KeyCode is 88(X)
Key pressed (1): Shift value is 6; KeyCode is 82(R)
Key pressed (1): Shift value is 6; KeyCode is 72(H)
Key pressed (1): Shift value is 6; KeyCode is 67(C)
but even though this meets the conditions for changing the visibility of the pages, nothing happens...

Code now reads:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Debug.Print "Key pressed (1): Shift value is " & Format(Shift) & "; KeyCode is " & KeyCode & "(" & Chr(KeyCode) & ")"

'   Detect Hot-keys for the pages in the MultiPage wizard, and make them Visible or not visible
'   P/D/X/S/R/H/C
If (Shift = acAltMask + acShiftMask) And KeyCode = vbkeyD Then
      Me.pgSourceDefinition.Visible = Not Me.pgSourceDefinition.Visible
ElseIf (Shift = acAltMask + acShiftMask) And KeyCode = vbkeyX Then     '   Extract Phase
      Me.pgExtract.Visible = Not Me.pgExtract.Visible
ElseIf (Shift = acAltMask + acShiftMask) And KeyCode = vbkeyS Then      '   Scoring Phase
      Me.pgScoring.Visible = Not Me.pgScoring.Visible
ElseIf (Shift = acAltMask + acShiftMask) And KeyCode = vbkeyR Then     '   Review Phase
      Me.pgReview.Visible = Not Me.pgReview.Visible
ElseIf (Shift = acAltMask + acShiftMask) And KeyCode = vbkeyH Then     '   Project History
      Me.pgHistory.Visible = Not Me.pgHistory.Visible
ElseIf (Shift = acAltMask + acShiftMask) And KeyCode = vbkeyC Then     '   Project Configuration
      Me.pgConfigure.Visible = Not Me.pgConfigure.Visible
Else
End If

End Sub
Must be something real simple, yet real not obvious (at least to me ;-)

T
 

Users who are viewing this thread

Back
Top Bottom