Jump to content

How do you bind to keys?


Recommended Posts

It is hardcoded to certain values in the FlightCtrlState class, which can be accessed by overriding the OnCtrlUpd(FlightCtrlState) function. It also seems you can modify certain input controls (yaw, pitch, roll) I haven\'t tested if it accepts changes to it.

Also seems you can access it through 'FlightInputHandler.state'. The only way you can do it is like Crazygerbil suggested. It might be possible to have the hotkey editable through the .cfg file but I haven\'t tested this either.

Link to comment
Share on other sites

But if you do that, this only returns if the key is pressed down. I\'m looking for something to alert me if the key is pushed and released.

I know in Java it was by registering something called a keylistener, does Unity have an equivalent?

Link to comment
Share on other sites

There are actually a number of ways to get input. If you want access to the input that\'s already used by the game. You have to use the FlightCtrlState. This will give you the status of the 'throttle', 'pitch' and any other built in keys. Such as 'gear down'.

If you want to use unity input. You can access it a number of ways.


using UnityEngine; // This is a shortcut so you don\'t have to type UnityEngine.Input, just input

public string DoSomething = 'B'; // This declares a public string, Since its public you can access it in the part.cfg file on load.

// The key functions return either 'true' or 'false' if the condition is met. This is a useful way of checking 'if' a key is down.

if (Input.GetKey(') // if true, so key is depressed
{
// Do something
}

if (Input.GetKey('B') // You can use a string to define the keys
if (Input.GetKey(KeyCode. // Or you can hard code them to a unity keycode
if(Input.GetKey(DoSomething) // we can also use a string variable to access a key. This is how you can add key configuration to your part.cfg file

// There are three main variants of the get key functions in unity.

GetKey(); // This is used to check if the key is depressed, returns true while the key is held. Useful for things like autofire or values that count up.

GetKeyDown(); // Returns true if the key has just been pressed down. Good for toggles and single shot items.
GetKeyUp(); // Returns true if the key has just been released. Good for toggles and single shot items. Will not be true if the key is held, only if it has been released.

This also applies to the mouse buttons

Input.GetMouseButton(0); // Gets the first mouse button (left)


Link to comment
Share on other sites

I\'ve one more question regarding keyboard input.

Everything is OK as soon as I handle button holds: Input.GetKey and GetButton methods work well. But capturing a single button press with Input.GetButtonDown or GetButtonUp is a kinda messed up. If those methods are called from MyClass.onCtrlUpd then only the part in an active vessel receives the keystroke (which it exactly what I need), but those keystrokes are sometimes skipped. If I call those methods from MyClass.onPartUpdate then no keystrokes are skipped, but every MyClass part in the visible range receives them! So I\'ve ended up testing activeVessel variable like this:

  protected override void onPartUpdate() {
base.onPartUpdate();
if( vessel != FlightGlobals.ActiveVessel )
return;
if( Input.GetKeyDown( KeyCode.H ) ) { // do something
...

which is quite ugly.

Is there more convenient way to capture keystrokes for active vessel only?

Link to comment
Share on other sites

  • 2 weeks later...

I don\'t really see what\'s ugly about checking if the part is on active vessel - seems pretty normal thing to me. However, if you dislike the activeVessel variable - you may want to take a look at the isControllable property of the Part class.

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