Forms Not Responding to Keydown Events

Forms not responding to KeyDown events

Does your form have KeyPreview property set to true?

Form.KeyPreview Property

Gets or sets a value indicating whether the form will receive key
events before the event is passed to the control that has focus.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Keydown Event not firing

setting the event

this.KeyDown += Form5_KeyDown;
this.KeyPreview = true;

the event

void Form5_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
MessageBox.Show("A pressed");
else if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.F1)
MessageBox.Show("Combination of ALt and F1 pressed");
}

dont forget the KeyPreview = true, if you want to handle all keydown

Windows.Form not fire keyDown event

Seems to work for me:

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.BringToFront();
this.Focus();
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}

void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("test");
}

Are there any child controls on your form ?

KeyDown event doesnt work

You need to set Form.KeyPreview property to True. This property gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

Also note that there is a mistake in your code; according to your message you need to verify if e.KeyCode == Keys.A.

C# Windows Forms Applications Hotkey - KeyDown event not working

If you want to create global hotkeys manager for your form to be available for all controls in that form, you need to override the Form.ProcessCmdKey() method that catch all keys for all controls, instead of using the form key down event that works only when the background is focused and which can only happens when ActiveControl is null:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch ( keyData )
{
case Keys.Control | Keys.S:
timer1.Stop();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

Thus you can catch any key combination you need and return true if processed.

KeyDown event not firing?

I was able to reproduce a similar issue (which is hopefully related..)

Explanation:

  • Controls which return CanSelect==true are selectable for keyboard input
  • A blank descendent of Control() is selectable, one of Panel() is not
  • The first selectable control added to a form will get selected
  • A selected control will steal keyboard events from its parents by default
  • Certain keys used for navigation within a window require extra steps to be handleable

Check here for a good overview of how windows keyboard input works.

Code to reproduce it:

public class GameForm : Form
{
public GameForm()
{
this.KeyDown += Game_KeyDown;
var tests = new List<Control[]>() {
new[] { new Panel() },
new[] { new Panel(), new Panel() },
new[] { new Control() },
new[] { new Control(), new Panel() },
new[] { new Panel(), new Control() }
};
// When test index 2 to 4 used, keyboard input does not reach form level
Controls.AddRange(tests[0]);
// When uncommented, ensures all keyboard input reaches form level
/*this.KeyPreview = true;
// Additional plumbing required along with KeyPreview to allow arrow and other reserved keys
foreach (Control control in this.Controls)
{
control.PreviewKeyDown += control_PreviewKeyDown;
}*/
}
void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
e.IsInputKey = true;
}
private void Game_KeyDown(object sender, KeyEventArgs e)
{
// breakpoint here
Debug.WriteLine(e.KeyCode);
}
}

C#: KeyDown Event does not work in controls

On the keypress event make sure it is for the selected control an not the main form. If you are capturing for the form to determine which key was pressed then use the keypress event for that. You can use a messagebox to verify that you have the right control. Every key has an integer value and you can access and use those by using the properties of e.

Bonus. Depending on how you implemented your code you will have to use either global varibles to pass the data across the forms or use delegates to actively access and set controls on another form

In winforms, on keydown event handler textbox event keypress is not working

Every KeyDown event receive a KeyEventArgs parameter.

Inside the KeyEventArgs parameter there is a property named SuppressKeyPress.

According to MSDN setting this property to true avoid the KeyPress event

If you set this property to false, the current control with focus will receive the keypress.

private void formMain_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Do your processing
....
e.Handled = true;
e.SuppressKeyPress = false;
}

How to have interior form respond to key down events?

Since your Presenter class is aware of the form it is putting into the panel, it will also need to get from that form or from associated data what control in the form is meant to have default focus. Presumably, your Presenter class has a method that shows a given form on that panel, so you do something like:

Presenter.ShowForm(MainViewPanel, Form1);

You would then need to do something like:

Form1.Controls.OrderBy(x => x.TabIndex).First().Focus();


Related Topics



Leave a reply



Submit