random-file-runner/RyzStudio/Windows/ThemedForms/TextBox/TKeyCodeTextBox.cs

97 lines
2.8 KiB
C#
Raw Permalink Normal View History

2021-10-24 15:49:48 +00:00
using System.Text;
using System.Windows.Forms;
using UIResources = RandomFileRunner.UIResource;
namespace RyzStudio.Windows.ThemedForms
{
public class TKeyCodeTextBox : TButtonTextBox
{
public class Results
{
public bool IsCtrl { get; set; } = false;
public bool IsAlt { get; set; } = false;
public bool IsShift { get; set; } = false;
public Keys Key { get; set; } = Keys.None;
public int ModifierCode => ((this.IsAlt ? 1 : 0) + (this.IsCtrl ? 2 : 0) + (this.IsShift ? 4 : 0));
public int KeyCode => (int)this.Key;
public string DisplayText
{
get
{
StringBuilder sb = new StringBuilder();
if (this.IsCtrl) sb.Append("Ctrl+");
if (this.IsShift) sb.Append("Shift+");
if (this.IsAlt) sb.Append("Alt+");
sb.Append(this.Key.ToString());
return sb.ToString();
}
}
public void Clear()
{
this.IsCtrl = false;
this.IsAlt = false;
this.IsShift = false;
this.Key = Keys.None;
}
}
public Results KeyCodeResults { get; set; } = new Results();
public TKeyCodeTextBox() : base()
{
this.NormalImage = UIResources.trash;
this.HighlightImage = UIResources.trash2;
this.Text = string.Empty;
this.InnerTextBox.ReadOnly = true;
this.InnerTextBox.PreviewKeyDown += textBox1_PreviewKeyDown;
}
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.ControlKey) return;
if (e.KeyCode == Keys.ShiftKey) return;
if (e.KeyCode == Keys.Menu) return;
this.KeyCodeResults.IsCtrl = e.Control;
this.KeyCodeResults.IsAlt = e.Alt;
this.KeyCodeResults.IsShift = e.Shift;
this.KeyCodeResults.Key = e.KeyCode;
this.Text = this.KeyCodeResults.DisplayText;
}
protected override void imageBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
this.KeyCodeResults.Clear();
this.Text = this.KeyCodeResults.DisplayText;
}
public void UpdateKeyCode(bool isCtrl, bool isAlt, bool isShift, Keys key)
{
this.KeyCodeResults.IsCtrl = isCtrl;
this.KeyCodeResults.IsAlt = isAlt;
this.KeyCodeResults.IsShift = isShift;
this.KeyCodeResults.Key = key;
this.Text = this.KeyCodeResults.DisplayText;
}
}
}