Click to See Complete Forum and Search --> : How to assign more than one keyboard shortcut to a menu


Michael Hartmann
February 7th, 2000, 07:36 AM
Hello VB6-cracks,

I hope somebody can help me with a very big problem. I need the ability to assign more than one keyboard shortcut to a menu entry. With the standard methods of VB6 I assign e.g. Strg+N to a menu "File-New". Now I need the ability to assign another shortcut to the menu File-New e.g. F5 or something like that. In VC++ this is no problem, there I can define accelerator-keys in the resource file. But in VB6 this resource type is not supported (maybe in VB7 ???). I hope that somebody could help me with this crazy thing. I hope this feature is available anyway.

With best regards

Michael Hartmann

Lothar Haensler
February 7th, 2000, 07:49 AM
you can respond to (almost) any key combination by trapping the keyPress or keyDown events at the form level.
Set the keyPreview property of your form to true and trap these events at the form level.
That way you can respond to these events, there won't be any visible cues to the user in your menu, though.

Michael Hartmann
February 7th, 2000, 08:56 AM
That method didn't work, because I have a MDI-Window with a menubar. But, unfortunately, a MDI-Window can't receive KeyDown or KeyPress events. For all other forms this would be the way to do this.

Best regards

Michael Hartmann

Lothar Haensler
February 7th, 2000, 08:59 AM
in that case you could try the RegisterHotkey API call.
You'll need to subclass your mDIparent to get the WM_HOTKEY message, though.

Michael Hartmann
February 7th, 2000, 09:51 AM
I think this would work, but I have some problems to subclass my MDI form. I looked into the MSDN Library, how to hook into the messaging system of WIndows. There I found the following things:

When my MDI form loads I call the following:

lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
IsHooked = True

lpPrevWndProc is a global defined long variable,
IsHooked is a global defined boolean variable to avoid double hooking. gHW is also a global defined long variable and GWL_WNDPROC is a global defined constant which has the value -4 (for whatever this stands)
WindowProc is a function which is defined as follow:

Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lparam As Long) As Long

Debug.Print "Message: "; hw, uMsg, wParam, lparam
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lparam)
End Function


When I unload the MDI form I do the following:

Dim temp As Long

temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
IsHooked = False

And I call the RegisterHotKey API after I've hooked into the massaging system with the following statement:

Dim iRet as Integer
iRet = RegisterHotKey(mnuResultNew.Parent.hWnd, &H1000, 0, VK_F2)

(I want to assign the F2 key the menu mnuResultNew).

I start the program and nothing happens when I press the F2 key. What do I make wrong. Do I have something forgotten ?

With best regards

Michael Hartmann

Aaron Young
February 7th, 2000, 10:04 AM
You're subclassing your MDI's Messages, but you aren't Checking for the WM_HOTKEY Message and performing the appropriate Action, ie.

Function WindowProc(byval hw as Long, byval uMsg as Long, byval wParam as Long, byval lparam as Long) as Long
If uMsg = WM_HOTKEY then
'Recieved a HOTKEY KeyStroke..
'wParam will Contain the ID of the HotKey Pressed as You Assigned it when Creating the HotKey
Select Case wParam
Case 1
MDIForm1.mnuFileNew_Click
End Select
End If
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lparam)
End Function




Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com

Michael Hartmann
February 8th, 2000, 01:55 AM
It still doesn't work. I added the routine to check if uMsg is WM_HOTKEY and i set a breakpoint to the if statement. I never come to this point of the program. So I tryed to play a little with the value of gHW. I set it to MDIForm.hWnd. Then I come to the point of checking on message WM_HOTKEY, but this event never occure, but when I close my program I get an exception fault in kernel32.dll. So, can anybody tell me how to set correctly this value gHW in the SetWindowLong function, to avoid these program crashes and to receive the WM_HOTKEY event.

Best regards

Michael Hartmann

Crazy D
February 8th, 2000, 02:12 AM
Maybe not the nicest solution but it never fails...
Either make a function in a seperate module, and call that from every form_keydown event, or change the keydown event of the mdi window to Public and call that in every keydown...Not as nice as a hotkey, but hey it does work :-)

Crazy D :-)
"One ring rules them all"