使用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;
}
}
任何人都可以帮忙解决这个问题吗?
答案 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;
}
}