MrV*_*mes 3 c# events visual-studio
我希望能够使用空格键来修改按住鼠标时的行为。不知道更好,我想象它涉及到两个(或三个)事件处理程序之间的某种协调- mousemove,keydown和keyup。但是我想知道是否有某种方法可以完全在一个事件处理程序-中处理它mousemove。
示例代码给出了我希望能够做什么的想法...
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Keyboard.KeyDown == Keys.Space)
{
/* Do modified behavour for left mouse being held down while
space is also held down */
}
else
{
// Do normal behavour for left mouse being held down
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否可能发生这种情况,还是我必须使用keydown事件处理程序将空格键的状态保存到类变量中,并使用鼠标移动处理程序进行检查?
可以使用Control.ModifierKeys和Control.MouseButtons完成IT工作。但是仅适用于kay,例如shift,ctrl和alt。
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if ((Control.ModifierKeys & Keys.Shift) != 0)
{
if ((Control.MouseButtons & MouseButtons.Left) != 0)
{ // here you go
}
}
}
Run Code Online (Sandbox Code Playgroud)