code for an <ALT + R> key

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I would like to put code behind some <alt> keys in a form. I am assuming I
can put it on the Key_down event. Then I should check KeyCode like:
If KeyCode = vbKeyTab then
Do something
End if

I can't seem to find what to compare KeyCode to for an Alt + R. Are there
any gotcha's when doing this?

Thanks,
Steve
 
Attempting to do it without cheating a bit will, I think, be a lot of
trouble. You could play with KeyPreview type stuff but I'm not sure you'll
get a consistent effect depending on which control you have active at the
time you hit the key combination.

If I was doing it then the way that I'd cheat would be to add a command
button. Set it's caption property to be &R. Then, whenever you hit Alt+R,
it's click event will fire. (That's the general way you'd set a button to
have a shortcut by the way... entering a caption of Prin&t will underline
the t and make it respond to Alt+T)

That's nice and straightforward if you're triggering buttons that you want
displayed on the form. But if you don't want it to be seen then you can't
just make the button invisible (or the code doesn't fire). You can set it
to 0 width and 0 height but it'll still show a bit after it's run (when it
gets the focus). I've just had a quick plan and the only way that I can get
it disappear completely is to put a solid rectangle over the control, and
then switch the visibility of that off and back on at the end of the code.
 
Rob,
I do have a button that does this also. My goal is to get the ALT + R to
fire it's click event. So we are on the right track. when I make the
caption property to be &amp:R, on the button, it says, amp;R with the a
underlined, and I have to hit ALT + A to get it to fire. Can I get it to
just say a word with the R underlined and ALT + R work?

Thanks
 
Ah Ha.
I set the caption property to &Rob, it underlines the R and Alt+R fires the
on click.

Thanks!
 
Hi Steve,

If possible I'd do it with command buttons with their accelerator keys
set to the letters I wanted to capture as Alt+R etc. If necesssary the
the buttons could be hidden behind something else on the form.

Otherwise, the key codes for KeyUp and KeyDown are documented in the
help topic "Keycode Constants"; codes for the letter keys are the same
as the ASCII code for the uppercase letter, so R is 82.

To check for Alt in addition to a letter, you have to read the Shift
argument of the event procedure, e.g.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = acAltMask Then 'Alt is pressed but not Ctrl or Shift
Select Case KeyCode
Case 82 'R
'Do something
Case 83 'S
'Do something else
Case Else
'Do nothing
KeyCode = 0
End Select
End If
End Sub
 

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

Back
Top