C# WinForm快捷键设置技巧

2019-12-26 14:14:05王冬梅
  •   else    { 
  •     return KeyCode.ToString();    } 
  • }  /// 
  • /// 设置按键不响应  /// 
  • private void txtHotKey_KeyPress(object sender, KeyPressEventArgs e)  { 
  •   e.Handled = true;  } 
  • ///  /// 释放按键后,若是无实际功能键,则置无 
  • ///  private void txtHotKey_KeyUp(object sender, KeyEventArgs e) 
  • {    CheckHotkey(sender); 
  • }  /// 
  • /// 失去焦点后,若是无实际功能键,则置无  /// 
  • private void txtHotKey_LostFocus(object sender, EventArgs e)  { 
  •   CheckHotkey(sender);  } 
  • ///  /// 检查是否无实际功能键,是则置无 
  • ///  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

    #实现快捷键(系统热键)响应

    在应用中,我们可能会需要实现像Ctrl+C复制、Ctrl+V粘贴这样的快捷键,本文简单介绍了它的实现,并给出了一个实现类。

    (1)建立一个类文件,命名为HotKey.cs,代码如下: