[英]C# Prevent Hotkeys while TextBox focus
I have a program containing multiple C# Forms TextBoxes. 我有一个包含多个C#窗体文本框的程序。 I've set up Hotkeys for the entire form activating certain functions.
我已经为激活某些功能的整个表单设置了热键。 My problem is that my Hotkeys have been set onto the Form KeyDown event and they activate if I write something on a TextBox.
我的问题是我的热键已设置到Form KeyDown事件上,并且如果我在TextBox上写东西,它们就会激活。
Example : One Hotkey might be I. Everytime I write the letter onto a textbox the Hotkey activates. 示例 :一个热键可能是I。每次我将字母写到文本框时,热键都会激活。
Alterior solutions and problems : I've thought about putting a Key in front of the Hotkey like CTRL+Hotkey, but these also present problems as CTRL+C is Windows Copy command etc. SHIFT is an UpperKey button. 先前的解决方案和问题 :我曾考虑过将CTRL + Hotkey之类的Key放在热键的前面,但是由于CTRL + C是Windows Copy命令等,这些也存在问题。SHIFT是UpperKey按钮。
Question : Can I prevent Hotkeys from activating when I am writing onto a TextBox without having to go through all of them in the form? 问题 :当我在不使用表单中的所有内容的情况下写文本框时,是否可以阻止激活热键?
EDIT: Some code as requested. 编辑:一些要求的代码。 The button codes come from a stored XML file or the Hotkeys Form+Class (separate) where I've set up a window for them.
按钮代码来自存储的XML文件或快捷键Form + Class(单独),在其中为它们设置了一个窗口。
public Hotkeys hotkeysForm = new Hotkeys();
void Form1_KeyDown(object sender, KeyEventArgs e)
{
toggleInformation = hotkeysForm.toggleInformation;
if (e.Control && e.KeyCode == toggleInformation)
{
showInfo(true);
}
else if (e.KeyCode == toggleInformation)
{
if (!isInfoActive)
showInfo();
else
hideInfo();
}
}
You should try this hack, if it could solve your problem, Create a Extented TextBox and use it in your code. 如果可以解决您的问题,则应尝试使用此技巧,创建一个扩展的文本框并在代码中使用它。 you can handle whether to write the pressed key in textbox or not in hotkeyPressed check.
您可以处理是否在文本框中编写按下的键,也可以在hotkeyPressed检查中进行处理。
public class ETextBox : System.Windows.Forms.TextBox
{
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (hotKeyPressed) // this is the condition when you don't want to write in text.
{
//Do whatever you want to do in this case.
}
else
{
base.OnKeyDown(e);
}
}
}
You can disable hotkeys while texbox is an active control. 当texbox是活动控件时,您可以禁用热键。 Add the
Enter
and Leave
events for all textboxes: 为所有文本框添加
Enter
和Leave
事件:
private void textBox_Enter(object sender, EventArgs e)
{
KeyPreview = false;
}
private void textBox_Leave(object sender, EventArgs e)
{
KeyPreview = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.