And*_*ech 4 c# enums keyeventargs enum-flags
我正在捕捉一个KeyDown事件,我需要能够检查当前按下的键是否为:Ctrl+ Shift+ M ?
我知道我需要使用e.KeyData来自KeyEventArgs中,Keys枚举和一些与枚举标志和位,但我不知道如何检查相结合.
Mik*_*scu 12
您需要使用KeyEventArgs类的Modifiers属性.
就像是:
//asumming e is of type KeyEventArgs (such as it is
// on a KeyDown event handler
// ..
bool ctrlShiftM; //will be true if the combination Ctrl + Shift + M is pressed, false otherwise
ctrlShiftM = ((e.KeyCode == Keys.M) && // test for M pressed
((e.Modifiers & Keys.Shift) != 0) && // test for Shift modifier
((e.Modifiers & Keys.Control) != 0)); // test for Ctrl modifier
if (ctrlShiftM == true)
{
Console.WriteLine("[Ctrl] + [Shift] + M was pressed");
}
Run Code Online (Sandbox Code Playgroud)