读书人

不能直接定义键盘擒获事件

发布时间: 2012-12-16 12:02:32 作者: rapoo

不能直接定义键盘捕获事件

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 keyboardTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)//判断是否按下了F1功能键
{
MessageBox.Show("您按下了F1键!", "提示", MessageBoxButtons.OK);
}
}
}
}

在建立一个应用程序之后直接在Form1.cs添加了键盘事件。可是运行始终没有作用呢?
难道说还需要在界面里面添加什么东西才可以?
新手,希望指点。
[最优解释]
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

[其他解释]
e.KeyCode == Keys.F1

应该是
e.Key == Keys.F1
[其他解释]
不对,改了直接就报错了。
[其他解释]
this.KeyPreview = true;
[其他解释]
加了这句也不对,
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.BackColor = Color.Red;
}

直接把窗口属性的keyPreview改了也不行。
[其他解释]
http://pan.baidu.com/share/link?shareid=181041&uk=3457089026
这个是项目

读书人网 >C#

热点推荐