Intercepting user key presses

General discussion for players of Oolite.

Moderators: winston, another_commander

User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Intercepting user key presses

Post by Commander McLane »

Simon B wrote:
Isn't there a way to intercept a keystroke in a script?
No.
Last edited by Commander McLane on Thu Nov 26, 2009 11:17 am, edited 1 time in total.
User avatar
Killer Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 2279
Joined: Tue Jan 02, 2007 12:38 pm

Post by Killer Wolf »

what about an OXP overriding keyboard def that didn't have the H key defined?
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Post 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.
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Post 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.
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Post 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.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Post 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)
Last edited by Commander McLane on Thu Nov 26, 2009 11:16 am, edited 1 time in total.
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Post 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. :)
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Post 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:
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Post 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... :)
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Post 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.
User avatar
drew
---- E L I T E ----
---- E L I T E ----
Posts: 2190
Joined: Fri May 19, 2006 9:29 am
Location: In front of a laptop writing a book.
Contact:

Post by drew »

another_commander wrote:
I epic fial in thread splitting :oops:
And spelling too! :lol:

Cheers,

Drew.
Drew is an author of SF and Fantasy Novels
WebsiteFacebookTwitter
UK_Eliter
---- E L I T E ----
---- E L I T E ----
Posts: 1248
Joined: Sat Sep 12, 2009 11:58 pm
Location: Essex (mainly industrial and occasionally anarchic)

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

Post by UK_Eliter »

If I may resurrect this thread - is there a way to do this, please?
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Intercepting user key presses

Post 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.
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Re: Intercepting user key presses

Post 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...
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Intercepting user key presses

Post 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.
Post Reply