Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard event propagation in winforms

I have a user control. Within the user control, there is a PictureBox that uses up the entire screen estate (Dock.Fill). I would like to catch keyboard events (e.g., Ctrl-V for implementing Paste functionality).

However, the PictureBox does not have any key events. Will the next layer under the PictureBox (i.e. the user control) get the KeyUp event? If I add my KeyUp event handler to the user control, will that work? I know that WPF has the solution of routed events. How does that work in winforms world?

like image 905
user1706184 Avatar asked Sep 28 '12 12:09

user1706184


People also ask

When a key from keyboard is released which event gets generated?

keyup – fires when you release a key on the keyboard. keypress – fires when you press a character keyboard like a , b , or c , not the left arrow key, home, or end keyboard, … The keypress also fires repeatedly while you hold down the key on the keyboard.

What are the events associated with keyboard?

There are three types of keyboard events: keydown , keypress , and keyup .

Will WinForms be deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.


1 Answers

You can receive the event in the form. See Form.KeyPreview.

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus

like image 50
Alex Avatar answered Nov 14 '22 20:11

Alex