1.在form1中添加一键盘事件:private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode==Keys.Up)
    {……}
    if(e.KeyCode==Keys.Down)
    {……}
}
上述操作,只使用上下箭头不好用,必须是其它键(比如Control或Alt)+上下键这样组合使用!很奇怪!这是为什么呢?

解决方案 »

  1.   

    因为winform中的键盘事件大多数都是以组合键的形式出现的
    如:F1F12
        ShiftF1。shiftF12
        Ctrl0。Ctrl9
        CtrlA。CtrlZ
        CtrlF1。CtrlF12
        CtrlShift0。CtrlShift9
        CtrlShiftA。CtrlShiftZ
        Alt0。Alt9
        AltF1。Altf12
      

  2.   

    我刚才试了 没这个问题 你键盘是不是坏了 代码贴给你
    Form1.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace TestKeyDown
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode==Keys.Up)
                {
                    int i = 1;
                }
            }
        }
    }Form1.Designer.cs
    namespace TestKeyDown
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.KeyPreview = true;
                this.Name = "Form1";
                this.Text = "Form1";
                this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
                this.ResumeLayout(false);        }        #endregion
        }
    }
      

  3.   

    不应该啊!检查下键盘上的ctrl 是不是卡住了。 呵呵!
      

  4.   

    1.我在form中做的键盘事件,然后在Picturebox中使用,这样用应该也没问题的吧?
      

  5.   


    是有定义。但是我根据不同条件分开的,比如:bool b1=false,b2=false;
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    if(b1)
    {
        if(e.KeyCode==Keys.Up)
        {……}
        if(e.KeyCode==Keys.Down)
        {……}
    }
    if(b2)
    {
        if(e.KeyCode==Keys.Up)
        {……}
        if(e.KeyCode==Keys.Down)
        {……}
    }
        
    }这样有问题吗?