Forum Jump :

Author Message


Posts: 12
Rank:


Level: Member

Country: us
Location:
Occupation:
Age:
In-game name:

 
#1 Posted at 2013-04-20 14:03        
     
Hello, I have been looking into this for hours, so I think it's finally time to hit up the forums.

Here's my goal: I want to have it so when the user presses a keyboard key (let's say the "HOME" key) a script is executed. I don't want it to be an addon, just a script for a specific mission, preferably MP-friendly. Let's say the key press will activate this script:
player setpos (screenToWorld [0.5,0.5])
Hopefully I can learn how to do this with multiple scripts, and maybe with combined key presses, but first things first.
Here are a few links to pages I've looked at:
  • http://forums.bistudio.com/showthread.php?112327-Assign-a-script-to-a-custom-key-combination
  • http://community.bistudio.com/wiki/User_Interface_Event_Handlers#onKeyDown
  • http://www.armaholic.com/forums.php?m=posts&q=20709
  • http://community.bistudio.com/wiki/DIK

I am a script-kiddie, very novice with code, so I would very much appreciate it if someone with the know-how could give me detailed, step-by-step instructions on how to achieve this!


Author Message


Posts: 1120
Rank:


Level: Member PFS

Country: uk
Location:
Occupation:
Age:
In-game name:

 
#2 Posted at 2013-04-20 17:36        
     
save as init.sqf
waituntil {!(IsNull (findDisplay 46))};
_keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "if (_this select 1 == 199) then {player setpos (screenToWorld [0.5,0.5])}"];

This post was edited by F2kSel (2013-04-20 18:23, 365 days ago)


Advertisement


Author Message


Posts: 12
Rank:


Level: Member

Country: us
Location:
Occupation:
Age:
In-game name:

 
#3 Posted at 2013-04-21 10:54        
     
# F2kSel : save as init.sqf
waituntil {!(IsNull (findDisplay 46))};
_keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "if (_this select 1 == 199) then {player setpos (screenToWorld [0.5,0.5])}"];
Thank you so much! At first I was a bit puzzled where the HOME key fit in. I looked at the DIK codes for key presses and saw that “0xc7” is its hex code. I searched in DuckDuckGo, and saw that 0xc7 = 199 on this site.
Since I now know how to read it, I can probably figure out how to do key combinations with this thread.

One more thing, I tried using execVM to execute an .sqs file by button press and it didn't work at first. In the example below, eventually I figured out that for test.sqs, if you use an apostrophe: ' instead of quotation marks: ", it works, because it doesn't cancel out the array entry:
waituntil {!(IsNull (findDisplay 46))};  _keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "(if (_this select 1 == 200) then {hint 'test'; player execVM 'test.sqs'})"];

Thanks again, this is going to be so awesome... hopefully it also works in multiplayer!


Author Message


Posts: 1120
Rank:


Level: Member PFS

Country: uk
Location:
Occupation:
Age:
In-game name:

 
#4 Posted at 2013-04-21 12:46        
     
execVM is for sqf files

exec is for sqs files

you can easily find the key being pressed

waituntil {!(IsNull (findDisplay 46))};
_keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "hint str _this"];


That will return all the info from the event handler.


Author Message


Posts: 12
Rank:


Level: Member

Country: us
Location:
Occupation:
Age:
In-game name:

 
#5 Posted at 2013-04-30 21:14        
     
# F2kSel : execVM is for sqf files
exec is for sqs files
you can easily find the key being pressed
waituntil {!(IsNull (findDisplay 46))};
_keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "hint str _this"];
That will return all the info from the event handler.
This helped immensely. I do have a question, however. Right now, a player can hold the key and the script will just repeat infinitely without a pause. I can't seem to make it so that the key-press script cannot be repeated until the script is done. Here's what I tried:
waituntil {!(IsNull (findDisplay 46))};  
_keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "(if (_this select 1 == 200) then {_lunge = player execVM 'lunge.sqf';
waitUntil {scriptDone _lunge};
preprocessFile 'lunge.sqf';
... which didn't work.


Author Message


Posts: 1120
Rank:


Level: Member PFS

Country: uk
Location:
Occupation:
Age:
In-game name:

 
#6 Posted at 2013-05-01 00:53        
     
The reason that won't work is it's an eventhandler in effect a script that is attached to the player so what comes after it will have no effect.

I don't know if this will work in MP, You need to change _keydown to keydown

disableSerialization;
waituntil {!(IsNull (findDisplay 46))};  
keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "(if (_this select 1 == 200) then {_lunge = player execVM 'lunge.sqf'}"];
preprocessFile 'lunge.sqf';



in your script

(finddisplay 46) displayremoveeventhandler ['KeyDown',keyDown]; 
// your
// script
// goes
// here

keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "(if (_this select 1 == 200) then {_lunge = player execVM 'key.sqf'})"];


Or you could try using a variable

player setvariable ["key",true,true];
waituntil {!(IsNull (findDisplay 46))};  
_keyDown = (findDisplay 46) displayAddEventHandler ["KeyDown", "(if (_this select 1 == 200 and player getvariable 'key') then {_lunge = player execVM 'key.sqf';player setvariable ['key',false,true]  })"];


place this at the end of your script to allow the evh to work again
player setvariable ['key',true,true];


I've not tested these but I have used something like these before.