现有一窗体,在哪个事件中知道有按键,并得到按键,如按下F1键

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication3
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.label1.Location = new System.Drawing.Point(96, 40);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(66, 25);
    this.label1.TabIndex = 0;
    this.label1.Text = "请按键";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.label1);
    this.KeyPreview = true;
    this.Name = "Form1";
    this.Text = "Form1";
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    MessageBox.Show(e.KeyCode.ToString());
    }
    }
    }
      

  2.   

    if(e.KeyCode==Keys.B&&whetherSelected==false)  //当鼠标玩者在走旗时,键盘玩者不得悔旗操作
    label7_Click(sender,e);
    if(e.KeyCode==Keys.G&&whetherSelected==false)  //当鼠标玩者在走旗时,键盘玩者不得悔旗操作
    label8_Click(sender,e);
      

  3.   

    if((e.KeyCode==Keys.ControlKey||e.KeyCode==Keys.Space||e.KeyCode==Keys.Enter)
      

  4.   

    老兄按照你的写法,还是不行
    窗体中有很多控件,还包括面板
    根本没有触发KeyDown该事件