我有一个小问题。我有一个表单,这是我的登录表单。然后我还有另一个表单,这是我的添加用户表单。当我单击登录表单的登录按钮时,它需要检查是否同时按住了 Shift 和 Control 键。如果两者均未按下,则不应打开添加用户表单。但如果它们都被按下并单击登录按钮,它应该显示表单。
我拥有的:
if (Control.ModifierKeys == (Keys.Control & Keys.Shift))
{
//Show the form
}
但这是行不通的。
当我有:
if (Control.ModifierKeys == Keys.Shift)
{
//Show the form
}
然后就可以了。
我怎样才能同时按下控制和移动按钮来实现这一点?
Try Keys.Control | Keys.Shift
.
这是一个标志枚举;每个值都由底层中的单独位表示int
.
Keys.Control & Keys.Shift
结果为零 - 例如如果Control
is 0001
and Shift
is 0010
,按位&
is 0000
.
Control.ModifierKeys
将0000
当且仅当用户持有no按下修改键,所以==
只会导致true
如果用户没有按住任何东西。
Keys.Control | Keys.Shift
另一方面,会产生一个表示“这两个标志”的值 - 例如如果Control
is 0001
and Shift
is 0010
,按位|
is 0011
.
Control.ModifierKeys
will be 0011
if and only if the user is holding both Ctrl and Shift down, so the ==
will only result in true
in this case.
或者,您可以将其分解为
bool ctrlIsHeldDown = (Control.ModifierKeys & Keys.Control) == Keys.Control;
bool shiftIsHeldDown = (Control.ModifierKeys & Keys.Shift) == Keys.Shift;
if (CtrlIsHeldDown && shiftIsHeldDown)
{
...
}
The (value & x) == x
构造检查是否有单独的标志x
设置后,它只是标准的布尔逻辑。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)