|
-
May 13th, 2011, 09:18 AM
#1
Thread Starter
Lively Member
[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! 
-
May 14th, 2011, 09:48 AM
#2
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
-
May 14th, 2011, 12:41 PM
#3
Re: Detecting PrintScreen (PrtScn) Keypress?
for a fully working keyboard hook component, see my global input hook link in my signature
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 14th, 2011, 08:43 PM
#4
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.
-
May 15th, 2011, 12:32 AM
#5
Thread Starter
Lively Member
Re: Detecting PrintScreen (PrtScn) Keypress?
 Originally Posted by Sitten Spynne
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! 
-
Sep 3rd, 2014, 05:29 AM
#6
Registered User
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|