accessing the key buffer
-
I'm trying to make an app that catches the clipboard data when somebody press print screen. The problem I'm facing is to determin if the user has pressed print screen, the event needs to be triggerd even if my app doesn't have focus. So what I'm wondering is if there's any reliable way to do this without hooks and that sort of stuff (which I know nothing about hehe). All I need is some keywords or perhaps a good article/tutorial :). just figured it out, no need to answer this one.
-
I'm trying to make an app that catches the clipboard data when somebody press print screen. The problem I'm facing is to determin if the user has pressed print screen, the event needs to be triggerd even if my app doesn't have focus. So what I'm wondering is if there's any reliable way to do this without hooks and that sort of stuff (which I know nothing about hehe). All I need is some keywords or perhaps a good article/tutorial :). just figured it out, no need to answer this one.
Since your application doesn't worn the desktop window, you'd have to set a Windows hook. They're not as hard as you think, and MSDN even has examples. Lookup the functions
SetWindowsHookEx
andUnhookWindowsHook
. The topic "Win32 Hooks" even has sample code. Another way might be to P/Invoke the functionsSetClipboardViewer
andChangeClipboardChain
. Then callSetClipboardViewer
with your window handle (Form.Handle.ToInt32()
). When the clipboard is changed, your application is sent theWM_DRAWCLIPBOARD
message (DWORD, 0x0308). You must also P/InvokeSendMessage
so that whenWM_CHANGECBCHAIN
is passed (when the clipboard chain changes, i.e. a viewer is added or removed) you can pass the HANDLEs that you received from callingSetClipboardViewer
. This may sound complicated, but it's not. Essentially, you're just inserting yourself in a chain and handling messages passed to that chain. When you get theWM_DRAWCLIPBOARD
message, you can use all the .NET class library's members for accessing the clipboard, checking forDataFormats.Bitmap
or some other string (even your own). This would tell you that an image was copied, though, not that the PrintScreen key was pressed. You could compare the size of the image with the size of the screen, but that only gives you a probable screenshot. Basically, a windows hook is the way to go if you want a true handle for PrintScreen. The other is just an alternative if you just want to know when an image was copied to the clipboard.Reminiscent of my younger years...
10 LOAD "SCISSORS" 20 RUN
-
Since your application doesn't worn the desktop window, you'd have to set a Windows hook. They're not as hard as you think, and MSDN even has examples. Lookup the functions
SetWindowsHookEx
andUnhookWindowsHook
. The topic "Win32 Hooks" even has sample code. Another way might be to P/Invoke the functionsSetClipboardViewer
andChangeClipboardChain
. Then callSetClipboardViewer
with your window handle (Form.Handle.ToInt32()
). When the clipboard is changed, your application is sent theWM_DRAWCLIPBOARD
message (DWORD, 0x0308). You must also P/InvokeSendMessage
so that whenWM_CHANGECBCHAIN
is passed (when the clipboard chain changes, i.e. a viewer is added or removed) you can pass the HANDLEs that you received from callingSetClipboardViewer
. This may sound complicated, but it's not. Essentially, you're just inserting yourself in a chain and handling messages passed to that chain. When you get theWM_DRAWCLIPBOARD
message, you can use all the .NET class library's members for accessing the clipboard, checking forDataFormats.Bitmap
or some other string (even your own). This would tell you that an image was copied, though, not that the PrintScreen key was pressed. You could compare the size of the image with the size of the screen, but that only gives you a probable screenshot. Basically, a windows hook is the way to go if you want a true handle for PrintScreen. The other is just an alternative if you just want to know when an image was copied to the clipboard.Reminiscent of my younger years...
10 LOAD "SCISSORS" 20 RUN