当两个按钮的同时按下 [英] When two buttons are pressed at same time

查看:199
本文介绍了当两个按钮的同时按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。我有一个表格,这是我的登录表单。然后我也有另一种形式,这是我的外接用户表单。当我点击登录表单登录按钮,它需要检查,如果移位和控制键都在相同的时间内保持下去。 FF他们两人都没有按下,则添加用户表单不应开启。 。但如果他们都按下并且登录按钮被点击时,它应该显示格式为:

I have a little problem. I have a form, which is my login form. Then I also have another form, which is my add user form. When I click on the login button for the login form, it needs to check if the shift and control keys are both held down on the same time. Ff both of them are not pressed, then the add user form should not open. but if they are both pressed down and the login button is clicked, it should show the form.

我有什么:

if (Control.ModifierKeys == (Keys.Control & Keys.Shift))
{
    //Show the form
}

但是,这是行不通的。

当我有:

if (Control.ModifierKeys == Keys.Shift)
{
    //Show the form
}

然后,它的工作原理。

我如何与压下来,控制和转移?

How can I achieve this with both buttons pressed down, control and shift?

推荐答案

尝试<$ C两个按钮实现这一目标$ C> Keys.Control | 。Keys.Shift

这是一个标志枚举;每个值由底层 INT

This is a flags enumeration; each value is represented by a separate bit in the underlying int.

Keys.Control单独位代表&安培; Keys.Shift 导致零值 - 例如如果控制 0001 0010 ,按位 - &安培; 0000

Keys.Control & Keys.Shift results in a zero value - e.g. if Control is 0001 and Shift is 0010, the bitwise-& is 0000.

Control.ModifierKeys 0000 当且仅当用户持有的没有的修改键,因此 == 只会造成真正如果用户未持有任何东西了。

Control.ModifierKeys will be 0000 if and only if the user is holding no modifier keys down, so the == will only result in true if the user isn't holding anything down.

Keys.Control | Keys.Shift ,在另一方面,结果,说:这两个标志的值 - 例如,如果控制 0001 0010 ,按位 - | 0011

Keys.Control | Keys.Shift, on the other hand, results in a value that says "both these flags" - e.g. if Control is 0001 and Shift is 0010, the bitwise-| is 0011.

Control.ModifierKeys 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.

另外,你可以打破这种下降为

Alternatively, you could break this down as

bool ctrlIsHeldDown  = (Control.ModifierKeys & Keys.Control) == Keys.Control;
bool shiftIsHeldDown = (Control.ModifierKeys & Keys.Shift)   == Keys.Shift;
if (CtrlIsHeldDown && shiftIsHeldDown)
{
    ...
}



(价值&安培; X)== X 结构检查无论是个人标志 X 设置,之后,它仅仅指刚标准布尔逻辑。

The (value & x) == x construct checks whether an individual flag x is set, and after that it's jsut standard boolean logic.

这篇关于当两个按钮的同时按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆