我们在软件中经常用到快捷键,这里整理备份一下。
首先我们要定义可以作为快捷键的按键,以下是整理的 一些,自己可以根据情况来修改
public static Dictionary
{
{,""},
{,""},
{,""},
{,""},
{,""},
{,""},
{,""},
{,""},
{,""},
{,""},
{,"A"},
{,"B"},
{,"C"},
{,"D"},
{,"E"},
{,"F"},
{,"G"},
{,"H"},
{,"I"},
{,"J"},
{,"K"},
{,"L"},
{,"M"},
{,"N"},
{,"O"},
{,"P"},
{,"Q"},
{,"R"},
{,"S"},
{,"T"},
{,"U"},
{,"V"},
{,"W"},
{,"X"},
{,"Y"},
{,"Z"},
{,"F1"},
{,"F2"},
{,"F3"},
{,"F4"},
{,"F5"},
{,"F6"},
{,"F7"},
{,"F8"},
{,"F9"},
{,"F10"},
{,"F11"},
{,"F12"},
{,"PageUp"},
{,"PageDown"},
{,"End"},
{,"Home"},
{,"←"},
{,"↑"},
{,"→"},
{,"↓"},
{,"Insert"},
{,"Delete"},
{,";"},
{,"="},
{,","},
{,"-"},
{,"."},
{,"/"},
{,"`"},
{,"["},
{,"\\"},
{,"]"}
};
然后弄个数据表存一下定义功能对应的快捷键,表结构如下
CREATE TABLE [dbo].[HotKey](
[ID] [int] IDENTITY(,) NOT NULL,
[FrmName] [nvarchar]() NULL,
[Sort] [int] NULL,
[ActionName] [nvarchar]() NULL,
[ActionKey] [nvarchar]() NULL,
[KeyValue] [nvarchar]() NULL
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'界面' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotKey', @level2type=N'COLUMN',@level2name=N'FrmName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'顺序' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotKey', @level2type=N'COLUMN',@level2name=N'Sort'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'功能名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotKey', @level2type=N'COLUMN',@level2name=N'ActionName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'功能编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotKey', @level2type=N'COLUMN',@level2name=N'ActionKey'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'热键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotKey', @level2type=N'COLUMN',@level2name=N'KeyValue'
GO
系统启动的时候缓存一下设置的快捷键
public static List
基类窗体里面写窗体的
写KeyUP 和KeyDown事件
bool m_blnCtrl = false;
bool m_blnAlt = false;
bool m_blnShift = false;
\[Description("定义的热键列表"), Category("自定义")\]
public Dictionary<int, string> HotKeys { get; set; }
//如果事件返回true则表示已经找到了快捷键事件,则不再向下传递
public delegate bool HotKeyEventHandler(string strHotKey);
/// <summary>
/// 热键事件
/// </summary>
\[Description("热键事件"), Category("自定义")\]
public event HotKeyEventHandler HotKeyDown;
protected void FrmBase_KeyUp(object sender, KeyEventArgs e)
{
if (HotKeyDown != null && HotKeys != null)
{
if (e.KeyCode == Keys.Control || e.KeyCode == Keys.ControlKey)
m_blnCtrl = false;
if (e.KeyCode == Keys.Alt || e.KeyCode == Keys.Menu)
m_blnAlt = false;
if (e.KeyCode == Keys.Shift || e.KeyCode == Keys.ShiftKey)
m_blnShift = false;
}
}
protected void FrmBase\_KeyDown(object sender, KeyEventArgs e)
{
if (HotKeyDown != null && HotKeys != null)
{
if (e.KeyCode == Keys.Control || e.KeyCode == Keys.ControlKey)
m\_blnCtrl = true;
if (e.KeyCode == Keys.Alt || e.KeyCode == Keys.Menu)
m\_blnAlt = true;
if (e.KeyCode == Keys.Shift || e.KeyCode == Keys.ShiftKey)
m\_blnShift = true;
if (HotKeys.ContainsKey(e.KeyValue))
{
string strKey = string.Empty;
if (m\_blnCtrl)
{
strKey += "Ctrl+";
}
if (m\_blnAlt)
{
strKey += "Alt+";
}
if (m\_blnShift)
{
strKey += "Shift+";
}
strKey += HotKeys\[e.KeyValue\];
if (HotKeyDown(strKey))
{
e.Handled = true;
e.SuppressKeyPress = true;
}
}
}
}
你会注意到keyup和kedown事件的修饰符是protected,为什么要用protected呢?是因为窗体的一些输入控件的按键事件是不会传递到窗体的,比如textbox这些,所有需要手动指定一下
子窗体中注册事件就可以了,如果你没有用基类窗体,那么直接写在一起就可以了
this.HotKeys = HotKeys;
this.HotKeyDown += FrmMain_HotKeyDown;
//将输入框的2个事件指向窗体的2个事件
this.txtSearch.KeyDown += this.FrmBase_KeyDown;
this.txtSearch.KeyUp += this.FrmBase_KeyUp;
///
/// 热键值
///
bool FrmMain_HotKeyDown(string strHotKey)
{
Console.WriteLine(strHotKey);
string strHotAction = GetHotKeyAction("FrmMain", strHotKey);
if (string.IsNullOrEmpty(strHotAction))
return false;
else
{
//这里根据返回的功能名称做操作就可以了
return true;
}
}
public static string GetHotKeyAction(string strFrom, string strHotKey)
{
var entity = HotKeySetting.Find(p => p.FrmName.ToLower() == strFrom.ToLower() && p.KeyValue.ToStringExt().ToLower() == strHotKey.ToLower());
if (entity != null)
return entity.ActionKey;
else
return string.Empty;
}
这样就可以了
手机扫一扫
移动阅读更方便
你可能感兴趣的文章