检测箭头键的WinForms [英] Detecting arrows keys in winforms
本文介绍了检测箭头键的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
的上,下,左,右箭头键不会触发KeyDown事件
首先看到代码
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;
using System.Threading;
namespace winform_project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("Hello");
}
}
}
我能够检测阿尔法-numeric键。但是我不能够检测到方向键。
I am able to detect alpha-numeric keys. However i am not able to detect arrow keys.
任何帮助,将在这方面的赞赏。
Any help would be appreciated in this regard.
推荐答案
确定所以经过一番研究,我发现处理箭头键事件的最简单的方法是重写ProcessCmdKey方法。
Ok so after some research i found out the simplest way to handle arrow key events is to override the ProcessCmdKey method.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData == Keys.Left)
{
MessageBox.Show("You pressed Left arrow key");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
希望这有助于。
Hope this helps.
这篇关于检测箭头键的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文