Как настроить сохранение настроек биндов? И как сделать бинд клавиши на другую клавишу чтобы эмулировать нажатие?
Суть в чём? Я пытаюсь написать код C# в VS 2022 на .NET Framework, который будет сохранять все настройки программы в файл JSON, но я не понимаю, что нужно написать внутри файла JSON, нужно ли добавлять файл AppSettings для того, чтобы шлёпнуть код который будет связывать форму настроек и файл JSON? А почему двойной вопрос? Потому что оно всё связано. Мне нужно сделать двойной бинд а не получается связать это всё. Обычные бинды получаются а связать по схеме клавиша = клавиша + клавиша / системное действие нет.
Вот что у меня в AppSettings если он нужен:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Programm
{
public class AppSettings
{
public List<HotKeysArrange> HotKeysList { get; set; }
public AppSettings()
{
HotKeysList = new List<HotKeysArrange>();
}
public void SaveToFile(string filePath)
{
try
{
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(filePath, json);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving settings: {ex.Message}");
}
}
public static AppSettings LoadFromFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<AppSettings>(json);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading settings: {ex.Message}");
}
return new AppSettings();
}
}
}
Вот что в форме настроек биндов:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq.Expressions;
namespace Programm
{
public partial class RebinderForm : Form
{
public static List<HotKeysArrange> HotkeysLis = new();
public List<Keys?> KeysListTemp = new();
public RebinderForm()
{
InitializeComponent();
KeyPreview = true;
}
private void RebinderForm_KeyDown(object sender, KeyEventArgs e)
{
Debug.WriteLine(e.KeyCode);
if (!KeysListTemp.Contains(e.KeyCode) && HotkeysLis.Any(hotkey => hotkey.OneKey == e.KeyCode))
KeysListTemp.Add(e.KeyCode);
if (!KeysListTemp.Contains(e.KeyCode) && HotkeysLis.Any(hotkey => hotkey.TwoKey == e.KeyCode))
KeysListTemp.Add(e.KeyCode);
if (!KeysListTemp.Contains(e.KeyCode) && HotkeysLis.Any(hotkey => hotkey.FreeKey == e.KeyCode))
KeysListTemp.Add(e.KeyCode);
#region чек нажатых клавиш.
if (KeysListTemp.Count == 1)
{
var temp = HotkeysLis.Where(hotKey => KeysListTemp.Contains(h`введите сюда код`otKey.OneKey) && hotKey.TwoKey == null && hotKey.FreeKey == null).FirstOrDefault();
if (temp != null)
{
temp.Action();
KeysListTemp.Clear();
}
}
if (KeysListTemp.Count == 2)
{
var temp = HotkeysLis.Where(hotKey => KeysListTemp.Contains(hotKey.OneKey) && KeysListTemp.Contains(hotKey.TwoKey) && hotKey.FreeKey == null).FirstOrDefault();
if (temp != null)
{
temp.Action();
KeysListTemp.Clear();
}
}
if (KeysListTemp.Count == 3)
{
var temp = HotkeysLis.Where(hotKey => KeysListTemp.Contains(hotKey.OneKey) && KeysListTemp.Contains(hotKey.TwoKey) && KeysListTemp.Contains(hotKey.FreeKey)).FirstOrDefault();
if (temp != null)
{
temp.Action();
KeysListTemp.Clear();
}
}
#endregion
}
private void RebinderForm_KeyUp(object sender, KeyEventArgs e) => KeysListTemp.Clear();
#region Методы для которых будут задаваться комбинации клавиш
public void OneMessage() => MessageBox.Show(label1.Text);
public void TwoMessage() => MessageBox.Show(label2.Text);
public void FreeMessage() => MessageBox.Show(label3.Text);
#endregion
#region передача методов для привязки горячих клавиш.
private void BindBox1_Click(object sender, EventArgs e)
{
BindingHotkeyForm form = new(OneMessage, BindBox1);
form.Show();
}
private void BindBox2_Click(object sender, EventArgs e)
{
BindingHotkeyForm form = new(TwoMessage, BindBox2);
form.Show();
}
private void BindBox3_Click(object sender, EventArgs e)
{
BindingHotkeyForm form = new(FreeMessage, BindBox3);
form.Show();
}
private void ActionBox1_Click(object sender, EventArgs e)
{
BindingHotkeyForm form = new(OneMessage, ActionBox1);
form.Show();
}
private void ActionBox2_Click(object sender, EventArgs e)
{
BindingHotkeyForm form = new(TwoMessage, ActionBox2);
form.Show();
}
private void ActionBox3_Click(object sender, EventArgs e)
{
BindingHotkeyForm form = new(FreeMessage, ActionBox3);
form.Show();
}
#endregion
private void CancelRBN_Click(object sender, EventArgs e) => Close();
private void SaveRBN_Click(object sender, EventArgs e)
{
AppSettings appSettings = new AppSettings();
appSettings.HotKeysList = HotkeysLis;
string filePath = Path.Combine(Application.StartupPath, "sett.json");
appSettings.SaveToFile(filePath);
this.Close();
}
#region
private void RebinderForm_Load(object sender, EventArgs e)
{
}
private void BindBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
#endregion
}
public class HotKeysArrange
{
public Action Action { get; set; }
public Keys? OneKey { get; set; }
public Keys? TwoKey { get; set; }
public Keys? FreeKey { get; set; }
}
}