No.Simon B wrote:Isn't there a way to intercept a keystroke in a script?
Intercepting user key presses
Moderators: winston, another_commander
- Commander McLane
- ---- 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
Last edited by Commander McLane on Thu Nov 26, 2009 11:17 am, edited 1 time in total.
- Killer Wolf
- ---- E L I T E ----
- Posts: 2279
- Joined: Tue Jan 02, 2007 12:38 pm
- Commander McLane
- ---- 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:
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.
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)
-
- Quite Grand Sub-Admiral
- Posts: 6683
- Joined: Wed Feb 28, 2007 7:54 am
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:
3. Make a script.js containing the event handler udk1Pressed, something like this:
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.
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;
Code: Select all
this.udk1Pressed = function()
{
log("udk1Pressed", "User defined function key 1 pressed.");
}
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.
- Commander McLane
- ---- 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:
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...
Oh, and you also made me look like I had started a thread with a typo in its title. Tz, tz, tz...
Last edited by Commander McLane on Thu Nov 26, 2009 11:16 am, edited 1 time in total.
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.
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)
-
- Quite Grand Sub-Admiral
- Posts: 6683
- Joined: Wed Feb 28, 2007 7:54 am
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.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...
I epic fial in thread splitting
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
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...
Code: Select all
if (missionVariables.brokenHyperspace) player.ship.fuel=0;
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
- Commander McLane
- ---- 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:
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:... 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.
this would be a much better solution.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...
-
- ---- 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
If I may resurrect this thread - is there a way to do this, please?
Re: Intercepting user key presses
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
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...
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)
Re: Intercepting user key presses
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.
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.