Click or drag to resize

KeyboardInputSwitch Class

Wrapper around a keyboard key code that incapsulates the persiting and the handling logic into a single class.
Inheritance Hierarchy
SystemObject
  KSPDev.InputUtilsKeyboardInputSwitch

Namespace:  KSPDev.InputUtils
Assembly:  KSPDev_Utils.2.5 (in KSPDev_Utils.2.5.dll) Version: 2.5 for KSP v1
Syntax
C#
public class KeyboardInputSwitch : IConfigNode
Request Example View Source

The KeyboardInputSwitch type exposes the following members.

Constructors
  NameDescription
Public methodKeyboardInputSwitch
Creates a switch with a None key binding. It's a default constructor needed for the PersistentFieldAttribute functionality to work.
Public methodKeyboardInputSwitch(KeyCode)
Main constructor to create a switch for the provided key code.
Top
Properties
  NameDescription
Public propertyStatic memberisAnyKeyHold
Tells if any switch is being hold.
Public propertyisHold
Defines the current hold state of the switch.
Top
Methods
  NameDescription
Public methodLoad
Loads a persisted switch binding.
Public methodSave
Saves the switch binding.
Protected methodSetHoldState
Updates the hold state and triggers the event(s) if any.
Public methodUpdate
Checks the keyboard status and updates the switch accordingly.
Top
Events
  NameDescription
Public eventOnClick
Event that notifies about the click event.
Public eventOnPress
Event that notifies that the switch key has been pressed.
Public eventOnRelease
Event that notifies that the switch key has been released.
Public eventOnStateChanged
Event that notifies about the hold state change. The event is only called when the state has actually changed.
Top
Fields
  NameDescription
Public fieldStatic memberClickDelay
Maximum delay to record a click event.
Public fieldkeyboardEnabled
Determines if the switch should react on the keyboard events from the Update method.
Public fieldkeyCode
Key code for the switch.
Top
Remarks

The fields of this type are correctly handled by the stock game persisting functionality (KSPField). It's also compatible with the KSPDev persisting logic (PersistentFieldAttribute).

Important! This type will be correctly loaded or saved by the KSP core but it will not be correctly copied in the game's editor. It's not an issue when the field is static but in case of it's an instance member, the code must not be accessing it in the editor mode.

Examples

To define a key binding just create a class with the key code as a parameter, and notify the switch about the frame updates so that it could update its state:

class MyClass : MonoBehaviour {
  KeyboardInputSwitch mySwitch = new KeyboardInputSwitch(KeyCode.Alpha1);

  void Update() {
    if (mySwitch.Update()) {
      Debug.Log("The key is being hold");
    }
  }
}

In case of the switch state needs to be checked from the other methods use isHold property:

class MyClass : MonoBehaviour {
  KeyboardInputSwitch mySwitch = new KeyboardInputSwitch(KeyCode.Alpha1);

  void Update() {
    mySwitch.Update();
  }

  void FixedUpdate() {
    if (mySwitch.isHold) {
      Debug.Log("The key is being hold");
    }
  }
}

When the code needs to react to a switch state event, it can register a listener:

class MyClass : MonoBehaviour {
  KeyboardInputSwitch mySwitch = new KeyboardInputSwitch(KeyCode.Alpha1);

  void Awake() {
    mySwitch.OnStateChanged += OnSwitchStateChange;
    mySwitch.OnPress += delegate{
      Debug.Log("Key pressed");
    };
    mySwitch.OnRelease += delegate{
      Debug.Log("Key is released");
    };
    mySwitch.OnClick += delegate{
      Debug.Log("Key click registered");
    };
  }

  void OnDestroy() {
    // Do nothing since in this example the switch is an instance field, and it will be
    // destroyed together with the owning class (and, hence, all the listeners).
    // However, if it was a static field we would do something like this:
    mySwitch.OnStateChanged -= OnSwitchStateChange;
    // Anonymous functions cannot be unregistered, so don't use them on the static fields.
  }

  void OnSwitchStateChange() {
    Debug.LogFormat("Switch state changed to: {0}", mySwitch.isHold);
  }

  void Update() {
    mySwitch.Update();
  }
}

In many cases you may want to load a key bining from a config file. It can be achieved by adding an attribute to the field of this type:

[PersistentFieldsFile("my/mod/settings.cfg", "")]
class MyClass : PartModule {
  // Note that KSPField attributed fields *must* be public.
  [KSPField]
  public KeyboardInputSwitch switchFromPart = new KeyboardInputSwitch(KeyCode.Alpha1);

  // Note that for a PersistentField attribute the field doesn't need to be public.
  // However, the private fields are handled a bit differently (read the docs!).
  [PersistentField("Keyboard/Bindings")]
  KeyboardInputSwitch switchFromSettings = new KeyboardInputSwitch(KeyCode.Alpha2);

  public override void OnLoad(ConfigNode node) {
    // At this point `switchFromPart` is already loaded from the part's config.
    base.OnLoad(node);
    // Load `switchFromSettings` via KSPDev from "my/mod/settings.cfg".
    KSPDev.ConfigUtils.ConfigAccessor.ReadFieldsInType(typeof(MyClass), this);
  }
}
See Also