$ACCELERATOR directive

Purpose: The $ACCELERATOR directive is valid only in BCX programs which use the GUI statement. The $ACCELERATOR directive should be placed immediately preceding the GUI statement.


 Syntax:

 $ACCELERATOR hAccTable

 Parameters:

  • hAccTable Handle to the accelerator table dimensioned as data type HACCEL. This handle must be dimensioned as a GLOBAL. See below for the recommended layout of the code.

Here is the recommended layout for the $ACCELERATOR directive


 $ACCELERATOR FooBloo

 GUI "MyBloof"

 DIM FooBloo AS HACCEL '<<<< -- This is mighty important
 
 DIM Form1 AS CONTROL
 DIM Button1 AS CONTROL
 DIM Edit1 AS CONTROL

 SUB FORMLOAD
 .
 .
 END SUB

 BEGIN EVENTS
 END EVENTS

The C code generated with this directive is


while(GetMessage(&Msg,NULL,0,0))
 {
  if(!IsWindow(GetActiveWindow())          ||
  !IsDialogMessage(GetActiveWindow(),&Msg) ||
  !TranslateAccelerator(GetActiveWindow(),hAccelTable,&Msg))
   {
     TranslateMessage(&Msg);
     DispatchMessage(&Msg);
   }
 }

Example:

Below, the $ACCELERATOR directive is used to facilitate the capture of a keypress for menu items.

 
 $ACCELERATOR CustomKeys
 
 GUI "ACCELERATOR", PIXELS
 
 CONST   ID_Edit     =  200   'Main Menu member
 CONST   ID_Options  =  300   'Main Menu member
 CONST   ID_Open     =  400   'File Menu member
 CONST   ID_Close    =  500   'File Menu member
 CONST   ID_Save     =  600   'File Menu member
 CONST   ID_SaveAs   =  700   'File Menu member
 CONST   ID_Exit     =  800   'File Menu member
 
 GLOBAL  Form1      AS  HWND
 GLOBAL  MainMenu   AS  HMENU
 GLOBAL  FileMenu   AS  HMENU
 GLOBAL  CustomKeys AS  HACCEL
 
 SUB FormLoad
    DIM RAW Caption$
 
    SET sAccel[] AS ACCEL
       FCONTROL OR FVIRTKEY, ASC("O"), ID_Open,
       FCONTROL OR FVIRTKEY, ASC("S"), ID_Save
    END SET
 
    CustomKeys = CreateAcceleratorTable(sAccel, 2)
 
    '========================================================================
    Caption$ =  "ACCELERATOR Demo"
    '========================================================================
 
    Form1 = BCX_FORM(Caption$, 6 * 1.75,18 * 1.71,160 * 1.80,100 * 2.20)
 
    '========================================================================
    '                          Start Building Menus
    '========================================================================
    MainMenu   =  CreateMenu()   ' CreateMenu returns a MENU HANDLE
    FileMenu   =  CreateMenu()   ' CreateMenu returns a MENU HANDLE
    '========================================================================
    '                     Build the File Menu First
    '========================================================================
    AppendMenu(FileMenu,MF_STRING   ,ID_Open  ,"&Open" + CHR$(9) + "Ctrl+O")
    AppendMenu(FileMenu,MF_STRING   ,ID_Close ,"&Close")
    AppendMenu(FileMenu,MF_STRING   ,ID_Save  ,"&Save" + CHR$(9) + "Ctrl+S")
    AppendMenu(FileMenu,MF_STRING   ,ID_SaveAs,"Save &As")
    AppendMenu(FileMenu,MF_SEPARATOR,    0    ,"")
    AppendMenu(FileMenu,MF_STRING   ,ID_Exit  ,"E&xit")
    '========================================================================
    '                        Build the Main Menu Next
    '========================================================================
    AppendMenu ( MainMenu , MF_STRING , ID_Edit    , "Edit")
    AppendMenu ( MainMenu , MF_STRING , ID_Options , "Options")
    '========================================================================
    '                  Attach the File menu to the main menu
    '========================================================================
    InsertMenu ( MainMenu, ID_Edit , MF_POPUP , FileMenu ,"File")
    '========================================================================
    SetMenu(Form1,MainMenu)              ' Activate the menu
    CENTER (Form1)
    SHOW   (Form1)
 END SUB
 
 
 
 BEGIN EVENTS
    SELECT CASE CBMSG
       '========================================================================
       CASE WM_COMMAND
       '========================================================================
       SELECT CASE CBCTL
          CASE ID_Exit : PostQuitMessage(0)
          CASE ID_Open : MSGBOX "Open"
          CASE ID_Save : MSGBOX "Save"
       END SELECT
       
    END SELECT
 END EVENTS