在 KeyDown 事件上筛选 KeyCode 的更有效方法
当用户尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将所有值放入
HashSet
< /a> 并检查 KeyCode 是否在集合中。或者,由于您正在检查是否相等,因此您可以将所有内容放入 switch 语句中。
You could throw all values into a
HashSet<T>
and check if the KeyCode is in the set.Or alternatively, since you're checking for equality, you could just throw all that into a switch statement.
例如,您可以(有很多好的尝试)检查此页面 有关
Char
类的帮助,您可以在其中使用 IsLetterOrDigit 或其他函数。现在我无法识别您是否使用 Windows 窗体?如果是这样,请使用像(char)e.KeyCode
这样的简单转换来获取字符。例子:
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:
您可以处理 KeyPress 事件形式。上述事件采用 KeyPressEventArgs 作为其参数范围。
使用 Char.IsLetterOrDigit 函数检查 KeyPressEventArgs.KeyChar 属性。
编辑:
您还可以尝试列出可接受的
Char
值,然后检查其中是否包含预置字符:您可能需要考虑结合使用这两种方式或甚至更多地满足您的要求。
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.
EDIT:
You can also try to make a list of your accepted
Char
values, then check if the preseed character is included in it:You may need to consider combining both ways or even more to fulfill your requirements.
如果您关心 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.
就像 @Daniel 在他的评论中指出的那样,也许将有效密钥列入白名单比将所有您不感兴趣的密钥列入黑名单更好。因此,如果您只对字母键和数字感兴趣,则可以按照 msdn 密钥示例
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