Results 1 to 7 of 7

Thread: need to capture all keystrokes... api perhaps?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    need to capture all keystrokes... api perhaps?

    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)
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you need to set KeyPreview to True , then use the override process keypreview, here's a quick example...
    VB Code:
    1. [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
    2.         KeyPreview = [Color=Blue]True
    3. [/color]    [Color=Blue]End[/color] [Color=Blue]Sub
    4.  
    5. [/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
    6. [/color]        [Color=Blue]If[/color] m.WParam.ToInt32 = 27 [Color=Blue]Then
    7. [/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]
    8. [/color]        [Color=Blue]End[/color] [Color=Blue]If
    9. [/color]    [Color=Blue]End[/color] [Color=Blue]Function[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    doesnt seem to work. I want the usercontrol to capture the keystrokes.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    sorry i thought with this...
    how I can capture the keypress events with my usercontrol (just they keystrokes that happen in my form)
    you wanted to catch the keystrokes in the Form that the usercontrol resided on.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by dynamic_sysop
    sorry i thought with this... you wanted to catch the keystrokes in the Form that the usercontrol resided on.
    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

    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.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Member
    Join Date
    May 2003
    Posts
    58
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width