windowcapture
исходный код / Models/KeyBindings.cs

KeyBindings.cs

137 строк · 5,651 байт · модуль Models
  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using System.Windows.Forms;
  5
  6namespace WindowCapture.Models
  7{
  8    public static class KeyBindings
  9    {
 10        static string bindingsPath = Path.Combine(
 11            AppDomain.CurrentDomain.BaseDirectory, "keybindings.ini");
 12
 13        // Global keys
 14        public static Keys CaptureKey = Keys.Menu; // Alt
 15
 16        // Editor keys
 17        public static Keys CopyAndClose = Keys.Space;
 18        public static Keys Undo = Keys.Z; // with Ctrl
 19        public static Keys Cancel = Keys.Escape;
 20        public static Keys ToggleAltMode = Keys.Menu;
 21
 22        // Mouse buttons (stored as int for serialization)
 23        public static MouseButtons DrawRect = MouseButtons.Left;
 24        public static MouseButtons DrawArrow = MouseButtons.Right;
 25        public static MouseButtons PlaceMarker = MouseButtons.Middle;
 26        public static MouseButtons WindowSelect = MouseButtons.Middle; // with Alt
 27        public static MouseButtons RegionUnion = MouseButtons.Left; // with Alt drag
 28
 29        // Action names for UI
 30        public static Dictionary<string, string> ActionNames = new Dictionary<string, string>
 31        {
 32            { "CaptureKey", "Захват экрана (глобальная)" },
 33            { "CopyAndClose", "Копировать и закрыть" },
 34            { "Undo", "Отменить (с Ctrl)" },
 35            { "Cancel", "Отмена/Закрыть" },
 36            { "ToggleAltMode", "Режим автоопределения" },
 37            { "DrawRect", "Рисовать прямоугольник (мышь)" },
 38            { "DrawArrow", "Рисовать стрелку (мышь)" },
 39            { "PlaceMarker", "Поставить маркер (мышь)" },
 40            { "WindowSelect", "Выделить окно (мышь + Alt)" },
 41            { "RegionUnion", "Объединить области (мышь + Alt)" }
 42        };
 43
 44        public static void Load()
 45        {
 46            try
 47            {
 48                if (!File.Exists(bindingsPath)) return;
 49                foreach (var line in File.ReadAllLines(bindingsPath))
 50                {
 51                    // Split on the first '=' only; skip malformed lines without aborting the load.
 52                    int eq = line.IndexOf('=');
 53                    if (eq < 0) continue;
 54                    var key = line.Substring(0, eq).Trim();
 55                    var val = line.Substring(eq + 1).Trim();
 56
 57                    Keys k; MouseButtons mb;
 58                    switch (key)
 59                    {
 60                        case "CaptureKey": if (Enum.TryParse(val, out k)) CaptureKey = k; break;
 61                        case "CopyAndClose": if (Enum.TryParse(val, out k)) CopyAndClose = k; break;
 62                        case "Undo": if (Enum.TryParse(val, out k)) Undo = k; break;
 63                        case "Cancel": if (Enum.TryParse(val, out k)) Cancel = k; break;
 64                        case "ToggleAltMode": if (Enum.TryParse(val, out k)) ToggleAltMode = k; break;
 65                        case "DrawRect": if (Enum.TryParse(val, out mb)) DrawRect = mb; break;
 66                        case "DrawArrow": if (Enum.TryParse(val, out mb)) DrawArrow = mb; break;
 67                        case "PlaceMarker": if (Enum.TryParse(val, out mb)) PlaceMarker = mb; break;
 68                        case "WindowSelect": if (Enum.TryParse(val, out mb)) WindowSelect = mb; break;
 69                        case "RegionUnion": if (Enum.TryParse(val, out mb)) RegionUnion = mb; break;
 70                    }
 71                }
 72            }
 73            catch { }
 74        }
 75
 76        public static void Save()
 77        {
 78            try
 79            {
 80                var dir = Path.GetDirectoryName(bindingsPath);
 81                if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
 82
 83                var lines = new List<string>
 84                {
 85                    "CaptureKey=" + CaptureKey,
 86                    "CopyAndClose=" + CopyAndClose,
 87                    "Undo=" + Undo,
 88                    "Cancel=" + Cancel,
 89                    "ToggleAltMode=" + ToggleAltMode,
 90                    "DrawRect=" + DrawRect,
 91                    "DrawArrow=" + DrawArrow,
 92                    "PlaceMarker=" + PlaceMarker,
 93                    "WindowSelect=" + WindowSelect,
 94                    "RegionUnion=" + RegionUnion
 95                };
 96                File.WriteAllLines(bindingsPath, lines);
 97            }
 98            catch { }
 99        }
100
101        public static void ResetDefaults()
102        {
103            CaptureKey = Keys.Menu;
104            CopyAndClose = Keys.Space;
105            Undo = Keys.Z;
106            Cancel = Keys.Escape;
107            ToggleAltMode = Keys.Menu;
108            DrawRect = MouseButtons.Left;
109            DrawArrow = MouseButtons.Right;
110            PlaceMarker = MouseButtons.Middle;
111            WindowSelect = MouseButtons.Middle;
112            RegionUnion = MouseButtons.Left;
113        }
114
115        public static string GetKeyName(Keys key)
116        {
117            switch (key)
118            {
119                case Keys.Menu: return "Alt";
120                case Keys.ControlKey: return "Ctrl";
121                case Keys.ShiftKey: return "Shift";
122                default: return key.ToString();
123            }
124        }
125
126        public static string GetMouseButtonName(MouseButtons btn)
127        {
128            switch (btn)
129            {
130                case MouseButtons.Left: return "ЛКМ";
131                case MouseButtons.Right: return "ПКМ";
132                case MouseButtons.Middle: return "СКМ";
133                default: return btn.ToString();
134            }
135        }
136    }
137}