#region 快捷键相关
/// <summary>
/// 记录快捷键
/// </summary>
private void txtHotKey_KeyDown(object sender, KeyEventArgs e)
{
int HotKeyValue = 0;
string HotKeyString = "";
e.SuppressKeyPress = false;
e.Handled = true;
if (e.Modifiers != Keys.None)
{
switch (e.Modifiers)
{
case Keys.Control:
HotKeyString += "Ctrl + ";
HotKeyValue = (int)e.Modifiers;
break;
case Keys.Alt:
HotKeyString += "Alt + ";
HotKeyValue = (int)e.Modifiers;
break;
case Keys.Shift:
HotKeyString += "Shift + ";
HotKeyValue = (int)e.Modifiers;
break;
case Keys.Control | Keys.Alt:
HotKeyString += "Ctrl + Alt + ";
HotKeyValue = (int)e.Modifiers;
break;
case Keys.Control | Keys.Shift:
HotKeyString += "Ctrl + Shift + ";
HotKeyValue = (int)e.Modifiers;
break;
case Keys.Alt | Keys.Shift:
HotKeyString += "Alt + Shift + ";
HotKeyValue = (int)e.Modifiers;
break;
case Keys.Control | Keys.Alt | Keys.Shift:
HotKeyString += "Ctrl + Alt + Shift + ";
HotKeyValue = (int)e.Modifiers;
break;
}
if (e.KeyCode != Keys.None && e.KeyCode != Keys.ControlKey && e.KeyCode != Keys.Menu && e.KeyCode != Keys.ShiftKey)
{
HotKeyString += KeyCodeToString(e.KeyCode);
HotKeyValue += (int)e.KeyCode;
}
}
else
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
HotKeyString = "无";
HotKeyValue = -1;
}
else if (e.KeyCode != Keys.None)
{
HotKeyString = KeyCodeToString(e.KeyCode);
HotKeyValue = (int)e.KeyCode;
}
}
if (HotKeyValue == 0)
HotKeyValue = -1;
TextBox txtHotKey = (TextBox)sender;
txtHotKey.Text = HotKeyString;
txtHotKey.Tag = HotKeyValue;
txtHotKey.SelectionStart = txtHotKey.Text.Length;
}
/// <summary>
/// 将按键转换成相应字符
/// </summary>
/// <param name="KeyCode">按键</param>
/// <returns>字符</returns>
private string KeyCodeToString(Keys KeyCode)
{
if (KeyCode >= Keys.D0 && KeyCode <= Keys.D9)
{
return KeyCode.ToString().Remove(0, 1);
}
else if (KeyCode >= Keys.NumPad0 && KeyCode <= Keys.NumPad9)
{
return KeyCode.ToString().Replace("Pad", "");
}
else
{
return KeyCode.ToString();
}
}
/// <summary>
/// 设置按键不响应
/// </summary>
private void txtHotKey_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
/// <summary>
/// 释放按键后,若是无实际功能键,则置无
/// </summary>
private void txtHotKey_KeyUp(object sender, KeyEventArgs e)
{
CheckHotkey(sender);
}
/// <summary>
/// 失去焦点后,若是无实际功能键,则置无
/// </summary>
private void txtHotKey_LostFocus(object sender, EventArgs e)
{
CheckHotkey(sender);
}
/// <summary>
/// 检查是否无实际功能键,是则置无
/// </summary>
private void CheckHotkey(object sender)
{
TextBox txtHotKey = (TextBox)sender;
if (txtHotKey.Text.EndsWith(" + ") || String.IsNullOrEmpty(txtHotKey.Text))
{
txtHotKey.Text = "无";
txtHotKey.Tag = -1;
txtHotKey.SelectionStart = txtHotKey.Text.Length;
}
}
#endregion