C#KeyPressEventArgs多个Keypress

时间:2017-06-05 13:15:46

标签: c# gtk keypress gdk

使用KeyPressEventArgs我希望能够检测到多个按键。我没有运气就这样尝试过。

[GLib.ConnectBefore]
public override void OnKeyPress (object o, global::Gtk.KeyPressEventArgs args)
{ 
  if ((args.Event.Key == Gdk.Key.Up) && (args.Event.Key == Gdk.Key.Right)) 
  {
      playerPhysics.AddVector (_taxiGoUp);
      Player._state = Taxi.State.MoveUp;   
      playerPhysics.AddVector (_taxiGoRight);
      Player._state = Taxi.State.MoveRight;
  }
}

任何人都可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您应该在您的情况下处理按键和向上事件。以下是基本概念的代码如何执行此操作:

bool keyRight = false;
bool keyUp = false;

[GLib.ConnectBefore]
public override void OnKeyDown (object o, global::Gtk.KeyDownEventArgs args)
{ 
    switch (args.Event.Key) 
    {
       case Gdk.Key.Up:
         if(!keyUp)
         {
            keyUp = true;
            playerPhysics.AddVector (_taxiGoUp);
            Player._state = Taxi.State.MoveUp;            
         }
         break;
       case Gdk.Key.Right:
         if(!keyRight)
         {
            keyRight = true;
            playerPhysics.AddVector (_taxiGoRight);
            Player._state = Taxi.State.MoveRight;            
         }
         break;
    }
}

[GLib.ConnectBefore]
public override void OnKeyDown (object o, global::Gtk.KeyUpEventArgs args)
{ 
    switch (args.Event.Key) 
    {
       case Gdk.Key.Up:
         keyUp = false;
         break;
       case Gdk.Key.Right:
         keyRight = false;
         break;
    }
}