This question already has an answer here:
这个问题已经有了答案:
- Difference between Keys.Shift and Keys.ShiftKey 2 answers
- 键之间的区别。转变和钥匙。ShiftKey 2答案
What's the exact difference between Keys.Control and Keys.ControlKey/Keys.Shift and Keys.ShiftKey in System.Windows.Forms?
键之间的确切区别是什么?控制和Keys.ControlKey /钥匙。转变和钥匙。在System.Windows.Forms ShiftKey吗?
I've googled it but nothing specific came up, and it doesn't seem to be documented on MSDN either.
我曾在谷歌上搜索过,但没有发现任何具体的信息,而且似乎也没有在MSDN上记录下来。
Personally, I always use Keys.Control/Keys.Shift, because it works fine.
就我个人而言,我总是使用key . control / key。换班,因为它工作得很好。
Edit: After reading KreepN's answer, I thought if you never have to use Control/Shift, why are they in the framework?
编辑:在阅读了KreepN的答案后,我想如果你不需要使用Control/Shift,为什么它们会在框架中?
I mean, they aren't even the physical keys, so I can't see the reason why Microsoft decided to create them.
我的意思是,它们甚至都不是物理键,所以我不明白微软为什么要创建它们。
Are there circumstances where it's better to use Control/Shift?
是否有更好的使用控制/转移的情况?
1 个解决方案
#1
6
ControlKey and ShiftKey (and Menu--which you would assume would have been named AltKey) represent the physical keys themselves. In other words, they are "actual" keys and can be found in the KeyCode property of a KeyEventArgs object.
ControlKey和ShiftKey(以及Menu,您可能会认为它们被命名为AltKey)表示物理键本身。换句话说,它们是“实际的”键,可以在KeyEventArgs对象的KeyCode属性中找到。
Control, Shift, and Alt, on the other hand, will never appear in the KeyCode property, but their values can be found in the KeyData property. It seems you never actually NEED to use these constants, because the framework already pulls them out for you via the Alt, Control, and Shift properties of the KeyEventArgs object, but you CAN use them to test against the KeyData property if you really want to.
另一方面,控件、Shift和Alt不会出现在KeyCode属性中,但是它们的值可以在KeyData属性中找到。看起来您从来都不需要使用这些常量,因为框架已经通过KeyEventArgs对象的Alt、Control和Shift属性为您提取了这些常量,但是如果您确实需要,您可以使用它们对KeyData属性进行测试。
通过示例源代码。
Edit for your edit:
编辑为你编辑:
Look at the values that are returned when the "a" key is pressed:
查看按下“a”键时返回的值:
a (unshifted) / 41 / 41
a(未移位)/ 41 / 41。
A (Shift+a) / 41 / 10041
A(移位+ A) / 41 / 10041
Ctrl+a / 41 / 20041
Ctrl+a / 41 / 20041
The "KeyCode" in this case is = 41 for all modifiers. You could use this in code if all you cared about was the primary button pressed, in this case "a".
在这种情况下,所有修饰符的“KeyCode”都是= 41。如果您只关心按下主按钮(在本例中为“a”),那么您可以在代码中使用它。
If you wanted to have different functionality based on if a modifier was pressed you would need to get more specific and reference the "KeyData" field and look for the # that denoted a certain modifier. In this case "100" for shift and "200" for control at the beginning of the field.
如果您想要拥有不同的功能,根据是否按了一个修饰符,您需要获得更具体的信息并引用“KeyData”字段,并查找表示某个修饰符的#。在这种情况下,“100”表示移位,“200”表示字段开始时的控制。
That's not to say you couldn't just check for the "41" at the end of the KeyData field, but I've never been one to complain about convenience.
这并不是说你不能在KeyData字段的末尾检查“41”,但我从来没有抱怨过它的方便性。
It would be safe to say that the "difference" you are looking for between them in your first question is that they reference different property fields.
可以肯定地说,你在第一个问题中寻找的“差异”是它们引用了不同的属性字段。
Edit for additional relevance: The key modifier values combined with the key value directly correlate to the Shortcut enumeration members. For example: Shortcut.CtrlF8 ( 0x20077 ) is the same as Keys.Control | Keys.F8 ( 0x20000 | 0x77 )
编辑附加相关性:键修饰符值与键值直接关联到快捷枚举成员。例如:快捷方式。ctrl - f8 (0x20077)与键相同。控制|钥匙。F8 (0x20000 | 0x77)
This can be useful when dealing with the defined Shortcut properties of menu items.
这在处理菜单项定义的快捷方式属性时非常有用。
#1
6
ControlKey and ShiftKey (and Menu--which you would assume would have been named AltKey) represent the physical keys themselves. In other words, they are "actual" keys and can be found in the KeyCode property of a KeyEventArgs object.
ControlKey和ShiftKey(以及Menu,您可能会认为它们被命名为AltKey)表示物理键本身。换句话说,它们是“实际的”键,可以在KeyEventArgs对象的KeyCode属性中找到。
Control, Shift, and Alt, on the other hand, will never appear in the KeyCode property, but their values can be found in the KeyData property. It seems you never actually NEED to use these constants, because the framework already pulls them out for you via the Alt, Control, and Shift properties of the KeyEventArgs object, but you CAN use them to test against the KeyData property if you really want to.
另一方面,控件、Shift和Alt不会出现在KeyCode属性中,但是它们的值可以在KeyData属性中找到。看起来您从来都不需要使用这些常量,因为框架已经通过KeyEventArgs对象的Alt、Control和Shift属性为您提取了这些常量,但是如果您确实需要,您可以使用它们对KeyData属性进行测试。
通过示例源代码。
Edit for your edit:
编辑为你编辑:
Look at the values that are returned when the "a" key is pressed:
查看按下“a”键时返回的值:
a (unshifted) / 41 / 41
a(未移位)/ 41 / 41。
A (Shift+a) / 41 / 10041
A(移位+ A) / 41 / 10041
Ctrl+a / 41 / 20041
Ctrl+a / 41 / 20041
The "KeyCode" in this case is = 41 for all modifiers. You could use this in code if all you cared about was the primary button pressed, in this case "a".
在这种情况下,所有修饰符的“KeyCode”都是= 41。如果您只关心按下主按钮(在本例中为“a”),那么您可以在代码中使用它。
If you wanted to have different functionality based on if a modifier was pressed you would need to get more specific and reference the "KeyData" field and look for the # that denoted a certain modifier. In this case "100" for shift and "200" for control at the beginning of the field.
如果您想要拥有不同的功能,根据是否按了一个修饰符,您需要获得更具体的信息并引用“KeyData”字段,并查找表示某个修饰符的#。在这种情况下,“100”表示移位,“200”表示字段开始时的控制。
That's not to say you couldn't just check for the "41" at the end of the KeyData field, but I've never been one to complain about convenience.
这并不是说你不能在KeyData字段的末尾检查“41”,但我从来没有抱怨过它的方便性。
It would be safe to say that the "difference" you are looking for between them in your first question is that they reference different property fields.
可以肯定地说,你在第一个问题中寻找的“差异”是它们引用了不同的属性字段。
Edit for additional relevance: The key modifier values combined with the key value directly correlate to the Shortcut enumeration members. For example: Shortcut.CtrlF8 ( 0x20077 ) is the same as Keys.Control | Keys.F8 ( 0x20000 | 0x77 )
编辑附加相关性:键修饰符值与键值直接关联到快捷枚举成员。例如:快捷方式。ctrl - f8 (0x20077)与键相同。控制|钥匙。F8 (0x20000 | 0x77)
This can be useful when dealing with the defined Shortcut properties of menu items.
这在处理菜单项定义的快捷方式属性时非常有用。