我可以点击按钮激活它,但是当我松开键盘按键时,我的按钮不会激活。代码是C#,我正在使用VS 2015
private void btnApiBetHigh_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F)
{
CurrentSite.amount = ((decimal)nudApiBet.Value);
CurrentSite.chance = (decimal)(nudApiChance.Value);
CurrentSite.PlaceBet(true, (decimal)nudApiBet.Value, (decimal)(nudApiChance.Value));
}
}
答案 0 :(得分:1)
将表单的属性KeyPreview
设置为true
。然后在表单的KeyUp
事件中使用您的代码,而不是单个按钮。
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.F)
{
e.Handled = true;
MessageBox.Show("F");
}
else if(e.KeyCode == Keys.G)
{
e.Handled = true;
MessageBox.Show("G");
}
}
不编写任何代码的另一种方法是使用mnemonics。如果您使用文字" Bet High(& G)"对于按钮,当您在表单上的任何位置按G键时,按钮会自动按下(除非聚焦控件时接受文本输入)。