如何确定在c#中是否单击了右键Ctrl/Shift/Alt或左键
本文关键字:Shift Ctrl Alt 右键 何确定 单击 是否 | 更新日期: 2023-09-27 18:11:45
我想在c#中使用右/左控制/Shift/Alt但是当我输入
private void Form1_KeyDown(or KeyPress)(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.RControlKey) //(or e.KeyCode)
{
//Code
}
}
但它不工作…Shift和Alt也一样
那么这里有什么问题呢?
在KeyEventArgs中有属性Ctrl, Alt和Shift显示这些按钮是否被按下。或者你可以执行checkif (keyboard。iskeydown (key。p) &&
不确定这是否解决了你的问题。
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.LMenu:
//Your code for Left Alt Key
break;
case Keys.LControlKey:
//Your code for Left Control Key
break;
case Keys.LShiftKey:
//Your code for Left Shift Key
break;
case Keys.RControlKey:
//Your code for Right Control Key
break;
case Keys.RMenu:
//Your code for Right Alt Key
break;
case Keys.RShiftKey:
//You code for Right Shift Key
break;
}
}
KeyUp事件:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup (v = vs.110) . aspx
键:
https://msdn.microsoft.com/en-US/library/system.windows.forms.keys (v = vs.110) . aspx
,如果你按下RControlKey+A,它会创建几个事件。
private void watcher_keyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("keyDown: " + e.KeyData.ToString());
}
private void watcher_KeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine("keyPress: " + e.KeyChar.ToString());
}
public void watcher_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine("keyUp: " + e.KeyData.ToString());
}
keyDown: RControlKey
keyDown: A, Control
keyPress:
keyUp: A, Control
keyUp: RControlKey, Control
从第一个或最后一个事件,您可以知道哪个(左或右)被点击。这意味着你可能需要缓存键代码,因为有2个keyUp/keyDown事件。