1.在form1中添加一键盘事件:private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Up)
{……}
if(e.KeyCode==Keys.Down)
{……}
}
上述操作,只使用上下箭头不好用,必须是其它键(比如Control或Alt)+上下键这样组合使用!很奇怪!这是为什么呢?
{
if(e.KeyCode==Keys.Up)
{……}
if(e.KeyCode==Keys.Down)
{……}
}
上述操作,只使用上下箭头不好用,必须是其它键(比如Control或Alt)+上下键这样组合使用!很奇怪!这是为什么呢?
如:F1F12
ShiftF1。shiftF12
Ctrl0。Ctrl9
CtrlA。CtrlZ
CtrlF1。CtrlF12
CtrlShift0。CtrlShift9
CtrlShiftA。CtrlShiftZ
Alt0。Alt9
AltF1。Altf12
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
}
}
是有定义。但是我根据不同条件分开的,比如: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)
{……}
}
}这样有问题吗?