簡體   English   中英

捕獲光標鍵的KeyDown和KeyUp事件

[英]Capturing KeyDown and KeyUp events for cursor keys

我想在winfor應用程序中移動一些圖形。 為此,我需要知道是否按下了任何光標鍵。 我試圖覆蓋ProcessCmdKey但沒有成功。

任何提示/想法如何實現這一目標?

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch (keyData)
    {
        case Keys.Left:
            // while Left is down
            // call this method repeadetdly
            buttonGoLeft();
            // when key is up stop calling this method
            // and check for other keys
            return true;

         //Other cases
     }
  }

這可行!

using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            KeyPreview = true;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left:
                    // while Left is down
                    // call this method repeadetdly
                    MessageBox.Show("its left", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    // when key is up stop calling this method
                    // and check for other keys
                    return true;

                default:
                    return false;
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM