jmasterx
jmasterx

Reputation: 54113

Keydown event for entire application VB .Net

I want to make it so that no matter which control has focus, it will do my event. So that I dont have to write a keydown event for all 137 of my objects. Is this possible?

Thanks

Upvotes: 2

Views: 2473

Answers (2)

Scuba Steve
Scuba Steve

Reputation: 1648

I'm no expert but I believe this is possible. I've done it before where I've had one event handler handle multiple button presses, and then do something different depending on which button has focus.

The code is for that is something like:

For Each ctl in Me.Controls
If ctl.Type is <whatever type of control you want to handle> And ctl.Isfocused Then
Do whatever
End If
Next

You don't need to worry about recursion for something like that.

It's easier if you want each control to do the same thing, no matter which one has focus.

Private Sub keypresshandle (System arguments ignore this) Handles key1.press, key2.press etc .... key10000.press
do some stuff here
End Sub

Upvotes: 1

Meta-Knight
Meta-Knight

Reputation: 17855

You have to set the KeyPreview property of your Form to True.

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events.

Upvotes: 4

Related Questions