无法检测上的keydown事件按Ctrl +快捷键,只要有一个只读的文本框,重点 [英] Can't detect a Ctrl + Key shortcut on keydown events whenever there's a readonly textbox with focus
问题描述
我想我通过自己解决了这个问题,但它回来困扰我的应用程序,使这里有云:
i thought i solved this problem by myself but it came back to haunt my application so here it goes:
我已经注册的形式与一对夫妇的残疾人和只读文本框下面的keydown事件处理程序,他们只对按键简单快捷方式:
i have the following keydown event handler registered in a form with a couple of disabled and readonly textboxes and they are only simple shortcuts for the buttons:
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();
}
的形式主要有preVIEW设置为true,但每当一个只读文本框具有焦点,并且我preSS按Ctrl + E我不能让Control.ModifierKeys == Keys.Control和e.Key code == Keys.E既如此在同一时间。什么是真正奇怪的是,按Ctrl + W的作品。任何人有任何想法,这到底是怎么回事? :(
the form has KeyPreview set to true but whenever a readonly textbox has focus and i press Ctrl + E i can't get "Control.ModifierKeys == Keys.Control" and "e.KeyCode == Keys.E" to be both true at the same time. What is really strange is that Ctrl + W works. Anyone has any idea what the hell is going on? :(
推荐答案
根据这个问题和的this 之一,它看起来像处理键盘快捷键的更普遍的方法是重写给ProcessCmdKey()方法:
According to this question and this one, It looks like a more general way to handle keyboard shortcuts is to override the ProcessCmdKey() method:
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.
这篇关于无法检测上的keydown事件按Ctrl +快捷键,只要有一个只读的文本框,重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!