C#: Windows Forms: Getting keystrokes in a panel/picturebox?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-04-24T01:33:41Z Indexed on 2010/04/24 1:43 UTC
Read the original article Hit count: 392

Filed under:
|
|
|

I'm making a level editor for a game using windows forms. The form has several drop down menus, text boxes, etc, where the user can type information.

I want to make commands like CTRL + V or CTRL + A available for working within the game world itself, not text manipulation. The game world is represented by a PictureBox contained in a Panel.

This event handler isn't ever firing:

private System.Windows.Forms.Panel canvas;
// ...
this.canvas = new System.Windows.Forms.Panel();
// ...
this.canvas.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.canvas_PreviewKeyDown);

What is the preferred way of doing this? Can a panel even receive keyboard input? I would like to allow the user to use copy/paste/select-all commands when working with the text input, but not when placing objects in the game world.

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-forms