Jump to content
Orion987

Run script when button is pressed.

Recommended Posts

Hello. I am sorry if this has been asked/answered before, but I could not find anything on it. I am trying to run a script when the user presses a certain button (maybe "\" for example). The reason I am not just using addAction to have it on the scroll menu is because it is a respawn script, and it is accidently pressed from time to time, which is very annoying. Is there anyway to map a key to a script? Or is there a way to add it to one of the F1-F8 menus, that would be fine too. The player is not always the squad leader which is why I am not using radio triggers. Please tell me if I left out anything important. Thanks!

Share this post


Link to post
Share on other sites

Hi...

Put this in a file called init.sqf....in your mission folder. This will create a little handler for keypresses.

MY_KEYDOWN_FNC = {
switch (_this) do {

               //Key Y
	case 44: {
		nul = [] execVM "my_script1.sqf";
	};

               //Key X
	case 45: {
		nul = [] execVM "my_script2.sqf";
	};

               //Key C
	case 46: {
		nul = [] execVM "my_script3.sqf";
	};
};
};

To enable your little keypress handler at any time....

//Add an EventHandler to the main display...
waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown","_this select 1 call MY_KEYDOWN_FNC;false;"];

Now.. when you press the "Y" key... my_script1.sqf will run.

Press the "X" key and my_script2.sqf will run.... and so on.....

To remove the EventHandler and return your keyboard to normal.... use....

//Remove the EventHandler...
(findDisplay 46) displayRemoveAllEventHandlers "KeyDown";

ASCII Codes can be found here.

I believe you have to use the HEX codes.... not 100% sure on that so fiddle to find out.

EDIT: I just saw Demonized post above... and think you'd better use the link he posted there to get the Key Codes. As standard ASCII codes are probably not what Arma 2 uses. I have changed the codes in the script above according to that page... so it should now work as posted.

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

Thank you both very much! I am having one more problem that doesn't really deserve its own thread. I am trying to spawn a module only when a certain variable is true (which is set initially in a unit). I set the variable to true, and in the condition of presence line I put (myVariable). The module doesn't spawn. However, if I put the condition of presence back to true and use hint format to check the variable in the modules init line, the variable returns true. I do not understand why this is not working. Thanks for any help!

Share this post


Link to post
Share on other sites

What exactly is in your condition. It should be something like this....

this && myVariable

Is the this && there?

Share this post


Link to post
Share on other sites
Hi...

Put this in a file called init.sqf....in your mission folder. This will create a little handler for keypresses.

MY_KEYDOWN_FNC = {
switch (_this) do {

               //Key Y
	case 44: {
		nul = [] execVM "my_script1.sqf";
	};

               //Key X
	case 45: {
		nul = [] execVM "my_script2.sqf";
	};

               //Key C
	case 46: {
		nul = [] execVM "my_script3.sqf";
	};
};
};

To enable your little keypress handler at any time....

//Add an EventHandler to the main display...
waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown","_this select 1 call MY_KEYDOWN_FNC;false;"];

Now.. when you press the "Y" key... my_script1.sqf will run.

Press the "X" key and my_script2.sqf will run.... and so on.....

To remove the EventHandler and return your keyboard to normal.... use....

//Remove the EventHandler...
(findDisplay 46) displayRemoveAllEventHandlers "KeyDown";

ASCII Codes can be found here.

I believe you have to use the HEX codes.... not 100% sure on that so fiddle to find out.

EDIT: I just saw Demonized post above... and think you'd better use the link he posted there to get the Key Codes. As standard ASCII codes are probably not what Arma 2 uses. I have changed the codes in the script above according to that page... so it should now work as posted.

You' re da man, you know that, right?

Edited by alleycat

Share this post


Link to post
Share on other sites

I tri

 

Hi...

Put this in a file called init.sqf....in your mission folder. This will create a little handler for keypresses.

MY_KEYDOWN_FNC = {
switch (_this) do {

//Key Y
case 44: {
nul = [] execVM "my_script1.sqf";
};

//Key X
case 45: {
nul = [] execVM "my_script2.sqf";
};

//Key C
case 46: {
nul = [] execVM "my_script3.sqf";
};
};
};



To enable your little keypress handler at any time....

//Add an EventHandler to the main display...
waituntil {!isnull (finddisplay 46)};
(findDisplay 46) displayAddEventHandler ["KeyDown","_this select 1 call MY_KEYDOWN_FNC;false;"];


Now.. when you press the "Y" key... my_script1.sqf will run.

Press the "X" key and my_script2.sqf will run.... and so on.....

To remove the EventHandler and return your keyboard to normal.... use....

//Remove the EventHandler...
(findDisplay 46) displayRemoveAllEventHandlers "KeyDown";



ASCII Codes can be found here.

I believe you have to use the HEX codes.... not 100% sure on that so fiddle to find out.


EDIT: I just saw Demonized post above... and think you'd better use the link he posted there to get the Key Codes. As standard ASCII codes are probably not what Arma 2 uses. I have changed the codes in the script above according to that page... so it should now work as posted.

i know this is an old comment but i'm trying this and it's not working

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×