C
C#16mo ago
Eris

❔ Form not detecting key presses

Heyo! I can't figure out why this form doesn't take key presses. I'm working in a .NET Windows Forms app, and I have a Form where I want to move a pictureBox using the arrow keys. I've set the form's KeyPreview = true property and set its KeyDown event to my method, tried both in designer and by writing this.Keydown += Form_KeyDown; in code, as well as both simultaneously. However no key press triggers the method. I have double-clicked the Event in the form editor many times to check again whether the Event is linked to the correct method and it definitely is. The method in question:
private void Form_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("here!");
if (e.KeyCode == Keys.Left)
{
zoomReticle.Location = new Point(zoomReticle.Location.X - 1, zoomReticle.Location.Y);
e.SuppressKeyPress = true;
updateReticle();
}
if (e.KeyCode == Keys.Up)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y - 1);
e.SuppressKeyPress = true;
updateReticle();
}
if (e.KeyCode == Keys.Right)
{
zoomReticle.Location = new Point(zoomReticle.Location.X + 1, zoomReticle.Location.Y);
e.SuppressKeyPress = true;
updateReticle();
}
if (e.KeyCode == Keys.Down)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y + 1);
e.SuppressKeyPress = true;
updateReticle();
}
}
private void Form_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("here!");
if (e.KeyCode == Keys.Left)
{
zoomReticle.Location = new Point(zoomReticle.Location.X - 1, zoomReticle.Location.Y);
e.SuppressKeyPress = true;
updateReticle();
}
if (e.KeyCode == Keys.Up)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y - 1);
e.SuppressKeyPress = true;
updateReticle();
}
if (e.KeyCode == Keys.Right)
{
zoomReticle.Location = new Point(zoomReticle.Location.X + 1, zoomReticle.Location.Y);
e.SuppressKeyPress = true;
updateReticle();
}
if (e.KeyCode == Keys.Down)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y + 1);
e.SuppressKeyPress = true;
updateReticle();
}
}
The MessageBox was there for debugging, it never shows up no matter what key is pressed, the pictureBox doesn't move either. I've used this method for detecting keys and it's always worked fine, I have no idea why it doesn't work now sadboihours
12 Replies
Kouhai
Kouhai16mo ago
Have you tried setting KeyPreview to true on the form? Form.KeyPreview = true
Eris
ErisOP16mo ago
I have noted that in op, it's set to true
Kouhai
Kouhai16mo ago
oops sorry didn't notice 😅 , can you show the code where you set the property?
Eris
ErisOP16mo ago
not in code tbf, but it's set in the editor
Kouhai
Kouhai16mo ago
Weird, I think that should work, I guess at this point just do
protected override bool ProcessCmdKey(ref Message msg, Keys key)
{
var handled = false;
MessageBox.Show("here!");
if (key == Keys.Left)
{
zoomReticle.Location = new Point(zoomReticle.Location.X - 1, zoomReticle.Location.Y);
handled = true;
updateReticle();
}
if (key == Keys.Up)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y - 1);
handled = true;
updateReticle();
}
if (key == Keys.Right)
{
zoomReticle.Location = new Point(zoomReticle.Location.X + 1, zoomReticle.Location.Y);
handled = true;
updateReticle();
}
if (key == Keys.Down)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y + 1);
handled = true;
updateReticle();
}
return handled || base.ProcessCmdKey(ref msg, key);
}
protected override bool ProcessCmdKey(ref Message msg, Keys key)
{
var handled = false;
MessageBox.Show("here!");
if (key == Keys.Left)
{
zoomReticle.Location = new Point(zoomReticle.Location.X - 1, zoomReticle.Location.Y);
handled = true;
updateReticle();
}
if (key == Keys.Up)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y - 1);
handled = true;
updateReticle();
}
if (key == Keys.Right)
{
zoomReticle.Location = new Point(zoomReticle.Location.X + 1, zoomReticle.Location.Y);
handled = true;
updateReticle();
}
if (key == Keys.Down)
{
zoomReticle.Location = new Point(zoomReticle.Location.X, zoomReticle.Location.Y + 1);
handled = true;
updateReticle();
}
return handled || base.ProcessCmdKey(ref msg, key);
}
Eris
ErisOP16mo ago
do I set this as an event or something?
Kouhai
Kouhai16mo ago
No in your Form.cs override ProcessCmdKey
Eris
ErisOP16mo ago
yup, that works, tyty <3 no idea why it wouldn't otherwise though
Kouhai
Kouhai16mo ago
No clue either XD should've worked with KeyPreview = true but for some reason it doesn't
Eris
ErisOP16mo ago
yeah it always did, just not now thank you for your help though ^-^
Kouhai
Kouhai16mo ago
Np!
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server