Run script with key press

I was ask to share information about how to run a script using a key press. The best way for me to explain it is to provide links to existing information available on the internet. After reviewing that information if you have specific questions we can discuss it here.

This first link is a BI forum post and has great examples and links to other relevant information. I would start here, specifically comments from stuguy: Custom keys to activate scripts - ARMA 3 - MISSION EDITING & SCRIPTING - Bohemia Interactive Forums

This is a link to the BI function to watch for the keypress and run code when activated:
https://community.bistudio.com/wiki/displayAddEventHandler

This link is to CBA documentation related to their keybinding system. Most developers prefer using the CBA keybinding: Keybinding · CBATeam/CBA_A3 Wiki · GitHub

This last link is to CBA documentation related to keybinding, there is additional information on the left hand menu on this CBA site:
https://cbateam.github.io/CBA_A3/docs/files/keybinding/fnc_addKeybind-sqf.html

These links are not specific but are general keypress info:
https://community.bistudio.com/wiki/actionKeys
https://community.bistudio.com/wiki/DIK_KeyCodes
https://community.bistudio.com/wiki/inputAction/actions
https://community.bistudio.com/wiki/ArmA:_CfgDefaultKeysMapping

As an example the view distance script in all of the Zeus missions, CHVD, has both standard keybindings and CBA keybindings as well as a CBA custom menu.

For the user who wanted the info, there’s also the addAction command. addAction - Bohemia Interactive Community

It’s not quite a keypress, but adds to the action menu (like the menu where the “open door” action is)

For external scripts:

_actionID = player addAction ["Exec the file", "scriptFile.sqf"];

Or code into the addAction command directly:

_actionID = player addAction ["This is the name of the action", { hint "Hello World!"; player setDamage 1; }];

TY for the info. The developer is a more advanced coder and is aware of add action, add action is not a practical option in this case.

Just trying to learn arma dev (I’ve been coding for 30-odd years but this is new to me!) and thought this would be of use?

waitUntil {!(isNull (findDisplay 46))};			// Wait for main UI to be ready

(findDisplay 46) displayAddEventHandler ["KeyDown", {	// Add event handler
	switch (_this select 1) do {
		case 181: {				// 181 is keycode for "/"
			hint "Run a script";		// Do whatever
		};
	};
	false
}];

If you really want to learn Arma coding, you need to learn to use the BI Wiki. But I added comments to help explain the code
https://community.bistudio.com/wiki/Category:Scripting_Commands_Arma_3
https://community.bistudio.com/wiki/Category:Arma_3:_Functions
https://community.bistudio.com/wiki/Category:Arma_3:_Editing

This code uses an event handler, one of the better ways to code actions, a list of the different event handlers can be found here - User Interface Event Handlers - Bohemia Interactive Community

waitUntil {!(isNull (findDisplay 46))};		
// COMMENT ** Wait until display 46 as not null (Display 46 is the main mission display) you can make custom displays therefor you could tell this to wait until your custom display is on screen - https://community.bistudio.com/wiki/findDisplay

(findDisplay 46) displayAddEventHandler ["KeyDown",
// COMMENT ** Adds an event handler to the given display (an event handler is a watch statement - watch for specific events on a specific display) - https://community.bistudio.com/wiki/displayAddEventHandler
// COMMENT ** Syntax:
// COMMENT **    display displayAddEventHandler [eventName, code]
// COMMENT ** Parameters:
// COMMENT **      display: Display
// COMMENT **      eventName: String
// COMMENT **      code: String or Code
// COMMENT ** Return Value:
// COMMENT **      Number
     
       {	
	switch (_this select 1) do {
		case 181: {				// 181 is keycode for "/"
			hint "Run a script";		// Do whatever
		};
	};
// COMMENT ** This whole section between the first and end curly brackets is the code that runs when a key (any key) is pressed while display 46 is active
// COMMENT ** Switch (_this select 1) do { - This tells the code to do an action based on the second element of the displayAddEventHandler (which would be the value of the key pressed) *Arma like most programming languages start counting arrays at 0 so "select 1" would be the second element of the array
// COMMENT **  Case 181: goes back to the switch command, this is like "if case 181 then do this".  You could easily use an if statement here.  I normally use a "switch do" when I have 3 or more conditions, you could easily add additional Case key values. The value of the key pressed is based on DIK codes, 181 is NUM /, you can find the reference here - https://community.bistudio.com/wiki/DIK_KeyCodes
// COMMENT **  the code between the next pair of curly brackets is what happens when key 181 or NUM / is pressed.  in this case, it just displays a hint, which shows in the upper right corner by default. Hint is often used in debugging to visually inform the coder when different parts of code are running. This code could be very complex.
	false
// COMMENT **  lastly the false tells Arma if your code should overwrite the default engine handling for keyboard events.  This would default to false and could the left out. For Display 46 you would not want to overwrite the default handling of keys
}];

This is a good thread on the issue - Detect Certain Key and Does Action? - ARMA 3 - MISSION EDITING & SCRIPTING - Bohemia Interactive Forums
and this - [CODE SNIPPET] Using actionKeys && prevent players from reloading if magazine is full - ARMA 3 - MISSION EDITING & SCRIPTING - Bohemia Interactive Forums

I know this is complicated to start but the Wiki and the development threads are a good resource.