-
how can i make hotkeys for graphical command buttons? I know its easy if i have a standard-command button (i will just have to use the ampersand & command right?) my command button does not use captions but i need to have a hotkey for it. Hope you guys can help me ASAP.
-
You can do this very simply by using a timer and the API GetAsyncKeyState(vkey) function.
Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 100 'interval at which to check keys
End Sub
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyControl) And GetAsyncKeyState(vbKeyC) Then ' check if Ctrl+C was entered
MsgBox "Ctrl+C entered"
End If
End Sub
For some reason there is no vbAlt key code.
You could also do this in a less elegant way by entering your hotkey & in the button's caption as usual then selecting
a font size of 1 - however a tiny dot will still appear on the button.
The other, more complicated but probably best way, is to use the API SendMessage function with the WM_SETHOTKEY command.
But this requires you to subclass the control.
You can find an example of this here: http://www.vbsquare.com/articles/hotkey/
Hope this helps ;)
-
you could also put a label with a description of the button on it , next to the button and the assign the hotkey to the label.
all you then have to do is link , through code , the button and respective label.
but a word of note : unless you are developing the program for some kiddies to use , you should stay away from pics on cmdbuttons , they ruin the look of the app and take up more RAM . Users also get sick of the pictures after using the app for some time.
Trust me , I know from experience
{just a word of advice , use it , abuse it :-) }
-
Railor
Good idea also, but if the label has a tab order just before the button's tab order you don't need any code link.
The label can't be edited and it never receives focus so the next control in the tab order receives it.
And if you make the label so small you can't see it the user won't even know it's there. :cool:
-
Even easier, set the Form's KeyPreview property to True and put this code in the Command1_KeyDown or Form_KeyDown event.
Code:
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyA To vbKeyZ:
MsgBox Chr(KeyCode) & " key pressed."
End Select
End Sub
If any key from A to Z is pressed, than a Msgbox will pop up telling you which key was pressed.
-
Hitcgar
thnaks 4 reminding me , i forgot about that one (label no focus)
I just said that , because we have a program here (where i work) that has labels and cmdbuttons as mentioned b4
we have now removed the pics from the cmdbuttons but still have the labels so that users can use the label kotkey as well as click on the labels
:-)