Run script with key press

To share and ask questions about Arma code, scripts, mods, and missions.

Moderator: VileAce

Post Reply
User avatar
VileAce
Site Admin
Posts: 1454
Joined: April 6th, 2017, 1:00 pm
Location: North Carolina

NAK Command

NAK Tac

NAK Elite

NAK Donor

August 5th, 2018, 8:40 am

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: https://forums.bohemia.net/forums/topic ... e-scripts/

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

This link is to CBA documentation related to their keybinding system. Most developers prefer using the CBA keybinding: https://github.com/CBATeam/CBA_A3/wiki/Keybinding

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/f ... d-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/Arm ... eysMapping

User avatar
VileAce
Site Admin
Posts: 1454
Joined: April 6th, 2017, 1:00 pm
Location: North Carolina

NAK Command

NAK Tac

NAK Elite

NAK Donor

August 5th, 2018, 11:19 am

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.

onibuke
Posts: 3
Joined: May 18th, 2018, 4:34 pm

NAK Tac

August 5th, 2018, 1:06 pm

For the user who wanted the info, there's also the addAction command. https://community.bistudio.com/wiki/addAction

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

For external scripts:

Code: Select all

_actionID = player addAction ["Exec the file", "scriptFile.sqf"];
Or code into the addAction command directly:

Code: Select all

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

In-game name: Luke

User avatar
VileAce
Site Admin
Posts: 1454
Joined: April 6th, 2017, 1:00 pm
Location: North Carolina

NAK Command

NAK Tac

NAK Elite

NAK Donor

August 5th, 2018, 1:52 pm

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.

Tirinoarim
Posts: 33
Joined: December 11th, 2017, 10:11 am

June 24th, 2019, 2:46 pm

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?

Code: Select all

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
}];

User avatar
VileAce
Site Admin
Posts: 1454
Joined: April 6th, 2017, 1:00 pm
Location: North Carolina

NAK Command

NAK Tac

NAK Elite

NAK Donor

June 24th, 2019, 7:15 pm

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/Cat ... nds_Arma_3
https://community.bistudio.com/wiki/Cat ... _Functions
https://community.bistudio.com/wiki/Cat ... 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 - https://community.bistudio.com/wiki/Use ... t_Handlers

Code: Select all

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 - https://forums.bohemia.net/forums/topic ... es-action/
and this - https://forums.bohemia.net/forums/topic ... e-is-full/

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

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users