I want my usercontrol to close whenever the user presses the Escape key, whether the control has the focus or not. I'm just wondering how I can capture the keypress events with my usercontrol (just they keystrokes that happen in my form)
Printable View
I want my usercontrol to close whenever the user presses the Escape key, whether the control has the focus or not. I'm just wondering how I can capture the keypress events with my usercontrol (just they keystrokes that happen in my form)
you need to set KeyPreview to True , then use the override process keypreview, here's a quick example...
VB Code:
[Color=Blue]Private[/color] [Color=Blue]Sub[/color] Form1_Load([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] [Color=Blue]MyBase[/color].Load KeyPreview = [Color=Blue]True [/color] [Color=Blue]End[/color] [Color=Blue]Sub [/color] [Color=Blue]Protected[/color] [Color=Blue]Overrides[/color] [Color=Blue]Function[/color] ProcessKeyPreview([Color=Blue]ByRef[/color] m [Color=Blue]As[/color] System.Windows.Forms.Message) [Color=Blue]As[/color] [Color=Blue]Boolean [/color] [Color=Blue]If[/color] m.WParam.ToInt32 = 27 [Color=Blue]Then [/color] [Color=Green]'///[/color] [Color=Green]do[/color] [Color=Green]stuff[/color] [Color=Green]to[/color] [Color=Green]your[/color] [Color=Green]UserControl[/color] [Color=Green]because[/color] [Color=Green]the[/color] [Color=Green]Escape[/color] [Color=Green]Key[/color] [Color=Green]was[/color] [Color=Green]pressed[/color] [Color=Green].[/color] [Color=Green] [/color] [Color=Blue]End[/color] [Color=Blue]If [/color] [Color=Blue]End[/color] [Color=Blue]Function[/color]
hmm weird, I thought I cant use keypreview since the user control doesnt have a keypreview property.... umm so the parent form's keypreview is set to true and then I can override the processKeyPreview sub in teh usercontrol? hmm
doesnt seem to work. I want the usercontrol to capture the keystrokes. :confused:
sorry i thought with this...you wanted to catch the keystrokes in the Form that the usercontrol resided on.Quote:
how I can capture the keypress events with my usercontrol (just they keystrokes that happen in my form)
well yeah I explained it a bit bad.. what I meant was that I dont want to capture all the keys that are pressed while the user is running windows (basically, I dont want a keylogger :) ) I want the usercontrol to capture any keys that are pressed in my application :DQuote:
Originally posted by dynamic_sysop
sorry i thought with this... you wanted to catch the keystrokes in the Form that the usercontrol resided on.
I tried adding a KeyPress event for every control in my usercontrol. but for some reason the only event that works is the keyPress event for the usercontrol itself.
Use Application.AddMessageFilter function. You can have more than one MessageFilter to filter out different messages(mouse, keyboard, etc...). The following example is taken from MSDN.
Code:' Creates a message filter.
Public Class TestMessageFilter
Implements IMessageFilter
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) _
As Boolean Implements IMessageFilter.PreFilterMessage
' Blocks all the messages relating to the left mouse button.
If ((m.Msg >= 513) And (m.Msg <= 515)) Then
Console.WriteLine("Processing the messages : " & m.Msg)
Return True
End If
Return False
End Function
End Class