| KeyboardInputSwitch Class | 
 Inheritance Hierarchy
Inheritance HierarchyNamespace: KSPDev.InputUtils
 Syntax
SyntaxThe KeyboardInputSwitch type exposes the following members.
 Constructors
Constructors| Name | Description | |
|---|---|---|
|  | KeyboardInputSwitch | 
            Creates a switch with a None key binding. It's a default constructor
            needed for the PersistentFieldAttribute functionality to work.
             | 
|  | KeyboardInputSwitch(KeyCode) | Main constructor to create a switch for the provided key code. | 
 Properties
Properties| Name | Description | |
|---|---|---|
|   | isAnyKeyHold | Tells if any switch is being hold. | 
|  | isHold | Defines the current hold state of the switch. | 
 Methods
Methods| Name | Description | |
|---|---|---|
|  | Load | Loads a persisted switch binding. | 
|  | Save | Saves the switch binding. | 
|  | SetHoldState | Updates the hold state and triggers the event(s) if any. | 
|  | Update | Checks the keyboard status and updates the switch accordingly. | 
 Events
Events| Name | Description | |
|---|---|---|
|  | OnClick | 
            Event that notifies about the click event.
             | 
|  | OnPress | 
            Event that notifies that the switch key has been pressed.
             | 
|  | OnRelease | 
            Event that notifies that the switch key has been released.
             | 
|  | OnStateChanged | 
            Event that notifies about the hold state change. The event is only called when the state has
            actually changed.
             | 
 Fields
Fields| Name | Description | |
|---|---|---|
|   | ClickDelay | Maximum delay to record a click event. | 
|  | keyboardEnabled | 
            Determines if the switch should react on the keyboard events from the Update
            method.
             | 
|  | keyCode | Key code for the switch. | 
 Remarks
RemarksThe 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
ExamplesTo 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
See Also