Is there a way to have hotkeys for a menu that's not visible? I tried but they don't work for some reason
Printable View
Is there a way to have hotkeys for a menu that's not visible? I tried but they don't work for some reason
I don't think there is a way to do it using the menu editor but you can do it through code.
VB Code:
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) If Shift = vbCtrlMask And KeyCode = vbKeyA Then mnuItem_Click End If End Sub Private Sub Form_Load() Me.KeyPreview = True End Sub Private Sub mnuItem_Click() MsgBox "Click" End Sub
What if I'm using a MDI Form (No _KeyUp event) :-\
In that case maybe you could use GetAsyncKeyState API with a Timer.Quote:
Originally Posted by Mephy
Well i can think of two ways, one is to set a system wide hotkey and use that (except if the program doesn't have focus, and you press the keys it will still 'click' the menu), and the other is to add the code to all the mdichild windows and make it 'click' the menu on the MDI Form.
I added the following code to a MDI Child and nothing happened.
VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) MsgBox KeyCode End Sub Private Sub Form_KeyPress(KeyAscii As Integer) MsgBox KeyAscii End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) MsgBox KeyCode End Sub
You must have Keypreview property set to true (of the form) for the form to get the keys pressed.
Alright, I can get when a key is pressed now, but I'm having trouble calling a Sub. I have the sub Menu1_Click(Index as integer) in frmMain. and i'm trying to call it from Form2
Not sure if your allowed to bump posts but this one's about to get deleted and i still need a little help
Remove the code you have inside that invisible menu Click Event, and add it to a Sub, you can still call that sub in the menu click event if you want.
Now that the code is in a sub you can execute it with keys, this will execute with Control-A:
Add a TIMER control to your MDI Form.
VB Code:
Option Explicit Private Declare Function GetAsyncKeyState Lib "user32" _ (ByVal vKey As Long) As Integer Private Sub ExecuteSomething() 'Now the code is in this sub MsgBox "Ok!" End Sub Private Sub MDIForm_Load() Timer1.Interval = 1 Timer1.Enabled = True End Sub Private Sub Timer1_Timer() If CBool(GetAsyncKeyState(vbKeyControl)) And CBool(GetAsyncKeyState(vbKeyA)) Then ExecuteSomething End If End Sub
jcis, the only problem with that is that if any other program has focus, and the keys are pressed then it will still run the sub.
Either way here is a way to call the sub
VB Code:
'In form1 'Removed the Private Sub ... and left it as Sub.... only Sub mnuitem_Click() MsgBox "Click" End Sub 'In form2 Private Sub Command1_Click() Call Form1.mnuitem_Click End Sub
Ok, but, why that code uses 2 Forms? shouldn't all happend inside the MDI Form? Or in MDI Form the focus is always on a child? I'm not sure cause I never used MDI.Quote:
Originally Posted by Andrew G
oops. Sorry by Form2 i meant the mdi child and form1 is the MDI form