我正在尝试使用文本框中的keydown事件将键盘快捷键(例如(Ctrl + A,Ctrl + Shift + X))模拟到文本框中,但我有点进退两难。我正在使用列表框来记录模拟键盘快捷键时引发的事件,但它会运行事件两次。我问我如何才能使它只在登录框中登录快捷键,如果它只有一个修饰键(ctrl,alt,shift,winkey)+一个字母数字键(a-z 0-9)?
private void txtShortcut_KeyDown(object sender, KeyEventArgs e)
{
// Example key press: Ctrl + A
if (e.Shift || e.Control || e.Alt)
{
string s = (e.Shift ? Keys.ShiftKey.ToString() + " + " : "") + // Shift
(e.Control ? Keys.ControlKey.ToString() + " + " : "") + // Control
(e.Alt ? Keys.Menu.ToString() + " + " : "") + // Alt (menu)
e.KeyCode; // Key.
lbLogger.Items.Add(s);
// Logger Results:
// 1) ControlKey + ControlKey
// 2) ControlKey + A
// * I'm trying to get it to only post the second line and only log the line
// when a modifier key + a-z 0-9 key is pressed with it.
}
}
如果按下修饰键+ a-z或0-9键,那么只进行此记录的最有效方法是什么?
答案 0 :(得分:2)
private void txtShortcut_KeyDown(object sender, KeyEventArgs e)
{
// Example key press: Ctrl + A
if ((e.Shift || e.Control || e.Alt) &&
(((e.KeyCode >= Keys.a) && (e.KeyCode <= Keys.z)) ||
((e.KeyCode >= Keys.A) && (e.KeyCode <= Keys.Z)) ||
((e.KeyCode >= Keys.NumPad0) && (e.KeyCode <= Keys.NumPad9)) ||
((e.KeyCode >= Keys.D0) && (e.KeyCode <= Keys.D9))))
{
string s = (e.Shift ? Keys.ShiftKey.ToString() + " + " : "") + // Shift
(e.Control ? Keys.ControlKey.ToString() + " + " : "") + // Control
(e.Alt ? Keys.Menu.ToString() + " + " : "") + // Alt (menu)
e.KeyCode; // Key.
lbLogger.Items.Add(s);
// Logger Results:
// 1) ControlKey + ControlKey
// 2) ControlKey + A
// * I'm trying to get it to only post the second line and only log the line
// when a modifier key + a-z 0-9 key is pressed with it.
}
}