Arma 3 Scripting

Topic in Event Handlers

User Interface Event Handlers

Also available as Markdown and JSON, or read the original wiki page.

NOEDITSECTION

User Interface Event Handlers are used to execute code when events related to GUI components (i.e. Displays and Controls) occur.

Warning

About ctrlDelete in an Event Handler's script: ctrlDelete needs to be the last executed statement or Arma will crash.

Alternatively, the code can also be spawned to circumvent this issue.

onButtonClick = "ctrlDelete (_this select 0); systemChat 'You will never see this';";            // Crashes the game
onButtonClick = "systemChat 'Bye bye button!'; ctrlDelete (_this select 0);";                    // Works fine
onButtonClick = "_this spawn { ctrlDelete (_this select 0); systemChat 'Bye bye button!'; };";    // Works fine

Reference List

Generic Events

onLoad
params ["_displayOrControl", ["_config", configNull]];

Note

The order of initialisation is as follows:

  1. all controls, top to bottom.
  2. Display This means that during the onLoad event of the upper controls the lower controls have yet to be created.
onUnload
params ["_display", "_exitCode"];

Note

The onUnload event never fires for RscTitles displays created with cutRsc.

Warning

Code or function should be called, otherwise controls might be destroyed before spawned code is executed!

onChildDestroyed
params ["_display", "_closedChildDisplay", "_exitCode"];
onCommitted (Arma 3 v2.14)
params ["_control", "_animType", "_animTime"];
onEditChanged (Arma 3 v2.14)
params ["_control", "_newText"];
onMouseEnter
params ["_control"];

Note

The onMouseEnter event only fires on enabled buttons (see ctrlEnable).

Arma 3 v2.18 This event handler type seems to only work on controls that can be interacted with. For example buttons, listboxes and so on.

onMouseExit
params ["_control"];

Note

Arma 3 v2.18 This event handler type seems to only work on controls that can be interacted with. For example buttons, listboxes and so on.

onSetFocus
params ["_control"];
onKillFocus
params ["_control"];
onTimer

Note

This feature has yet to be implemented.

onKeyDown
params ["_displayOrControl", "_key", "_shift", "_ctrl", "_alt"];
true // Intercepts the default action, e.g. pressing Escape won't close the dialog.

In Arma 3, in order to be able to intercept key events during gameplay, the Event Handler should be added to Display 46:

findDisplay 46 displayAddEventHandler ["KeyDown", {}];

In general, one should never return true in the onKeyDown EH, especially if it's added to Display 46 - otherwise all input is overridden and there is no way to do anything in the game anymore.

Warning

Pressing and holding key triggers 'autorepeat' action on Windows, which in turn will make this EH fire repeatedly as well.

onKeyUp
params ["_displayOrControl", "_key", "_shift", "_ctrl", "_alt"];
onChar
params ["_displayOrControl", "_charCode"];
onIMEChar
params ["_control", "_charCode"];
onIMEComposition
params ["_control", "_charCode"];
onMouseButtonDown
params ["_displayOrControl", "_button", "_xPos", "_yPos", "_shift", "_ctrl", "_alt"];
onMouseButtonUp
params ["_displayOrControl", "_button", "_xPos", "_yPos", "_shift", "_ctrl", "_alt"];
onMouseButtonClick
params ["_control", "_button", "_xPos", "_yPos", "_shift", "_ctrl", "_alt"];
onMouseButtonDblClick
params ["_control", "_button", "_xPos", "_yPos", "_shift", "_ctrl", "_alt"];
onMouseMoving

params ["_control", "_xPos", "_yPos", "_mouseOver"];

params ["_display", "_xDeltaPos", "_yDeltaPos"];
onMouseHolding

params ["_control", "_xPos", "_yPos", "_mouseOver"];

params ["_display", "_xPos", "_yPos"];
onMouseZChanged
params ["_displayOrControl", "_scroll"];
onCanDestroy
params ["_control", "_exitCode"];
onDestroy
params ["_control", "_exitCode"];

Button Events

onButtonClick
params ["_control"];
onButtonDblClick (Arma 3 v1.00)
params ["_control"];
onButtonDown
params ["_control"];
onButtonUp
params ["_control"];

Listbox Events

onLBSelChanged

Warning

This EH will fire even if no new selection was made and the user clicked on existing selection

Note

Since Arma 3 v2.11 controls with the LB_MULTI style pass an additional _lbSelection parameter to the EH script (see lbCurSel, lbSelection).

params ["_control", "_lbCurSel", "_lbSelection"];
onLBListSelChanged
params ["_control", "_selectedIndex"];
onLBDblClick
params ["_control", "_selectedIndex"];
onLBDrag
params ["_control", "_listboxInfo"];
// Get info of first item being dragged:
(_listboxInfo select 0) params ["_lbText", "_lbValue", "_lbData"];
onLBDragging
params ["_control", "_xPos", "_yPos", "_listboxIDC", "_listboxInfo"];
// Get info of first item being dragged:
(_listboxInfo select 0) params ["_lbText", "_lbValue", "_lbData"];
onLBDrop
params ["_control", "_xPos", "_yPos", "_listboxIDC", "_listboxInfo"];
//Get info of first item being dropped:
(_listboxInfo select 0) params ["_lbText", "_lbValue", "_lbData"];

Tree Events

onTreeSelChanged
params ["_control", "_selectionPath"];
onTreeLButtonDown
params ["_control"];
onTreeDblClick
params ["_control", "_selectionPath"];
onTreeExpanded
params ["_control", "_selectionPath"];
onTreeCollapsed
params ["_control", "_selectionPath"];
onTreeMouseMove
params ["_control", "_path"];
onTreeMouseHold
params ["_control", "_path"];
onTreeMouseExit
params ["_control"];
onTreeFilterUpdated (Arma 3 v2.12)
params ["_treeControl", "_searchControl", "_searchString"];

Checkbox Events

onChecked (Arma 3 v1.00)

Note

This feature has yet to be implemented.

onCheckedChanged (Arma 3 v1.00)
params ["_control", "_checked"];
onCheckBoxesSelChanged
params ["_control", "_selectedIndex", "_currentState"];

Misc. Events

onToolBoxSelChanged
params ["_control", "_selectedIndex"];
params ["_control", "_url"];
onSliderPosChanged
params ["_control", "_newValue"];
onObjectMoved
params ["_control", "_offset"];
onMenuSelected
params ["_control", "_commandId"];
onDraw

Important

This event handler gets destroyed when the mission it was executed in ends.

params ["_controlOrDisplay"];
onVideoStopped (Arma 2: Operation Arrowhead v1.56)
params ["_control"];

Warning

When using the event names via GUI scripting commands (e.g ctrlAddEventHandler, displayAddEventHandler), the prefix "on" in the name must be removed (e.g. ButtonDown instead of onButtonDown)!

Event Parameters

The Event Handlers receive parameters through the magic variable _this. Each event passes a different set of parameters (listed above) in an array. The control or display that the event was assigned to is always found in _this select 0.

Scope

Armed Assault v1.00 In Armed Assault, most control-specific events ONLY work for controls and never for displays. The two exceptions being: onKeyDown and onKeyUp.

Arma 2 v1.00 Since Arma 2, most control-specific events work for both displays and controls.

Defining Events

User Interface Event Handlers can be assigned in two ways: Via class property definitions or via scripting commands.

Class-Defined Events

Events can be defined in the Dialog (display) or Control classes (in Config.cpp or Description.ext). The event property value (string) is executed as a line of code.

An example line (this would be found within a control or dialog class):

onMouseButtonDown = "hint str _this";

Full Example:

class RscControlsGroup
{
    type    = 15;
    idc        = -1;
    style    =  0;
    x = 0;
    y = 0;
    w = 1;
    h = 1;

    class VScrollbar
    {
        color[]                = { 1, 1, 1, 1 };
        width                = 0.021;
        autoScrollSpeed        = -1;
        autoScrollDelay        = 5;
        autoScrollRewind    = 0;
    };

    class HScrollbar
    {
        color[]    = { 1, 1, 1, 1 };
        height    = 0.028;
    };

    class ScrollBar        // >= Arma 2
    {
        color[]            = { 1, 1, 1, 0.6 };
        colorActive[]    = { 1, 1, 1, 1 };
        colorDisabled[]    = { 1, 1, 1, 0.3 };
        thumb            = "#(argb,8,8,3)color(1,1,1,1)";
        arrowEmpty        = "#(argb,8,8,3)color(1,1,1,1)";
        arrowFull        = "#(argb,8,8,3)color(1,1,1,1)";
        border            = "#(argb,8,8,3)color(1,1,1,1)";
    };

    class Controls {};
};

class MouseHandler : RscControlsGroup
{
    onMouseHolding        = "[0, _this] call myEventFunction";
    onMouseButtonDown    = "[1, _this] call myEventFunction";
    onMouseButtonUp        = "[2, _this] call myEventFunction";
    onMouseZChanged        = "[3, _this] call myEventFunction";
    onMouseEnter        = "[4, _this] call myEventFunction";
    idc = 123;
    x = 0.0;
    y = 0.0;
    w = 1.0;
    h = 1.0;
    colorBackground[] = { 0.2, 0.0, 0.0, 0.0 };
};

Script-Defined Events

These events can also be given to dialogs and controls using the displayAddEventHandler and ctrlAddEventHandler commands. The control has to be enabled for the Event Handler in order for Event Handlers to fire (see ctrlEnable).

// Add Event Handler:
(findDisplay 46) displayAddEventHandler ["KeyDown", {
    _this call functionName_keyDown;
}];

// Function definition:
functionName_keyDown = {
    params ["_ctrl", "_dikCode", "_shift", "_ctrlKey", "_alt"];
    private _handled = false;

    if (!_shift && !_ctrlKey && !_alt) then {
        if (_dikCode in (actionKeys "NetworkStats")) then {
            systemChat format ["EH fired for %1", _ctrl];
            _handled = true;
        };
    };

    _handled;
};