Results 1 to 6 of 6

Thread: [RESOLVED] Detecting PrintScreen (PrtScn) Keypress?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Resolved [RESOLVED] Detecting PrintScreen (PrtScn) Keypress?

    I'm trying to detect the Printscreen keypress with the use of a timer. I can't use form1_keyup/down as the focus isn't always on the form.

    Is it possible to do this without importing GetKeyAsyncState?
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Detecting PrintScreen (PrtScn) Keypress?

    See source below code.

    Code:
    Module Module1
        <Runtime.InteropServices.DllImport("user32")> _
        Public Function SetWindowsHookEx( _
            ByVal idHook As Integer, _
            ByVal lpfn As Form1.LowLevelKeyboardProcDelegate, _
            ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
        End Function
        <Runtime.InteropServices.DllImport("user32.dll")> _
        Public Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As _
            <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.Bool)> Boolean
        End Function
        <Runtime.InteropServices.DllImport("user32.dll")> _
        Public Function CallNextHookEx( _
            ByVal hhk As IntPtr, _
            ByVal nCode As Integer, _
            ByVal wParam As IntPtr, _
            ByVal lParam As IntPtr) As IntPtr
        End Function
        <Runtime.InteropServices.DllImport("kernel32.dll", _
                                           CharSet:=Runtime.InteropServices.CharSet.Unicode)> _
        Public Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
        End Function
    End Module
    Code:
    Imports System.Runtime.InteropServices
    Public Class Form1
        Private Const WH_KEYBOARD_LL As Integer = 13
        Private Const WM_KEYUP As Integer = &H101
        Private Const WM_SYSKEYUP As Integer = &H105
        Public Delegate Function LowLevelKeyboardProcDelegate( _
            ByVal nCode As Integer, _
            ByVal wParam As IntPtr, _
            ByVal lParam As IntPtr) As IntPtr
    
        Private hookID As IntPtr
        Public proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
        Public Sub New()
            InitializeComponent()
            hookID = SetHook(proc)
        End Sub
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
            UnhookWindowsHookEx(hookID)
        End Sub
        Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr
            Using curProcess As Process = Process.GetCurrentProcess()
                Using curModule As ProcessModule = curProcess.MainModule
                    Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
                End Using
            End Using
        End Function
        Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
            If nCode >= 0 AndAlso (wParam.ToInt32 = WM_KEYUP OrElse wParam.ToInt32 = WM_SYSKEYUP) Then
                Dim vkCode As Integer = Marshal.ReadInt32(lParam)
                If vkCode = Keys.PrintScreen Then
                    ' Print screen was pressed
                End If
            End If
            Return CallNextHookEx(hookID, nCode, wParam, lParam)
        End Function
    End Class
    http://jo0ls-dotnet-stuff.blogspot.c...to-detect.html

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Detecting PrintScreen (PrtScn) Keypress?

    for a fully working keyboard hook component, see my global input hook link in my signature

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Detecting PrintScreen (PrtScn) Keypress?

    I think a better question is why don't you want to use GetAsyncKeyState()? It's a function designed to help you detect if a keyboard key has been pressed. Low-level keyboard hooks are advanced tools used for keyloggers, security applications, and a host of other things that want *every* keypress to go through them first. Screw up GetAsyncKeyState() and you miss some keys. Screw up a low-level keyboard hook and you can't use your keyboard until you reboot. I'm sure .paul.'s code is fine, but you don't have to use a chainsaw to cut a birthday cake.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Detecting PrintScreen (PrtScn) Keypress?

    Quote Originally Posted by Sitten Spynne View Post
    I think a better question is why don't you want to use GetAsyncKeyState()? It's a function designed to help you detect if a keyboard key has been pressed. Low-level keyboard hooks are advanced tools used for keyloggers, security applications, and a host of other things that want *every* keypress to go through them first. Screw up GetAsyncKeyState() and you miss some keys. Screw up a low-level keyboard hook and you can't use your keyboard until you reboot. I'm sure .paul.'s code is fine, but you don't have to use a chainsaw to cut a birthday cake.
    Good point, i was trying to not use any API's and just use the .net framework substitutes.
    But they haven't seemed to cover everything (obviously)
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    1

    Re: [RESOLVED] Detecting PrintScreen (PrtScn) Keypress?

    if i used
    UnhookWindowsHookEx(hookID)
    seperately how to re-hook it to the windows without re-opeining ? like an Enable-Disable button

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