我以为我自己解决了这个问题,但它又回来困扰我的应用程序,所以这里是:
我在带有几个禁用和只读文本框的表单中注册了以下 keydown 事件处理程序,它们只是按钮的简单快捷方式:
private void AccountViewForm_KeyDown(object sender, KeyEventArgs e)
{
//e.SuppressKeyPress = true;
//e.Handled = true;
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.E && !isInEditMode)
btnEditMode_Click(sender, e);
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.S && isInEditMode) btnEditMode_Click(sender, e);
if (e.KeyCode == Keys.Escape) btnCancel_Click(sender, e);
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.W) Close();
}
表单将 KeyPreview 设置为 true,但每当只读文本框具有焦点并且我按 Ctrl + E 时,我无法让“Control.ModifierKeys == Keys.Control”和“e.KeyCode == Keys.E”同时为 true同时。真正奇怪的是 Ctrl + W 有效。有人知道到底发生了什么事吗? :(
根据这个问题 https://stackoverflow.com/q/400113/4794 and this one https://stackoverflow.com/q/818092/4794,看起来处理键盘快捷键的更通用方法是重写 ProcessCmdKey() 方法:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.F)) {
MessageBox.Show("What the Ctrl+F?");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Have you considered using Alt + E and Alt + S and just setting the mnemonic property for your buttons? That seems to work well for me, and it's easier to set up.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)