如何在 gtk# c# 中手动检测箭头键
How to manually detect arrow keys in gtk# c#
我正在尝试使用 Gtk#-C# 在 Monodevelop 中制作游戏,玩家可以使用箭头键移动角色。但是,未注册箭头键。
是否有绕过默认处理程序手动检测按键的方法?
Google 和 Stack Overflow 的多次搜索都没有给出关于如何使用 Gtk-C# 检测方向键的答案。
这是我一直用来尝试检测方向键的代码:
protected void Key_Press (object obj, KeyPressEventArgs args)
{
//Let the rest of the program know what keys were pressed.
if (pressedKeys.Contains (args.Event.Key))
return;
pressedKeys.Add (args.Event.Key, args.Event.Key);
}
这是我编写的一个基本程序,用于尝试弄清楚如何检测箭头键:
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
this.KeyPressEvent += new KeyPressEventHandler (KeyPress);
}
protected void KeyPress (object sender, KeyPressEventArgs args)
{
if (args.Event.Key == Gdk.Key.Up)
return;
label1.Text = args.Event.Key.ToString ();
}
您需要做的就是像这样添加您的 KeyPress
处理程序:
KeyPressEvent += KeyPress;
并将 GLib.ConnectBefore
属性添加到您的事件,以便您在应用程序处理程序使用它之前收到它:
[GLib.ConnectBefore]
Cut/Paste 例子:
using System;
using Gtk;
public partial class MainWindow : Gtk.Window
{
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
KeyPressEvent += KeyPress;
}
[GLib.ConnectBefore]
protected void KeyPress(object sender, KeyPressEventArgs args)
{
Console.WriteLine(args.Event.Key);
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
KeyPressEvent -= KeyPress;
Application.Quit();
a.RetVal = true;
}
}
示例输出:
Left
Left
Right
Down
Up
Shift_R
Down
Left
Right
Up
Right
Up
Down
我正在尝试使用 Gtk#-C# 在 Monodevelop 中制作游戏,玩家可以使用箭头键移动角色。但是,未注册箭头键。
是否有绕过默认处理程序手动检测按键的方法?
Google 和 Stack Overflow 的多次搜索都没有给出关于如何使用 Gtk-C# 检测方向键的答案。
这是我一直用来尝试检测方向键的代码:
protected void Key_Press (object obj, KeyPressEventArgs args)
{
//Let the rest of the program know what keys were pressed.
if (pressedKeys.Contains (args.Event.Key))
return;
pressedKeys.Add (args.Event.Key, args.Event.Key);
}
这是我编写的一个基本程序,用于尝试弄清楚如何检测箭头键:
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
this.KeyPressEvent += new KeyPressEventHandler (KeyPress);
}
protected void KeyPress (object sender, KeyPressEventArgs args)
{
if (args.Event.Key == Gdk.Key.Up)
return;
label1.Text = args.Event.Key.ToString ();
}
您需要做的就是像这样添加您的 KeyPress
处理程序:
KeyPressEvent += KeyPress;
并将 GLib.ConnectBefore
属性添加到您的事件,以便您在应用程序处理程序使用它之前收到它:
[GLib.ConnectBefore]
Cut/Paste 例子:
using System;
using Gtk;
public partial class MainWindow : Gtk.Window
{
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
KeyPressEvent += KeyPress;
}
[GLib.ConnectBefore]
protected void KeyPress(object sender, KeyPressEventArgs args)
{
Console.WriteLine(args.Event.Key);
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
KeyPressEvent -= KeyPress;
Application.Quit();
a.RetVal = true;
}
}
示例输出:
Left
Left
Right
Down
Up
Shift_R
Down
Left
Right
Up
Right
Up
Down