-4

I have a Keys array called "KeyboardInput". How can I store it in Settings and then also read them out as seperate keys somewhat like:

for (int i = 0; i < KeyboardInput.Length; i++)
{
    KeyboardInput[i] = (Keys)Enum.Parse(typeof(Keys), {{ Properties.Settings.Default.Keys[i]?? }});
}

Keys[] KeyboardInput = { Keys.A, Keys.S, Keys.N, Keys.M, Keys.H, Keys.F, Keys.T, Keys.G, Keys.W, Keys.Q, Keys.Z, Keys.X, Keys.Right, Keys.Left, Keys.Up, Keys.Down };

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
PRAGMA
  • 21
  • 3
  • the Settings are stored in string format (XML), so you can join them all in one string and then split the stored string and parse. Another approach is to look into XML or JSON serialization. – Slai Dec 31 '16 at 21:38
  • @EZI that doesnt seem to help in my case – PRAGMA Dec 31 '16 at 21:53
  • @PRAGMA you want to save some settings and read them later, What am I missing? – L.B Dec 31 '16 at 21:55
  • @PRAGMA I think you can use any object instead of dictionary in that question.. What is different in your case? – EZI Dec 31 '16 at 21:57

1 Answers1

3

Surprisingly the answer in the top Related question seems to work. After adding the setting, right click the Settings.settings in the Solution Explorer, Open With... any text editor, and change the type:

    <Setting Name="Keys" Type="System.Windows.Forms.Keys[]" Scope="User">

This will also update the Settings.Designer.cs file, and then you can use it like this:

Keys[] KeyboardInput = { Keys.A, Keys.S, Keys.N, Keys.M, Keys.H, Keys.F, Keys.T, Keys.G, 
                Keys.W, Keys.Q, Keys.Z, Keys.X, Keys.Right, Keys.Left, Keys.Up, Keys.Down };

Properties.Settings.Default.Keys = KeyboardInput;
Properties.Settings.Default.Save();

Keys[] keys = Properties.Settings.Default.Keys;
Slai
  • 22,144
  • 5
  • 45
  • 53