【发布时间】:2012-10-20 13:45:08
【问题描述】:
我对 C# 和一般编程很陌生。基本上我的问题是我正在尝试制作一个使用键输入的简单代码,但是当我运行(调试)程序时,它根本无法识别任何键输入。 KeyPreview 设置为 true,但它似乎仍然没有做任何事情。你能告诉我我做错了什么吗?谢谢。
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 来捕获键盘输入。
-
感谢您的回答。您能否解释一下“正确链接到您的表单事件”是什么意思?是的,我刚刚将它写入表单代码。谢谢
-
@user1761590 你也可以使用
protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); }