简体   繁体   English

C#-如何检测+和=按下的字符

[英]C# - How to detect + and = pressed characters

I was testing keys and I got that = and Shift+= (+) have the same code: 187 我正在测试键,并且得到=和Shift + =(+)具有相同的代码:187

And the expression e.KeyValue == 187 works in the both cases. 在两种情况下,表达式e.KeyValue == 187都适用。 How to verify the real key? 如何验证真实密钥? Thanks 谢谢

Examining the documentation for System.Windows.Forms.KeyEventArgs, which it seems you're using here, you'll discover several documented properties which give you "the rest of the story". 检查似乎在这里使用的System.Windows.Forms.KeyEventArgs文档,您会发现几个文档化的属性,这些属性为您提供了“故事的其余部分”。

What you're interested in is the Shift property. 您感兴趣的是Shift属性。 For example: 例如:

if ( e.Shift )  { /* the key is + */ }
           else { /* the key is = */ }

Note, too, that just examining the Shift property doesn't account for the possibility that the user has also held down the Alt key, or the Control key, which are reflected similarly in the properties Alt and Control 还要注意,仅检查Shift属性并不能说明用户还按住Alt键或Control键的可能性,这在属性Alt和Control中也有类似反映

Also, you may need to understand the Modifiers property, which is a bit field indicating all of the modifier key states (Atl, Control and Shift) at once. 另外,您可能需要了解Modifiers属性,该属性是一个位字段,用于一次指示所有修饰键状态(Atl,Control和Shift)。

Further, you will want to investigate the difference between KeyValue, KeyCode and KeyData. 此外,您将要研究KeyValue,KeyCode和KeyData之间的区别。

KeyValue is defined as a raw numeric value, which as you discovered ignores shift states, so it can't distinquish between lower and upper case, or between + and =. KeyValue定义为原始数值,您发现它忽略了移位状态,因此无法区分大小写或+和=。

You can read here that via the event args you can verify if shift is pressed or not, for intense: 您可以在此处阅读 ,通过事件args可以验证是否按下了shift键,例如:

if ( e.Shift )  
{ 
   // the key is +  
}
else 
{ 
  //  the key is = 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM