Jump to content

How to add a key binding?


Recommended Posts

Write your own code to monitor for the keypress and toggle the action group


// put this in Update
if (Input.GetKeyDown(KeyCode.Key)) // put your KeyCode here
{
vessel.ActionGroups.ToggleGroup(KSPActionGroup.Brakes)
}

Link to comment
Share on other sites

Write your own code to monitor for the keypress and toggle the action group


// put this in Update
if (Input.GetKeyDown(KeyCode.Key)) // put your KeyCode here
{
vessel.ActionGroups.ToggleGroup(KSPActionGroup.Brakes)
}

Is there any way to integrate this to the KSP key binding system or do I need to create my own?

Link to comment
Share on other sites

If you want to tie it to a KSP binding (yaw left, brakes, etc.) instead of a specific key, you use

if (GameSettings.*Action*.GetKeyDown()) // where action would probably be brakes in your case
// do things

Edited by Crzyrndm
Link to comment
Share on other sites

If you want to tie it to a KSP binding (yaw left, brakes, etc.) instead of a specific key, you use

if (GameSettings.*Action*.GetKeyDown()) // where action would probably be brakes in your case
// do things

I mean, is there a way to add my keybinding to the KSP keybinding system? Or do I just have to make my own keybinding system?

Link to comment
Share on other sites

To my knowledge, no, you can only add a bunch of your own monitoring code. Even if you could, there isn't much/any chance of it being simpler than the first snippet above.

If the issue is making it configurable, yes, you're going to have to write your own save/load method to remember which KeyCode was needed.

Link to comment
Share on other sites

To my knowledge, no, you can only add a bunch of your own monitoring code. Even if you could, there isn't much/any chance of it being simpler than the first snippet above.

If the issue is making it configurable, yes, you're going to have to write your own save/load method to remember which KeyCode was needed.

Alright, thanks!

Link to comment
Share on other sites

Welcome to my world. :P

I can confirm that there is no way to add your own stuff to KSP's keybinding system, you have to write your own system completely if you want to do this.

I've done this to differently levels for 3 different mods if you want to check those out.

The trickiest thing is that you have to save KeyCode data to disk as strings, so when you save use "KeyCodeObject.ToString()" and then convert that from a string back to a KeyCode when loading with "KeyCodeObject = (KeyCode)Enum.Parse(typeof(KeyCode), KeyCodeObejctString);

Hope that helps,

D.

Edited by Diazo
Link to comment
Share on other sites

One trick with types that implement IConfigNode is that you can get persistence practically for free inside certain types (ScenarioModules and PartModules):

[KSPScenario(ScenarioCreationOptions.AddToAllGames, new[] { GameScenes.SPACECENTER })]
public class ScenarioModuleKeyBindingPersistence : ScenarioModule
{
[KSPField(isPersistant = true)] // note: must be marked KSPField, persistent
private KeyBinding myBinding = new KeyBinding();


// note: none of the persistent fields have been set yet
public override void OnAwake()
{
base.OnAwake();
}

// persistent fields have been serialized by now
private void Start()
{
print("Start - KeyBindingPersistence - key is set to " + myBinding.primary);
}
}

Otherwise you'd want to be using KeyBinding.Save/Load. A verbose example:


public class AddonKeyBindingPersistence : MonoBehaviour
{
private KeyBinding myBinding = new KeyBinding();


private void Start()
{
if (File.Exists(GetConfigFilePath()))
LoadSettings();

print("myBinding is currently " + myBinding);
}

private void OnDestroy()
{
SaveSettings();
}

private void LoadSettings()
{
var config = ConfigNode.Load(GetConfigFilePath());
myBinding.Load(config.GetNode("MyBinding") ?? DefaultKeyBindingNode()); // KeyBinding will throw an exception on badly formed ConfigNodes
}

private void SaveSettings()
{
var config = new ConfigNode("MyAddonSettings");
myBinding.Save(config.AddNode("MyBinding"));
}


private string GetConfigFilePath()
{
var dllDir = Path.GetDirectoryName(AssemblyLoader.GetPathByType(typeof(AddonKeyBindingPersistence)));

if (!Directory.Exists(dllDir))
Directory.CreateDirectory(dllDir);

return Path.Combine(dllDir, "settings.cfg");
}

private static ConfigNode DefaultKeyBindingNode()
{
var node = new ConfigNode();
new KeyBinding(KeyCode.None).Save(node);
return node;
}
}
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]

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