【问题标题】:Press a C# Application button from keyboard从键盘按下 C# 应用程序按钮
【发布时间】:2014-05-01 23:13:46
【问题描述】:

我有一个示例 C# 应用程序,它有 4 个这样的按钮:

我希望当我按下 UP 按钮时,其他人按下 C# 应用程序中的相应按钮(当窗口聚焦时)并保持与键盘同时按下。这个怎么做?我试过 KeyEventArgsKeyPress 但我觉得我有点困惑。

LE:并且有可能同时按下 2 个按钮:

================================================ =======

稍后编辑:我真的无法理解。我创建了一个新项目。整个代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.KeyDown += Form1_KeyDown;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up) { MessageBox.Show("|"); }
        }
    }
}

这是新项目的全部代码,为什么从键盘上按向上按钮时不显示消息框?它适用于字母...

【问题讨论】:

标签: c# keypress keyboard-events


【解决方案1】:

根据Up, Down, Left and Right arrow keys do not trigger KeyDown event

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch (keyData)
    {
        case Keys.Right:
            button1.PerformClick();
            return true;
        case Keys.Left:
             //...
        default:
            return base.ProcessCmdKey(ref msg, keyData);
    }
}

【讨论】:

  • 我试过这个。当按键 (^v) 只是将焦点移动到不同的按钮但它不想执行点击...它仅适用于字母而不适用于 ^,但执行的点击不可见。有没有什么方法可以像点击一样完全可见?
【解决方案2】:

使用 KeyUp 和 KeyDown。 KeyPress 事件不是由箭头键触发的。

Why does my KeyPressEvent not work with Right/Left/Up/Down?

编辑:这对我有用。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.KeyDown += Form1_KeyDown;
    }

    void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up) { MessageBox.Show("|"); }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 2013-03-14
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2020-10-12
    相关资源
    最近更新 更多