gpt4 book ai didi

C#:从 KeyEventArgs 的 KeyData 获取按下的正确键

转载 作者:太空狗 更新时间:2023-10-30 00:17:38 25 4
gpt4 key购买 nike

我正在捕获一个 KeyDown 事件,我需要能够检查当前按下的键是否是:Ctrl + Shift + M ?


我知道我需要使用 KeyEventArgs 中的 e.KeyDataKeys 枚举以及带有枚举标志和位的东西,但我我不确定如何检查组合。

最佳答案

您需要使用 Modifiers KeyEventArgs 类的属性。

类似于:

//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");
}

关于C#:从 KeyEventArgs 的 KeyData 获取按下的正确键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/865774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com