Reputation: 445
I have a sample C# Application which have 4 buttons like this:
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. How to do this? I've tried with KeyEventArgs and KeyPress but I think I'm a little bit confused.
LE: And to have somewhat possibility to press 2 buttons simultaneously:
======================================================
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? It is working for letters...
Upvotes: 0
Views: 1864
Reputation: 6481
According to 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);
}
}
Upvotes: 1
Reputation: 1825
Use KeyUp and KeyDown. KeyPress events aren't triggered by the arrow keys.
Why does my KeyPressEvent not work with 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("|"); }
}
}
Upvotes: 1