我对C#和编程一般都很陌生。基本上我的问题是我试图做一个简单的代码,它使用键输入,但是当我运行(调试)程序时,它根本不能识别任何键输入。 KeyPreview设置为true,但它似乎还没有做任何事情。你能告诉我我做错了什么吗?谢谢。如何让Visual C#工作室识别键输入
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public List<string> list = new List<string>();
public Form1()
{
InitializeComponent();
KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F3)
{
list.Add("OMG!");
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(list[0]);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
“Form1_KeyDown”是否正确链接到您的表单事件?你是用手还是从设计师那里创造它的?您甚至不需要KeyPreview来捕获键盘输入。 – LightStriker
谢谢你的回答。您能否解释一下“与您的表单事件正确关联”是什么意思?是的,我刚写入表单代码。谢谢 – user1761590
@ user1761590你也可以使用'protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); }' –