Results 1 to 13 of 13

Thread: [RESOLVED] Invisible Menu Hotkeys

  1. #1

    Thread Starter
    Member Mephy's Avatar
    Join Date
    Dec 2005
    Posts
    52

    Resolved [RESOLVED] Invisible Menu Hotkeys

    Is there a way to have hotkeys for a menu that's not visible? I tried but they don't work for some reason

  2. #2
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Invisible Menu Hotkeys

    I don't think there is a way to do it using the menu editor but you can do it through code.

    VB Code:
    1. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    2. If Shift = vbCtrlMask And KeyCode = vbKeyA Then
    3.     mnuItem_Click
    4. End If
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8. Me.KeyPreview = True
    9. End Sub
    10.  
    11. Private Sub mnuItem_Click()
    12.     MsgBox "Click"
    13. End Sub

  3. #3

    Thread Starter
    Member Mephy's Avatar
    Join Date
    Dec 2005
    Posts
    52

    Re: Invisible Menu Hotkeys

    What if I'm using a MDI Form (No _KeyUp event) :-\

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Invisible Menu Hotkeys

    Quote Originally Posted by Mephy
    What if I'm using a MDI Form (No _KeyUp event) :-\
    In that case maybe you could use GetAsyncKeyState API with a Timer.

  5. #5
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Invisible Menu Hotkeys

    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.

  6. #6

    Thread Starter
    Member Mephy's Avatar
    Join Date
    Dec 2005
    Posts
    52

    Re: Invisible Menu Hotkeys

    I added the following code to a MDI Child and nothing happened.
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. MsgBox KeyCode
    3. End Sub
    4.  
    5. Private Sub Form_KeyPress(KeyAscii As Integer)
    6. MsgBox KeyAscii
    7. End Sub
    8.  
    9. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    10. MsgBox KeyCode
    11. End Sub

  7. #7
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Invisible Menu Hotkeys

    You must have Keypreview property set to true (of the form) for the form to get the keys pressed.

  8. #8

    Thread Starter
    Member Mephy's Avatar
    Join Date
    Dec 2005
    Posts
    52

    Re: Invisible Menu Hotkeys

    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

  9. #9

    Thread Starter
    Member Mephy's Avatar
    Join Date
    Dec 2005
    Posts
    52

    Re: Invisible Menu Hotkeys

    Not sure if your allowed to bump posts but this one's about to get deleted and i still need a little help

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Invisible Menu Hotkeys

    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:
    1. Option Explicit
    2.  
    3. Private Declare Function GetAsyncKeyState Lib "user32" _
    4.     (ByVal vKey As Long) As Integer
    5.  
    6. Private Sub ExecuteSomething() 'Now the code is in this sub
    7.     MsgBox "Ok!"
    8. End Sub
    9.  
    10. Private Sub MDIForm_Load()
    11.     Timer1.Interval = 1
    12.     Timer1.Enabled = True
    13. End Sub
    14.  
    15. Private Sub Timer1_Timer()
    16.     If CBool(GetAsyncKeyState(vbKeyControl)) And CBool(GetAsyncKeyState(vbKeyA)) Then
    17.         ExecuteSomething
    18.     End If
    19. End Sub

  11. #11
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Invisible Menu Hotkeys

    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:
    1. 'In form1
    2. 'Removed the Private Sub ... and left it as Sub.... only
    3. Sub mnuitem_Click()
    4.     MsgBox "Click"
    5. End Sub
    6.  
    7. 'In form2
    8. Private Sub Command1_Click()
    9.     Call Form1.mnuitem_Click
    10. End Sub

  12. #12
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Invisible Menu Hotkeys

    Quote Originally Posted by Andrew G
    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:
    1. 'In form1
    2. 'Removed the Private Sub ... and left it as Sub.... only
    3. Sub mnuitem_Click()
    4.     MsgBox "Click"
    5. End Sub
    6.  
    7. 'In form2
    8. Private Sub Command1_Click()
    9.     Call Form1.mnuitem_Click
    10. 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.

  13. #13
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Invisible Menu Hotkeys

    oops. Sorry by Form2 i meant the mdi child and form1 is the MDI form

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width