方向键的响应与窗体上按钮响应冲突,急

hujiiori 2004-09-18 10:21:12
我想让我的窗体响应keydown事件,用到方向键,但发现方向键的响应与窗体上按钮响应冲突,变成按钮的焦点切换了,该怎么办,急
...全文
178 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nchen123 2004-09-22
  • 打赏
  • 举报
回复
那个函数是捕获 Tab 按键用的, 否则你会发现如果按 tab 会跳到按钮上
hujiiori 2004-09-21
  • 打赏
  • 举报
回复
首先感谢木野狐,不过我对你的例子还是不大理解,那个WindowsApplication4.KeyTrapTextBox textBox1 是干什么用的,我发现将IsInputKey那个函数去掉也可以完成那个功能的,好像textBox1一旦接受焦点(不让button接受焦点)就可以实现要求,不需要isinputKey,怎么回事
jamzh 2004-09-19
  • 打赏
  • 举报
回复
学习!~
nchen123 2004-09-19
  • 打赏
  • 举报
回复
给你做了一个例子,试试便知:

/*******************************
* 木野狐, 2004-9-19
*******************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication4
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label1;
private WindowsApplication4.KeyTrapTextBox textBox1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.textBox1.Focus();

}

/// <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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new WindowsApplication4.KeyTrapTextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(56, 88);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(56, 128);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "button3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(56, 160);
this.button4.Name = "button4";
this.button4.TabIndex = 4;
this.button4.Text = "button4";
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Desktop;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Location = new System.Drawing.Point(200, 48);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "Test";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(0, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(216, 240);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(216, 48);
this.label2.TabIndex = 6;
this.label2.Text = "按上下左右,上面那个 Label 会移动,不会变成控件焦点的切换。 (木野狐)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 309);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


protected override bool IsInputKey(Keys keyData)
{
switch (keyData)
{
case Keys.Tab:
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
return true;
}
return base.IsInputKey (keyData);
}


protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
this.label1.Top -= 10;
break;
case Keys.Down:
this.label1.Top += 10;
break;
case Keys.Left:
this.label1.Left -= 10;
break;
case Keys.Right:
this.label1.Left += 10;
break;
}

base.OnKeyDown (e);
}



}

public class KeyTrapTextBox : TextBox
{
public KeyTrapTextBox()
{

}

const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_CHAR = 0x0102;

public override bool PreProcessMessage( ref Message msg )
{
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
if(msg.Msg == WM_KEYDOWN)
{
Console.WriteLine("TextBox.WM_KEYDOWN key: "+keyCode.ToString());
}

if((msg.Msg == WM_KEYDOWN)
&& keyCode == Keys.Tab)
{
return true;
}
return base.PreProcessMessage(ref msg);
}
}
}
Firestone2003 2004-09-19
  • 打赏
  • 举报
回复
学习
nchen123 2004-09-19
  • 打赏
  • 举报
回复
By default, the arrow keys are not handled by a control's key processing code, but instead are filtered out for focus management. Hence, the control's KeyDown, KeyUp and KeyPressed events are not hit when you press an arrow. If you want your control to handle these keyboard events, you tell the framework by overriding your control's IsInputKey method.

protected override bool IsInputKey(Keys key)

{

switch(key)

{

case Keys.Up:

case Keys.Down:

case Keys.Right:

case Keys.Left:

return true;

}

return base.IsInputKey(key);

}
hujiiori 2004-09-19
  • 打赏
  • 举报
回复
我想当键按着的时候连续响应,keyup不合需要吧,还有更好的办法吗
BearRui 2004-09-18
  • 打赏
  • 举报
回复
改为在KeyUp事件下响应!!!
内容概要:本文系统阐述了基于线性与非线性状态空间模型预测控制(MPC)的四旋翼无人机轨迹跟踪对比仿真研究,包含完整的Simulink仿真模型、详细的技术讲解与说明文档,属于硕士论文级别的复现阶段。研究围绕四旋翼飞行器的动力学建模展开,分别构建线性MPC与非线性MPC控制器,深入比较两者在复杂轨迹跟踪任务中的控制性能差异,重点评估其在轨迹精度、动态响应速度、系统稳定性及抗干扰能力等方面的表现。文中提供了从状态方程推导、约束条件设定、代价函数设计到仿真结果分析的全流程实现细节,有助于读者全面掌握MPC在高阶非线性系统中的应用机制与工程实现方法。; 适合人群:具备自动控制原理、现代控制理论(特别是状态空间方法)、非线性系统建模及MATLAB/Simulink仿真能力的研究生、科研人员,以及从事无人机飞控系统开发、先进控制算法研究的工程技术人员。; 使用场景及目标:① 学习并掌握线性与非线性MPC在四旋翼系统中的建模与控制器设计方法;② 对比分析两种MPC策略在实际轨迹跟踪中的性能优劣,理解其适用边界与局限性;③ 支持硕士论文复现、科研项目验证、控制算法优化与教学案例开发。; 阅读建议:建议结合所提供的完整仿真模型逐步操作,重点理解系统线性化处理方法、预测时域与控制时域的设置、状态与输入约束的处理机制,以及非线性MPC的实时优化求解过程。同时推荐配合经典控制理论教材与MPC专著进行延伸学习,以实现从理论推导到仿真验证的闭环掌握。

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧