Software Archive
Read-only legacy content
17061 Discussions

Menu Short cuts

davidgraham
Beginner
357 Views
I want to be able to access menu items by pressing a single key on the keyboard - say "M" for a "Move" command, rather than having to select the "Edit" then the "Move" menu items, or instead of using the "Move" button on the tool bar.

How do I trap these keyboard characters in a Windows API project to achieve this?

Thanks,
David
0 Kudos
4 Replies
isn-removed200637
357 Views
It is straightforward to program 'accelerator keys' using Visual Fortran.
Use the Resource Editor to create an 'accelerator table', defining key combinations for frequently-used menu items, and give it a name (e.g.'GENERIC') and then in your WInMain function add
  
character*100 lpszAcceltabname  
!  
lpszAcceltabname="Generic"C

After registering your window class, load your menu and accelerator table resources and create your main window
  
hmenu		= LoadMenu(hInstance, LOC(lpszMenuName))  
haccel		= LoadAccelerators(hInstance, LOC(lpszAcceltabname))  
ghInstance	= hInstance  
  
hWnd = CreateWindow(	lpszClassName,                      &  
                        lpszAppName,                           &  
                        INT(WS_OVERLAPPEDWINDOW),              &  
                        50,                         &  
                        50,                                     &  
                        800,                                   &  
                        575,                                   &  
                        NULL,                                  &  
                        hmenu,                                 &  
                        hInstance,                             &  
                        NULL                                   &  
                        )  
i = ShowWindow( hWnd, SW_SHOWNORMAL)  
ghWnd=hWnd

then start your message loop and include a reference to the TranslateAccelerator function to trap keyboard accelerators
  
!  
! Start a message loop  
  
do while( GetMessage (mesg, NULL, 0, 0) .NEQV. .FALSE.)  
! look for accelerator key-strokes - returns TRUE if key-stroke found and processed  
!  do not process message further if accelerotor key processed  
if (TranslateAccelerator( hwnd, haccel, mesg).EQV. .FALSE.) THEN  
! process message if accelerator not used  
   i =  TranslateMessage( mesg )  
   i =  DispatchMessage( mesg )  
   ENDIF  
  
end do  
  
WinMain = mesg.wParam  
end  

HTH
0 Kudos
Jugoslav_Dujic
Valued Contributor II
357 Views
...just an additional remark -- accelerator IDs from resource accelerator table should be the same as corresponding menu IDs. In this case, a) you don't change any of WM_COMMAND handling code b) Windows automatically disables accelerator when menu item is grayed and vice versa.

Jugoslav
0 Kudos
davidgraham
Beginner
357 Views
Thanks for the reply - I was trying to use WM_KEYUP!

I now have a resource table and have defined lpszAcceltabname - but when I make the call

haccel=LoadAccelerators(hInstance,LOC(lpszAcceltabname))

I get the error -
The specified resource name cannot be found in the image file.

I have not mistyped the accelerator name. Is there something I have missed?

Thanks, David
0 Kudos
davidgraham
Beginner
357 Views
I managed to fix it myself by forcing the resource ID number rather than the resource name:-

haccel= LoadAccelerators(hInstance,MakeIntResource(127))

Thanks, David
0 Kudos
Reply