Skip to main content
 首页 » 编程设计

c#之我如何捕获多个 key

2024年02月13日16langtianya

我需要同时捕获多个键,例如“Shift+Insert”,我如何使用 System.Windows.Input.KeyEventArgs 使用它。我写了一些代码,但它不起作用:

 private void Grid1KeyUp(object sender, KeyEventArgs e) 
    { 
        if (e.Key == System.Windows.Input.Key.Shift && e.Key == System.Windows.Input.Key.Insert) 
        { 
            //do something 
        } 
    } 

谁能帮帮我?

注意:在 silverlight 项目中,您不能使用“System.Windows.Forms”。

请您参考如下方法:

宁愿尝试类似的东西

if (e.Shift && e.Key == System.Windows.Input.Key.Insert) 
{ 
    //do something 
} 

看看KeyEventArgs Class

A KeyEventArgs, which specifies the key the user pressed and whether any modifier keys (CTRL, ALT, and SHIFT) were pressed at the same time, is passed with each KeyDown or KeyUp event.

您还可以查看 KeyEventArgs.Modifiers Property

Gets the modifier flags for a KeyDown or KeyUp event. The flags indicate which combination of CTRL, SHIFT, and ALT keys was pressed.