Results 1 to 2 of 2

Thread: Intercepting Print Screen

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    FL, USA
    Posts
    1

    Question Intercepting Print Screen

    How would I go about intercepting the print screen key while my application has focus? What I would like to do is have my program save the image on screen to a location on the hard drive. I already have code to take a screen shot and save it and it works just fine. The problem is I would like this to happen when the print screen key is pressed...

    I've already tried using the KeyDown or KeyUp events to intercept the key, but that doesn't seem to work... Seems like I could intercept any key BUT that one. :-\

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    Baltimore, MD
    Posts
    230
    Subclassing will catch the keystroke. Add this class to the project:
    VB Code:
    1. Imports System.Windows.Forms
    2.  
    3. Public Class Subclassing
    4.     Inherits System.Windows.Forms.NativeWindow
    5.  
    6.     'Event Declaration. This event will be raised when any Message will be posted to the Control
    7.     Public Event CallBackProc(ByRef m As Message)
    8.  
    9.     'Flag which indicates that either Event should be raised or not
    10.     Private m_Subclassed As Boolean = False
    11.  
    12.     'During Creation of Object of this class, Pass the Handle of Control which you want to SubClass
    13.     Public Sub New(ByVal handle As IntPtr)
    14.         MyBase.AssignHandle(handle)
    15.     End Sub
    16.  
    17.     'To Enable or Disable Receiving Messages
    18.     Public Property SubClass() As Boolean
    19.         Get
    20.             Return m_Subclassed
    21.         End Get
    22.         Set(ByVal Value As Boolean)
    23.             m_Subclassed = Value
    24.         End Set
    25.     End Property
    26.  
    27.     Protected Overrides Sub WndProc(ByRef m As Message)
    28.         If m_Subclassed Then 'If Subclassing Enabled then RaiseEvent
    29.             RaiseEvent CallBackProc(m)
    30.         End If
    31.         'Setting the Msg member to 0 in the CallBackProc event means the code is handling the message
    32.         'Used mainly for overriding textbox context menu
    33.         If m.Msg > 0 Then MyBase.WndProc(m)
    34.     End Sub
    35.  
    36.     Protected Overrides Sub Finalize()
    37.         MyBase.Finalize()
    38.     End Sub
    39. End Class

    Use it like this:
    VB Code:
    1. 'In the form
    2. Private WithEvents m_subclass As Subclassing
    3.  
    4. 'In form's Load event:
    5.  
    6. m_subclass= New Subclassing(txtLDesc.Handle)
    7. m_subclass.SubClass = True
    8.  
    9. 'add this procedure to the form
    10. Private Sub Callback(ByRef m As System.Windows.Forms.Message) Handles m_subclass.CallBackProc
    11.  
    12.     'handle PrntScr key
    13.     If m.WParam.ToInt32 = 44 Then
    14.  
    15.     End If
    16.  
    17. End Sub

    Hope I got it all.

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