简体   繁体   English

按键盘上的C#应用​​程序按钮

[英]Press a C# Application button from keyboard

I have a sample C# Application which have 4 buttons like this: 我有一个示例C#应用程序,它有4个按钮,如下所示:

在此输入图像描述

I want when I press a UP button respective the others to be pressed the correspondent button from C# Application (when window is focused) and keeps being pressed simultaneously with keyboard. 我想当我按下其他按钮时,按下C#应用程序中的对应按钮(当窗口被聚焦时),并与键盘同时按下。 How to do this? 这个怎么做? I've tried with KeyEventArgs and KeyPress but I think I'm a little bit confused. 我试过KeyEventArgsKeyPress,但我觉得我有点困惑。

LE: And to have somewhat possibility to press 2 buttons simultaneously: LE:并且有可能同时按下2个按钮:

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

Edit Later: I can't really understand. 稍后编辑:我真的不明白。 I've created a new project. 我创建了一个新项目。 The whole code is: 整个代码是:

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("|"); }
        }
    }
}

This is the whole code of new project, Why isn't showing te message box when press Up button from keybard? 这是新项目的全部代码,为什么在从keybard按下Up按钮时不显示te消息框? It is working for letters... 它正在为信件工作......

Use KeyUp and KeyDown. 使用KeyUp和KeyDown。 KeyPress events aren't triggered by the arrow keys. 键盘事件不是由箭头键触发的。

Why does my KeyPressEvent not work with Right/Left/Up/Down? 为什么我的KeyPressEvent无法使用Right / Left / Up / Down?

EDIT: This works for me. 编辑:这对我有用。

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("|"); }
    }
}

According to Up, Down, Left and Right arrow keys do not trigger KeyDown event : 根据向上,向下,向左和向右箭头键不会触发KeyDown事件

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM