C# KeyPressEventArgs 多次按键
C# KeyPressEventArgs Multiple Keypress
使用 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;
}
}
谁能帮忙解决这个问题?
您应该处理您的情况下的按键按下和按下事件。这是基本想法的代码,你可以怎么做:
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;
}
}
使用 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;
}
}
谁能帮忙解决这个问题?
您应该处理您的情况下的按键按下和按下事件。这是基本想法的代码,你可以怎么做:
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;
}
}