Hi,
Recently, I was trying to capture the UP arrow key from my window forms application. I was able to capture the key in the parent form but once i open a child form, I would not be able to capture all the arrow keys but the rest of the keys like 'a', 'b','+' and etc are ok.
I have searched the web but did not find any solution. Does anyone know how to overcome this problem
Thank you.
Soh

Capturing the up/down arrow key in a child form
Alen32
Wicket
Hmmm, Strange...
I try to create 2 form, Form1 and Form2.
Form1:
IsMdiParent = true;
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
}
Form2:
KeyPreview = true;
private void Form2_KeyDown(object sender, KeyEventArgs e){
MessageBox.Show(Convert.ToString(e.KeyCode));}
When I run the program, pressing "Enter" show "Return" on the messagebox but pressing arrow key did not show anything. Oh by the way I am using a notebook to do the program do you think it's the note book that's giving the problem
Thank you.
Soh
Sumit Kumar
If you are trying to capture keystrokes from the child form, you have to handle the KeyDown event of the child form:
frmChild frm = new frmChild();
frm.MdiParent = this;
frm.KeyPress += new KeyPressEventHandler(Child_KeyPress);
frm.Show();
Then define the event handler:
private void Child_KeyPress(object sender, KeyPressEventArgs e)
{
switch(e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
// do stuff here;
break;
}
}
Neal Hudson
soh99,
What is on your form Most controlls capture the various keys as they are used for example moving the cursor through an input field, changing rows /cells, modifying values.
If you try your code with a blank form it should work, not much use though perhaps, you can set the keypreview property of the form to true but it still going to miss keys, so you need an alternate way of getting to the key strokes.
Microsoft recommned overriding the ProcessCmdKey method of each of your controls : http://support.microsoft.com/ id=320584
You could also use win32 aka :
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);
But you have to poll that at regular intervals to get your input, such as with a timer, so the process is becomming rather complicated, and also you are going to to get the input from all keyboard use, not just your form, so you would further have to test the foreground window etc.
What is you are trying to achieve by catching the keystrokes
Steve Thornton
Hi
What I was trying to do is to capture the keystroke : Arrow UP, Arrow DOWN, Arrow LEFT and Arrow RIGHT on my child form. When I detect Arrow UP maybe I will relocate one of my controls.
From the parent form, when I click on one of the button, I do this:
//pass my parent form over to the child
frmChild frm = new frmChild();
frm.getParent = this;
frm.MdiParent = this;
frm.show();
The parent form, IsMdiContainer = true. Child form KeyPreview = true.
Thank you.
Soh
Codeme
kexh
Hi
Here is the code in the KeyDown Event.
private void frmGraphical_KeyDown(object sender, KeyEventArgs e)
{
MessageBox
.Show(Convert.ToString(e.KeyCode));}
The Message box did not pop up when i press the arrow key but it did pop up showing "U" when i pressed the U Key.
Form frmGraphical is a child form of a parent form.
Thank you.
mRizvandi
Thanh Duong
sailajam
Hi Soh,
Are you sure you have nothing else on your form
The code above will work with a blank form and a form with certain controls, for example putting a single button on the form will prevent the event from firing if it has focus, there is very little you can do about this, other than customise the controls on the form or the form it seems the simplest way of achieving what you require would be by overiding the forms Command key handling :
protected override bool ProcessCmdKey(ref Message msg, Keys keyData){
MessageBox.Show(keyData.ToString());
return base.ProcessCmdKey(ref msg, keyData);
}
Regards
Frank2808
SAADEDIN
Hi
Already try that but when i press the arrow UP the part of the switch that suppose to run did not. Anyway, I have re-do the program without the child form meaning put all the codes and controls on the parent form and it solved my problem but is there really no way to move an controls using arrow keys in the Parent and Child environment
Thank you.
Soh
JennyMQuinn
davser
Hi
Er.... in
private void Child_KeyPress(object sender, KeyPressEventArgs e)
{
switch(e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
// do stuff here;
break;
}
}
the KeyPressEventArgs e does not has a method keycode, it only has keychar.
Thank you
Soh