|
|
Subject:
Override behavior of Print Screen key in Windows
Category: Computers > Programming Asked by: bryand_nyc-ga List Price: $10.00 |
Posted:
24 Dec 2002 06:38 PST
Expires: 23 Jan 2003 06:38 PST Question ID: 133059 |
I have an application that needs to override the behavior of the print screen key in Windows (which will normally copy the contents of the screen or active window to the copy buffer in combination with alt or ctrl). There is a product (http://www.visualautomation.com/coldkey) that seems to offer this functionality, but I'm looking for some C++ (or Java though I think that is unlikely) code that disables the default behavior and allows me to add my own functionality. |
|
Subject:
Re: Override behavior of Print Screen key in Windows
Answered By: joseleon-ga on 24 Dec 2002 11:29 PST Rated: |
Hello, bryand_nyc: To override the behavior or any key on Windows, you need to install a keyboard hook, a keyboard hook is a function that will receive all the key strokes before being processed. I have found some C++ code that shows you how to do it, you can find it at: Need help translating a C++ code (to disable low level keys) http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20387028.html I have tested it with VC++ 6.0 and it works perfectly, I also include a modified version to capture only the print screen key: /****************************************************************************** Module: DisableLowLevelKeys.cpp Notices: Written 2000 Jeffrey Richter ******************************************************************************/ #define _WIN32_WINNT 0x0400 #include <Windows.h> /////////////////////////////////////////////////////////////////////////////// LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { BOOL fEatKeystroke = FALSE; if (nCode == HC_ACTION) { switch (wParam) { case WM_KEYDOWN: case WM_SYSKEYDOWN: case WM_KEYUP: case WM_SYSKEYUP: { PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam; fEatKeystroke=(p->vkCode == VK_SNAPSHOT); if (fEatKeystroke) { //Here goes your printkey code } break; } } } return(fEatKeystroke ? 1 : CallNextHookEx(NULL, nCode, wParam, lParam)); } /////////////////////////////////////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) { // Install the low-level keyboard & mouse hooks HHOOK hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinstExe, 0); // Keep this app running until we're told to stop MessageBox(NULL, TEXT("Print Key is now disabled.\n") TEXT("Click \"Ok\" to terminate this application and re-enable that key."), TEXT("Disable Low-Level Keys"), MB_OK); UnhookWindowsHookEx(hhkLowLevelKybd); return(0); } ///////////////////////////////// End Of File ///////////////////////////////// I hope it's what you were looking for and don't hesitate to request for any clarification. Search strategy vk_snapshot hook ://www.google.com/search?hl=es&ie=UTF-8&oe=utf-8&q=vk_snapshot+hook&lr= Regards. |
bryand_nyc-ga
rated this answer:
Many thanks. This totally and absolutely answers my question. |
|
Subject:
Re: Override behavior of Print Screen key in Windows
From: theta-ga on 24 Dec 2002 11:39 PST |
Hmm...lost the lock again. Need to pay more attention. :-( Well, here is my answer. ======================================= Hi bryand_nyc-ga, What you need is a way to somehow detect all PrintScreen key presses. Using the Win32 API, you can create an application that listens for particular keystrokes by utilizing any one of the following methods : ========================================================= 1) RegisterHotKey() : The simplest method involves using the API RegisterHotKey. From MSDN : "This API takes an hwnd, an ID, a virtual key, and a key modifier. If this call is successful, the hwnd's WndProc will receive a WM_HOTKEY message with the wParam of the message equal to ID whenever the virtual key and key modifier is pressed. This occurs whether the listener application window is active or not." The virtual keycode for the PrintScreen key is VK_SNAPSHOT. The WM_HOTKEY message will be posted to your app's message queue whenever he registered hotkey is pressed. The wParam value for this message contains the value IDHOT_SNAPDESKTOP whenever PrintScreen is pressed and IDHOT_SNAPWINDOW whenever Shift+PrintScreen is pressed. Check out the following links for more help : - MSDN Library Online : RegisterHotKey Function ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/RegisterHotKey.asp) - MSDN Library Online : WM_HOTKEY Notification ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputMessages/WM_HOTKEY.asp) - MSDN Magazine > December 2000 : C++ Q & A ( http://msdn.microsoft.com/msdnmag/issues/1200/c/default.aspx ) Look at the answer to the second question. Explains how to use the RegisterHotKey function in MFC. - VBNet : System-Wide Keyboard Trapping with RegisterHotKey ( http://www.mvps.org/vbnet/index.html?code/subclass/registerhotkey.htm) Visual Basic code to override PrintScreen key using the Win32 API function RegisterHotKey(). You should be able to translate this code to C++ easily. ========================================================= 2) Use low level Keyboard hooks : This is most likely the method used by the ColdKey application in your question. Although this is a more powerful method than the RegisterHotKey() call, it is also much more complicated. To capture systemwide keypresses, you will need to create a DLL. There are two keyboard Hooks that you can use to capture key presses. They are WH_KEYBOARD_LL and WH_KEYBOARD. The WH_KEYBOARD_LL hook is only available on WinNT/2K/XP while WH_KEYBOARD is available on all platforms. However, the two hooks handle key capture differently on Win95/98/Me and WinNT/2K/Xp, so you should test them both out and use the one that works for you. Another thing that you may notice is that by the time you get the PrintScreen key press message, Windows may have already copied the screen contents to the clipboard. This happens because Windows handles PrintScreen functionality at driver level. You can get past this hurdle by checking if a Bitmap is present on the clipboard when you get the keypress message, and then deleting it. Please read the following Google Groups posting for more info : - Subject: catrch printing key event Newsgroup: microsoft.public.vc.mfc ( http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=QbvkbhY0BHA.2480%40cpmsftngxa08&rnum=30) The following links will provide you with information and code explaining how you can use hooks under Windows : - MSDN Library Online : Hooks ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks.asp) - MSDN Library : Win32 Hooks by Kyle Marsh ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_hooks32.asp) - MSDN Code Samples : DisableLowLevelKeys.cpp ( http://msdn.microsoft.com/code/default.asp?url=/msdn-files/026/001/226/Source%20Files/DisableLowLevelKeys_cpp.asp) C++ code to trap and disable certain keys like ALT+TAB,ALT+ESC & CTRL+ESC on Windows NT 4 and above. - CodeProject.com : Articles on DLLs (http://www.codeproject.com/dll/index.asp#Hooks) C++ code and tutorials demonstrating the proper use of DLLs for using the various hooks available. - Keyboard Hook by Gil Dabah & Oren Becker ( http://qsoft.ragestorm.net/tutors/windows/kbhook.html ) Tutorial with sample source to demonstrate use of keyboard hooks using the Win32 API. - MSDN Magazine > October 2002 : Windows Hooks in the .NET Framework ( http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/default.aspx) Article demonstrate how to use hooks from the .NET framework. You can find many more articles and source code on hooks by doing a simple search on Google. ================================================== The following article, which explains how you can trap the Alt+Tab keypress under Windows Xp, and utilized both the methods specified above, may be of interest to you. - Using Windows XP Visual Styles and PrintWindow in Windows Applications by Paul Hellyar ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwxp/html/xpvisualstyles.asp) ================================================== Hope this helped. If you need any clarifications, just ask! Happy Coding! Regards, Theta-ga ================================================== Google/Google Groups Search Terms Used : "print screen" win32 keyboard hook "print screen" win32 print screen win32 capture key "C++" hook -dos print screen win32 capture key "C++" |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |