[RESOLVED] [2005] Intercept PrintScreen
I am trying to make my app take a screenshot, and save it to a file! Ive read several places on how to do this in VB.Net, but for some reason, I havent been able to do what I want, which is the following.
You start the program, and hit "go".. the program then minimizes.. When the user hits the PrintScreen key, the program should intercept it, and save the screenshot to a file and preview it in an picturebox.
Maybe this is impossible and I have to do it another way, just thought I would check it!
Cheers!
Re: [2005] Intercept PrintScreen
Gig wrote a nice Class capable of this function, see http://www.vbforums.com/showthread.php?t=385497
Re: [2005] Intercept PrintScreen
Cool! Thanks..
I still wonder though.. How can I get my application to take the screenshot whenever the user hits the printscreen button?
Example: The program is minimized, and the user hits printScreen.. the capturescreenshot event is fired.. Not sure how to do this
Re: [2005] Intercept PrintScreen
You can detect external key presses with a keyboard hook. The topic has been discussed often so a search should turn up plenty of information. I'm not sure if you can then "cancel" the key press to prevent the OS acting on it further. That's what research is for though, eh? :)
Re: [2005] Intercept PrintScreen
If you register PrtSc as a hotkey (using the RegisterHotkey API) it won't be passed on to the operating system after you app has handled it.
For a WH_KEYBOARD_LL hook, return 1 to prevent the OS processing it (as per msdn)
Re: [2005] Intercept PrintScreen
Thanks for replies.. but where can I find the RegisterHotkey API? Tried searching abit, but obviously not good enough!
Thanks!
Re: [2005] Intercept PrintScreen
Resolved using the following Code:
Code:
Public Class Form1
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
' Pressed the hotkey!
Debug.WriteLine(m.Msg.ToString & ":" & m.WParam.ToString & ":" & m.LParam.ToString)
End If
MyBase.WndProc(m) '<-- Never Ever Forget This!
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Call UnregisterHotKey(Me.Handle, 9) '<-- Don't forget this
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call RegisterHotKey(Me.Handle, 9, 0, 44) '<-- Play with these settings
End Sub
End Class