在 KeyDown 事件上筛选 KeyCode 的更有效方法

发布于 2024-11-06 11:20:15 字数 2141 浏览 5 评论 0原文

当用户尝试使用 KeyDown 事件仅在表单字段中输入有用数据时,我试图触发一个事件来执行一些工作。但是,我不断收到误报,因为 KeyDown 事件适用于任何键!

我试图不让“Alt、Control、Shift、Esc、F 键等”等按钮触发事件。这样做的最佳方法是什么?

到目前为止我所得到的是:

    private void formControl_KeyModified(object sender, KeyEventArgs e)
    {
        if (e.KeyCode != Keys.Shift && e.KeyCode != Keys.CapsLock && e.KeyCode != Keys.Tab && e.KeyCode != Keys.Escape &&
            e.KeyCode != Keys.Insert && e.KeyCode != Keys.Home && e.KeyCode != Keys.End && e.KeyCode != Keys.PageUp &&
            e.KeyCode != Keys.PageDown && e.KeyCode != Keys.Up && e.KeyCode != Keys.Down && e.KeyCode != Keys.Left &&
            e.KeyCode != Keys.Right && e.KeyCode != Keys.Control && e.KeyCode != Keys.Alt && e.KeyCode != Keys.NumLock &&
            e.KeyCode != Keys.Insert && e.KeyCode != Keys.None && e.KeyCode != Keys.PrintScreen && e.KeyCode != Keys.Help &&
            e.KeyCode != Keys.ControlKey && e.KeyCode != Keys.ShiftKey && e.KeyCode != Keys.Sleep && e.KeyCode != Keys.LWin &&
            e.KeyCode != Keys.RWin && e.KeyCode != Keys.RMenu && e.KeyCode != Keys.LMenu && e.KeyCode != Keys.LShiftKey &&
            e.KeyCode != Keys.RShiftKey && e.KeyCode != Keys.Pause && e.KeyCode != Keys.F1 && e.KeyCode != Keys.F2 &&
            e.KeyCode != Keys.F3 && e.KeyCode != Keys.F4 && e.KeyCode != Keys.F5 && e.KeyCode != Keys.F6 && e.KeyCode != Keys.F7 &&
            e.KeyCode != Keys.F8 && e.KeyCode != Keys.F9 && e.KeyCode != Keys.F10 && e.KeyCode != Keys.F11 && e.KeyCode != Keys.F12 &&
            e.KeyCode != Keys.L)
        {
             // Do some work...
        }
    }

然而,这对我来说似乎并不是处理这个问题的最佳方法。 同样,我只是想获取可以输入文本框的字符的键(例如 213135udf!@#%@!#@#%15nfaosdf~!@}{:?> ;, 等等)!任何帮助将不胜感激,谢谢!

真挚地, Isaac D.

(为了清晰和质量而编辑)

I'm trying to fire an event perform some work when the user tries to enter only useful data into a form-field using the KeyDown event. But, I keep getting false alarms because the KeyDown event works for just any key!

I'm trying not to make the event fire for buttons such as "Alt, Control, Shift, Esc, the F-keys, etc." What's the best way of doing this?

What I have so far is this:

    private void formControl_KeyModified(object sender, KeyEventArgs e)
    {
        if (e.KeyCode != Keys.Shift && e.KeyCode != Keys.CapsLock && e.KeyCode != Keys.Tab && e.KeyCode != Keys.Escape &&
            e.KeyCode != Keys.Insert && e.KeyCode != Keys.Home && e.KeyCode != Keys.End && e.KeyCode != Keys.PageUp &&
            e.KeyCode != Keys.PageDown && e.KeyCode != Keys.Up && e.KeyCode != Keys.Down && e.KeyCode != Keys.Left &&
            e.KeyCode != Keys.Right && e.KeyCode != Keys.Control && e.KeyCode != Keys.Alt && e.KeyCode != Keys.NumLock &&
            e.KeyCode != Keys.Insert && e.KeyCode != Keys.None && e.KeyCode != Keys.PrintScreen && e.KeyCode != Keys.Help &&
            e.KeyCode != Keys.ControlKey && e.KeyCode != Keys.ShiftKey && e.KeyCode != Keys.Sleep && e.KeyCode != Keys.LWin &&
            e.KeyCode != Keys.RWin && e.KeyCode != Keys.RMenu && e.KeyCode != Keys.LMenu && e.KeyCode != Keys.LShiftKey &&
            e.KeyCode != Keys.RShiftKey && e.KeyCode != Keys.Pause && e.KeyCode != Keys.F1 && e.KeyCode != Keys.F2 &&
            e.KeyCode != Keys.F3 && e.KeyCode != Keys.F4 && e.KeyCode != Keys.F5 && e.KeyCode != Keys.F6 && e.KeyCode != Keys.F7 &&
            e.KeyCode != Keys.F8 && e.KeyCode != Keys.F9 && e.KeyCode != Keys.F10 && e.KeyCode != Keys.F11 && e.KeyCode != Keys.F12 &&
            e.KeyCode != Keys.L)
        {
             // Do some work...
        }
    }

However, that doesn't quite seem like the best way to handle this to me. Again, I'm just trying to get keys for the characters that could be entered into a textbox (such as 213135udf!@#%@!#@#%15nfaosdf~!@}{:?>, and so on)! Any help at all will be appreciated, thanks!

Sincerely,
Isaac D.

(Edited for clarity and quality)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泪冰清 2024-11-13 11:20:15

您可以将所有值放入 HashSet< /a> 并检查 KeyCode 是否在集合中。

var invalidKeys = new HashSet<Keys> { Keys.Shift, Keys.CapsLock, Keys.Tab, ... Keys.L };
if (!invalidKeys.Contains(e.KeyCode))
{
    // Do some work...
}

或者,由于您正在检查是否相等,因此您可以将所有内容放入 switch 语句中。

switch (e.KeyCode)
{
case Keys.Shift:
case Keys.CapsLock:
case Keys.Tab:
// ...
case Keys.L:
    break;
default:
    // Do some work...
    break;
}

You could throw all values into a HashSet<T> and check if the KeyCode is in the set.

var invalidKeys = new HashSet<Keys> { Keys.Shift, Keys.CapsLock, Keys.Tab, ... Keys.L };
if (!invalidKeys.Contains(e.KeyCode))
{
    // Do some work...
}

Or alternatively, since you're checking for equality, you could just throw all that into a switch statement.

switch (e.KeyCode)
{
case Keys.Shift:
case Keys.CapsLock:
case Keys.Tab:
// ...
case Keys.L:
    break;
default:
    // Do some work...
    break;
}
娇纵 2024-11-13 11:20:15

例如,您可以(有很多好的尝试)检查此页面 有关 Char 类的帮助,您可以在其中使用 IsLetterOrDigit 或其他函数。现在我无法识别您是否使用 Windows 窗体?如果是这样,请使用像 (char)e.KeyCode 这样的简单转换来获取字符。

例子:

private void formControl_KeyModified(object sender, KeyEventArgs e)
{
    char c = (char)e.KeyCode;
    if (Char.IsLetterOrDigit(c)) {
        // useful
    }
    // might add more checks
    // else if (Char.IsPunctuation(c)) ...
}

you can for example (there are many good attempts) check this page for help on the Char class where you can use methods like IsLetterOrDigit or other functions. Now I could not recognise if you are using Windows Forms? If so, use a simple cast like (char)e.KeyCode to get the char.

Example:

private void formControl_KeyModified(object sender, KeyEventArgs e)
{
    char c = (char)e.KeyCode;
    if (Char.IsLetterOrDigit(c)) {
        // useful
    }
    // might add more checks
    // else if (Char.IsPunctuation(c)) ...
}
2024-11-13 11:20:15

您可以处理 KeyPress 事件形式。上述事件采用 KeyPressEventArgs 作为其参数范围。

使用 Char.IsLetterOrDigit 函数检查 KeyPressEventArgs.KeyChar 属性。

private void form_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

     if (char.IsLetterOrDigit(e.KeyChar)) {}
     else { e.Handled = false; }

}

编辑:

您还可以尝试列出可接受的 Char 值,然后检查其中是否包含预置字符:

private void form_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

     List<Char> charList = new List<Char>;
     charList.AddRange(new Char[] { 'a', 'b' ... });

     if (charList.Contains(e.KeyChar)) {}
     else { e.Handled = false; }

}

您可能需要考虑结合使用这两种方式或甚至更多地满足您的要求。

You can handle the KeyPress event of the form. The mentioned event take a KeyPressEventArgs as its arguments parameter.

Use the Char.IsLetterOrDigit function to check the value of the KeyPressEventArgs.KeyChar property.

private void form_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

     if (char.IsLetterOrDigit(e.KeyChar)) {}
     else { e.Handled = false; }

}

EDIT:

You can also try to make a list of your accepted Char values, then check if the preseed character is included in it:

private void form_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

     List<Char> charList = new List<Char>;
     charList.AddRange(new Char[] { 'a', 'b' ... });

     if (charList.Contains(e.KeyChar)) {}
     else { e.Handled = false; }

}

You may need to consider combining both ways or even more to fulfill your requirements.

静待花开 2024-11-13 11:20:15

如果您关心 if 语句的执行时间,请创建 Key 值的 SortedList 并检查 SortedList 是否包含您的键。

可能更好的解决方案是使用表单文本框“TextChanged”事件而不是使用 KeyDown 事件。

If you are concerned with the execution time of the if statement, create a SortedList of the Key values and check if the SortedList contains your key.

A possibly better solution is to use the Forms TextBox "TextChanged" event rather than using the KeyDown event.

从来不烧饼 2024-11-13 11:20:15

就像 @Daniel 在他的评论中指出的那样,也许将有效密钥列入白名单比将所有您不感兴趣的密钥列入黑名单更好。因此,如果您只对字母键和数字感兴趣,则可以按照 msdn 密钥示例

if(e.KeyCode > Keys.NumPad0 && e.KeyCode < Keys.NumPad9 ||
   e.KeyCode > Keys.D0 && e.KeyCode < Keys.D9 ||
   e.KeyCode > Keys.A && e.KeyCode < Keys.Z) {

    //do useful stuff here
}

Like @Daniel states in his comment, perhaps white-listing the valid keys is preferable than black-listing all those that are of no interest to you. So if, let's say, you are interested only in letter keys and numbers, you could do it just like it is described in the msdn Keys example

if(e.KeyCode > Keys.NumPad0 && e.KeyCode < Keys.NumPad9 ||
   e.KeyCode > Keys.D0 && e.KeyCode < Keys.D9 ||
   e.KeyCode > Keys.A && e.KeyCode < Keys.Z) {

    //do useful stuff here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文