Page 1 of 3

Intercepting user key presses

Posted: Thu Nov 26, 2009 8:02 am
by Commander McLane
Simon B wrote:
Isn't there a way to intercept a keystroke in a script?
No.

Posted: Thu Nov 26, 2009 8:29 am
by Killer Wolf
what about an OXP overriding keyboard def that didn't have the H key defined?

Posted: Thu Nov 26, 2009 9:38 am
by Commander McLane
Interesting idea. Could be worthwhile trying.

However, this would mean that, as long as you have this OXP installed, you could never use the H key, regardless of which ship you are actually flying. And we wouldn't want that, I think.

Posted: Thu Nov 26, 2009 10:06 am
by Kaks
One thing that could be done - and what I did with the commsDemo test oxp - is to intercept screen changes. Oolite doesn't differentiate between f1 to f4, so that leaves f5 to f8 available for 'interception'. It's just about doable, but a bit of a minefield.

Alternatively, and slightly less of a minefield, you could detect if you fired your ecm, and do 'whatever' at the same time as firing your electronic counter-measures. Of course, the ecm will have to be present and in working order for any script to detect its activation.

Posted: Thu Nov 26, 2009 10:49 am
by another_commander
Here is a quick outline of an idea for intercepting key presses defined by user:
1. Set up a new key definition in PlayerEntityControls.m. As an example, I am calling the key udk1 (User Defined Key 1) and I assign it to the '[' key. This can be changed using the standard procedure involving editing keyconfig.plist.

2. Intercept the press of the '[' key in PlayerEntityControls. When this key is detected as pressed, the code simply executes a script event handler:

Code: Select all

if ([gameView isDown:key_udk1])
			{	
				if (!udk1_pressed)
				{
					[self doScriptEvent:@"udk1Pressed"];
				}
				udk1_pressed = YES;
			}
			else  udk1_pressed = NO;
3. Make a script.js containing the event handler udk1Pressed, something like this:

Code: Select all

this.udk1Pressed = function()
{
	log("udk1Pressed", "User defined function key 1 pressed.");
}
You can put whatever code you want to execute in the handler, of course.

4. Launch game and press '['. Your script handler is executed.

Problem: If many scripts want to use udk1, then unwanted or unpredicted actions may be executed as well. But it's something that seems to work for a start. Possibly another three udk function keys could be set, so at least four different actions can be executed. This will take some work, but I tested the concept and I can definitely intercept the user defined key press.

Edit: Split to its own thread.

Posted: Thu Nov 26, 2009 11:05 am
by Commander McLane
Errmmm... sorry, a_c, but I think here you have made a splitting mistake. Especially posts number 2 and 3 in this new thread (by Killer Wolf and me) are not actually dealing with the question of how to intercept keypresses, but with the original question of how to disable the witchjump capability of a player ship. They should be returned to that thread.

Oh, and you also made me look like I had started a thread with a typo in its title. Tz, tz, tz... 8)

Posted: Thu Nov 26, 2009 11:13 am
by Kaks
I can see scripts conflicting over those poor 4 user defined keys already!

Mind you, it is a much better solution than trying to intercept Oolite's standard actions, ie the minefields I mentioned before. :)

Posted: Thu Nov 26, 2009 11:25 am
by another_commander
Commander McLane wrote:
Errmmm... sorry, a_c, but I think here you have made a splitting mistake. Especially posts number 2 and 3 in this new thread (by Killer Wolf and me) are not actually dealing with the question of how to intercept keypresses, but with the original question of how to disable the witchjump capability of a player ship. They should be returned to that thread.

Oh, and you also made me look like I had started a thread with a typo in its title. Tz, tz, tz... 8)
You are right, I am sorry. I messed it up and, what's worse, I am not sure I can return the two off-topic comments back without putting them in a new thread.

I epic fial in thread splitting :oops:

Posted: Thu Nov 26, 2009 11:35 am
by Kaks
Oh dear! Still, while we're here.... I did miss the original question, but yes, in js you can intercept the hyperspace countdown event. In there you can always put something along the lines of

Code: Select all

if (missionVariables.brokenHyperspace) player.ship.fuel=0;
Even better, you could put a 14 second timer (or hyperspace spin-up time - 1 seconds timer) that drops the fuel to 0 just before a hyperspace jump, then restores it to the original value once the hyperspace failed. Of course we could still add an allowHyperspace property to all the ships for 1.74, which would make things a tad simpler... :)

Posted: Thu Nov 26, 2009 12:04 pm
by Commander McLane
Kaks wrote:
... you could put a 14 second timer (or hyperspace spin-up time - 1 seconds timer) that drops the fuel to 0 just before a hyperspace jump, then restores it to the original value once the hyperspace failed.
Yes, but as I already wrote in the other thread as a response to the original question, from an in-game perspective I would expect a ship's computer to know that it isn't hyperspace capable at all, and therefore not start a countdown in the first place. Thus...
Kaks wrote:
Of course we could still add an allowHyperspace property to all the ships for 1.74, which would make things a tad simpler... :)
this would be a much better solution.

Posted: Thu Nov 26, 2009 12:05 pm
by drew
another_commander wrote:
I epic fial in thread splitting :oops:
And spelling too! :lol:

Cheers,

Drew.

Detecting whether a ship (or just the player) is ECMing

Posted: Thu Oct 06, 2011 7:22 am
by UK_Eliter
If I may resurrect this thread - is there a way to do this, please?

Re: Intercepting user key presses

Posted: Sun Jan 01, 2012 3:56 pm
by Switeck
I too am interested in .js scripts being able to detect both player keypresses and whether a ship (or just the player) is ECMing. Such as for making a very expensive missile that homes on ECM...or adding additional failure states to various equipment, like ECM sometimes draining more energy than normal due to poor ship repair.

Re: Intercepting user key presses

Posted: Sun Jan 01, 2012 7:10 pm
by Kaks
JS script detecting keypresses? Primable equipment was devised precisely to allow js script to detect a player hitting a (redefinable) key, without falling into the sticky situation of having 2 or 3 totally different scripts trying to react to the same keypress at the same time.

About ecm, you can use the shipHitByECM event handler to figure out which ship activated said ECM: it's not that simple, and it could be prone to errors: create a global variable with a ecm timestamp & blast remaining as a unique identifier, update that variable as the last line inside the event handler.

If the current game time & blast remaining is different from what it says inside that global variable, it means that the ship associated with this js script is the first ship hit by the current ECM wave - the first ship hit by the current ECM wave is the ship that started the ECM pulse.

This approach will not be that useful in pitched battles: two ships could conceivably start an ECM blast at the same time, in that case only one of them will be identified as the originator of both ECM pulses.


Having said all that, we might add a whom argument to the event handler to make that sort of thing 'slightly' more doable, though...

Re: Intercepting user key presses

Posted: Sun Jan 01, 2012 10:47 pm
by Switeck
So for instance, I'll have to make a primable version of the energy bomb just to detect when it's fired?

I'm already fighting with a list of 10-20 primable equipment items in no particular order in my most upgraded ship. Even using CTRL+Shift+N to go backwards through the list only helps a little. Doubly sucks that some of my ships have the equipment in different order.