Jump to content

Using keyboard input in a plugin (single key events)


Recommended Posts

I'm working on a mod atm and I'm not very experienced in KSP modding. To control parts in this mod I want to use the W,A,S,D,Q,E (and maybe shift & ctrl) buttons when my module is activated. After hours of trying to reverse-engineer some open source code and reading the tiny fraction of API information available, I still have a few questions:

- The input seems to be saved in a FlightCtrlState (specifically: vessel.CtrlState). If I change all the values it contains to zero, will that mean the control of the vessel will be disabled? I need to do this since I want to use the input to control other things. (I know I could use different keys, but using the same as for controlling a vessel will be better in the case of my mod)

- I found information from Unity that you can get the input from W,A,S,D with Input.GetAxis("Horizontal/Vertical") does it work in KSP? and what is the name of the axis for Q and E? (I know I could use Input.GetKey() but that only returns true/false)

- Is OnfixedUpdate the best place to check user input?

Thanks in advance! :)

Morolas

Link to comment
Share on other sites

you can grab the FlightCtrState before it's propagated through the part hierarchy by using the vessel.OnFlyByWire delegate thusly:


vessel.OnFlyByWire += new FlightInputCallback(this.drive);

note: Remember to remove the callback in the OnDestroy method thusly:


vessel.OnFlyByWire -= new FlightInputCallback(this.drive);

Your method will then be called with the current FlightCtrlState as input before it's passed through the vessel hierarchy and you can read/modify it however you want.

public void drive(FlightCtrlState s){

//do some stuff. Example, set pitch to neutral:

s.pitch = 0;

}

Another note. If you want to grab button presses and or axis values in accordance with the KSP control scheme, you can use GameSettings. For example, if you want to detect if the player is pressing the pitch up key:


if (GameSettings.PITCH_UP.GetKey()){
//do something
}

Or maybe you'll want to read the value of the pitch axis:


float pitch = GameSettings.AXIS_PITCH.GetAxis();

If you use this method, you'll automatically align your plugin with whatever key/axis bindings the player sets in the game settings.

Edited by JDP
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...