how to bring up a hidden button using special keys?

B

Ben

Hi all,

I would like to create a hidden button on a form, but when I only want
it to appears when I press certain keys combination, such as Shift-S-A.
That is shift key with the "S" key and the "A" key. I want to have
it so that I can do occasional system maintenance.

Can you share some thoughts with me?

Thanks,

Ben
 
J

Jack Leach

I would try the KeyPress event, but I'm not 100% on how to check for multiple
keys. You can then use KeyCode to check for the currently pressed key,
though again I'm not sure if this works with multiples. If I'm imagining
this anywhere near correctly, it would resemble something along these lines:

Private Sub Form_KeyPress()
If (KeyCode = blah) And (KeyCode = Blah2) And (KeyCode = Blah3) Then
Me.HiddenButton.Visible = Not Me.HiddenButton.Visible
End If
End Sub

Do a google search on keycodes and you should find a list of the codes for
each key pretty easily.

hth

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
J

John Spencer MVP

You might use something like the following - which traps the control and alt
key being down when the key is pressed

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyS And Shift = acCtrlMask + acAltMask Then
KeyCode = 0
'Show button if not visible, hide button if visible
'You should check and make sure the button does not have the
'focus if you are going to hide it. Or just move the focus
'using set focus before changing the visible property of the
'button
Me.SomeButton.Visible = Not(Me.SomeButton.Visible)
End If
End Sub

You will need to set the form's Key Preview property to True (Yes) if you want
the keystroke to be trapped. I don't know of a good method to trap both the S
and A and Shift being pressed at the same time.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
J

JimBurke via AccessMonster.com

Looks like you're already covered for detecting the Shift-S. If you really
need to capture that followed by A, the only thing I can think of is to use a
boolean variable declared in the form module, call it ShiftSPressed or
something. In the form open event set it to false. Then do something like
this in the Form_KeyUp proc:

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

If ShiftSPressed then
if KeyCode = vbKeyA then 'I'm assuming this would detect the A key
being pressed
Me.SomeButton.Visible = Not(Me.SomeButton.Visible)
KeyCode = 0
End If
ShiftSPressed = False
Else
If KeyCode = vbKeyS And Shift = acCtrlMask + acAltMask Then
KeyCode = 0
ShiftSPressed = True
End If
End If

End Sub

The thing I'm not sure about is 'KeyCode = 0' - I don't know exactly why
that's needed or what that's doing, or where it's needed. Maybe just put it
once at the end of the sub? But I think the rest of the logic should do the
trick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top