❔ 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:
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 12 Replies
Have you tried setting KeyPreview to true on the form?
Form.KeyPreview = true
I have
noted that in op, it's set to true
oops sorry didn't notice 😅 , can you show the code where you set the property?
not in code tbf, but it's set in the editor
Weird, I think that should work, I guess at this point just do
do I set this as an event or something?
No in your Form.cs override ProcessCmdKey
yup, that works, tyty <3
no idea why it wouldn't otherwise though
No clue either should've worked with
KeyPreview = true
but for some reason it doesn'tyeah it always did, just not now
thank you for your help though ^-^
Np!
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.