我正在尝试使用 WinForms 和 C# 构建一个 Windows 应用程序,其中一种形式是我希望用户能够为每个动作分配键(即左、右、上、下运动等)。这类似于
在左侧列中将列出动作,用户应该能够为每个动作分配一个键。我对 Windows 窗体非常陌生,无法弄清楚要在左侧使用什么控件,我尝试使用带有 KeyDown 事件的按钮,但在此事件中不会触发输入/返回键,其余键它工作正常。那么应该使用什么控制以及什么事件,以便用户可以为任何运动/控制分配他选择的任何键。
编辑:这是最初的代码。
namespace ControllerWinServe
{
public partial class Form2 : Form
{
static string[] array = new string[6];
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button_d_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
private void button_u_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: '" + e.KeyCode.ToString() + "' pressed.");
}
private void button_d_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: b2 '" +e.KeyCode.ToString() + "'pressed.");
}
}
}
在尝试使用 user17753 的建议之后。
namespace ControllerWinServe
{
public class EnterTextBox : TextBox
{
protected override bool IsInputKey(Keys key)
{
if (key == Keys.Enter)
return true;
return base.IsInputKey(key);
}
}
public partial class Form2 : Form
{
static string[] array = new string[6];
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button_d_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
private void button_u_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: '" + e.KeyCode.ToString() + "' pressed.");
}
private void button_d_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: b2 '" +e.KeyCode.ToString() + "'pressed.");
}
}
}