math-suite/Source/TibisMathematicsSuite/KeyboardIO/KeyShortcutBox.cs

112 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace TibisMathematicsSuite.KeyboardIO
{
public class KeyShortcutBox : TextBox
{
#region Variables
private KeyMessageFilter keyFilter = new KeyMessageFilter();
private MyKey currentKey = new MyKey();
private bool inputFinished = false;
#endregion
#region Properties
public MyKey Key
{
get {
return MyKey.Parse(this.Text);
}
}
#endregion
#region Constructor
public KeyShortcutBox() : base()
{
this.ReadOnly = true;
this.BackColor = SystemColors.Window;
Application.AddMessageFilter(keyFilter);
keyFilter.KeyDown += new EventHandler<KeyEventArgs>(keyFilter_KeyDown);
keyFilter.KeyUp += new EventHandler<KeyEventArgs>(keyFilter_KeyUp);
}
#endregion
#region Key updating
private void keyFilter_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.LMenu:
case Keys.RMenu:
case Keys.Menu: currentKey.Mods &= ~MyKey.Modifiers.Alt; break;
case Keys.LShiftKey:
case Keys.RShiftKey:
case Keys.ShiftKey: currentKey.Mods &= ~MyKey.Modifiers.Shift; break;
case Keys.LControlKey:
case Keys.RControlKey:
case Keys.ControlKey: currentKey.Mods &= ~MyKey.Modifiers.Ctrl; break;
case Keys.LWin:
case Keys.RWin: currentKey.Mods &= ~MyKey.Modifiers.Win; break;
default:
inputFinished = true;
currentKey.Key = Keys.None;
break;
}
updateKey();
}
private void keyFilter_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.LMenu:
case Keys.RMenu:
case Keys.Menu: currentKey.Mods |= MyKey.Modifiers.Alt; break;
case Keys.LShiftKey:
case Keys.RShiftKey:
case Keys.ShiftKey: currentKey.Mods |= MyKey.Modifiers.Shift; break;
case Keys.LControlKey:
case Keys.RControlKey:
case Keys.ControlKey: currentKey.Mods |= MyKey.Modifiers.Ctrl; break;
case Keys.LWin:
case Keys.RWin: currentKey.Mods |= MyKey.Modifiers.Win; break;
default: currentKey.Key = e.KeyCode; break;
}
inputFinished = false;
updateKey();
}
private void updateKey()
{
if (!inputFinished && this.Focused && this.Enabled)
this.Text = currentKey.ToString();
}
#endregion
#region Reset
public void Reset()
{
this.Text = "";
this.inputFinished = false;
}
#endregion
}
}