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

Define Shortcut Keys in a Form 1

Status
Not open for further replies.

Braveman

Programmer
Jun 4, 2005
3
US
Hey guys.
I would like to have shortcut keys in form, for example, CTRL+B will call a procedure or a function I define.
The only way I\'ve found to do so is to define a shortcut key to a menu.
The problem is, I don\'t want to have a menu in my form, and when I\'m setting the menu Visible property to False the short-cut key no longer works.
Do you have any idea on how can I create a shortcut from anywhere in the form without menu or how to make it work even when the menu is invisible?
Thank you very much.
 
Have a look at Hypetia's Hotkey code in thread222-607295
 
And if you want your shortcut to work only with your own form, try this simpler code.
___
[tt]
Private Sub Form_Load()
KeyPreview = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyB And Shift = vbCtrlMask Then
MsgBox "You pressed Ctrl+B."
End If
End Sub[/tt]
 
First of all, thank you strongm and Hypetia.
Hypetia - your code will work only of there is no control in the form. For instance, if the focus is on a TextBox your code wouldn\'t work.
Stringm, is this the simplest way of doing it?
Do you know other ways?

Thanks again.
 
>Hypetia - your code will work only of there is no control in the form.

Did you actually try it? It will work in presence of other controls as well. Setting the KeyPreview property in Form_Load ensures this.

See the KeyPreview property in VB help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top