News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

SetAccels: a simple and flexible use of the CreateAcceleratorTable API

Started by jj2007, March 18, 2024, 01:10:27 AM

Previous topic - Next topic

jj2007

Accelerators have one main advantage: they don't care which window or control has the focus - they simply work.

The new SetAccels macro (MasmBasic version 17 March 2024) offers several options:
- SetAccels F1:0, VK_F2:1 (the name of the key, with or without VK_ : the index): if a key is pressed, its index arrives in an Event Accel through eax, see below
- SetAccels Ctrl X:someprocorlabel (the name of the key:the name of a proc)
- SetAccels Ctrl X:someprocorlabel(123): same but with one immediate argument passed
- SetAccels Ctrl X:someprocorlabel(123:somevar): same but with two arguments passed; the second argument can be a variable set by SetGlobals

Full code below, project attached. Beware of the F12 key :cool:

GuiParas equ "Accel demo", x600, w400, h1000, b LiteBlueGreen, icon Butterfly
include \masm32\MasmBasic\Res\MbGui.asm
  GuiControl MyEdit, "edit", font -16:FW_SEMIBOLD
  GuiControl MyStatus, "statusbar", "Press Ctrl O to open a file"
  SetGlobals OpenTitle$="Open a text file:", SaveTitle$="Save this file:"
  SetGlobals    ; assign values to global variables
  SetAccels F1:0, VK_F2:1, Ctrl O:OpenOrSave(1:OpenTitle$)
  SetAccels Ctrl S:OpenOrSave(0:SaveTitle$), Ctrl X:NoArgs

Event Accel
  SetStatus$ Choose$(eax, "You pressed F1", "That was F2")

EndOfEvents    ; vvv procs below the events vvv
OpenOrSave proc mode, os$
  mov ecx, os$
  .if mode
    .if FileOpen$("Asm sources=*.asm|Includes=*.inc|All files=*.*", [ecx])
        SetWin$ hMyEdit=FileRead$(FileOpen$())
    .endif
  .else
    .if FileSave$("Asm sources=*.asm|Includes=*.inc|All files=*.*", [ecx])
        FileWrite FileSave$(), Win$(hMyEdit)
    .endif
  .endif   
  ret
OpenOrSave endp
NoArgs:
  MsgBox 0, "This is the NoArgs label (it could be a proc, too)", "Hi", MB_OK
  retn
GuiEnd