Keyboard input handle problem
-
Hi all, I want to close the from with a keyboard input 'K' but I cannot handle the input. Before asking here, I've done lots of researches on web and especially on msdn. This is not the first time I've been using KeyDown event but now I'm on a new computer and I cannot figure out why this time it does not work. my code is simple:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.K)
{
this.Close();
}
}and on the designer side:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
All other events work just fine, like MouseOver, MouseDown or MouseMove but when it comes to KeyDown or KeyPress, it doesn't work any suggestions? :( -
Hi all, I want to close the from with a keyboard input 'K' but I cannot handle the input. Before asking here, I've done lots of researches on web and especially on msdn. This is not the first time I've been using KeyDown event but now I'm on a new computer and I cannot figure out why this time it does not work. my code is simple:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.K)
{
this.Close();
}
}and on the designer side:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
All other events work just fine, like MouseOver, MouseDown or MouseMove but when it comes to KeyDown or KeyPress, it doesn't work any suggestions? :(you need to enable KeyPreview of Form for which you want to write code as
this.KeyPreview = true;
and then write
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.K)
{
this.Close();
}
}or
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'k')
{
Close();
}
}Both Work fine
-
you need to enable KeyPreview of Form for which you want to write code as
this.KeyPreview = true;
and then write
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.K)
{
this.Close();
}
}or
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'k')
{
Close();
}
}Both Work fine
It worked Thank you! :D