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 (http://masm32.com/board/index.php?topic=94.0)) 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 (https://masm32.com/board/index.php?topic=11781.msg128030#msg128030) :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