http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx
It hooks into the keyboard and allows my application to catch keyboard input even if the application is not focued or is hidden. I'm having a problem though.
I've got a few different hotkeys set up in my application, which uses a child form as the bounds to take a screenshot of a portion of the screen. You can hit F9 to take a screenshot and the program will ask you where to save it. F2 takes a screenshot and automtically saves it to the last save location. Finally, F3 copies the screenshot to your clipboard, but doesn't save it to a file. These all work as expected when the program is focused, and F9 and F2 work as expected when not focused, but F3 for some reason won't work unless the form has focus. I don't understand why F2 would work and not F3, they're essentially the same.
I tried using the hook class' KeyUp event handler instead of the KeyDown one and the same thing happened. I did put a breakpoint in at the event handler for the hook. It does exactly what it should, catches the F3, runs the copyToClipboard method, which gets the screenshot from the graphics object using CopyFromScreen(). Here is my method:
Expand|Select|Wrap|Line Numbers
- private void copyShotToClipboard()
- {
- using (Bitmap wnd = takeScreenshot())
- {
- Clipboard.SetImage(wnd);
- lblNotify.Text = "Copied to Clipboard";
- SilentNotify();
- }
- }
How can I solve this? I have to dispose the bitmap to keep memory usage down, but when I dispose of it, even code that comes before it won't work anymore...which makes absolutely no sense. Take this slightly different example method:
Expand|Select|Wrap|Line Numbers
- Bitmap wnd = takeScreenshot();
- Clipboard.SetImage(wnd);
- lblNotify.Text = "Copied to Clipboard";
- SilentNotify();
- wnd.Dispose();
I'm sorry this is so long, I just wanted to completely explain what was happening. Please help me out with this, I'm stuck on this project unless I can get this fixed.