Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #include <windows.h> |
| 20 | #include <rfb_win32/LowLevelKeyEvents.h> |
| 21 | #include <rfb/Threading.h> |
| 22 | #include <rfb/LogWriter.h> |
| 23 | #include <list> |
| 24 | |
| 25 | using namespace rfb; |
| 26 | using namespace win32; |
| 27 | |
| 28 | static LogWriter vlog("LowLevelKeyEvents"); |
| 29 | |
| 30 | |
| 31 | HHOOK hook = 0; |
| 32 | std::list<HWND> windows; |
| 33 | Mutex windowLock; |
| 34 | |
| 35 | |
| 36 | static bool filterKeyEvent(int vkCode) { |
| 37 | switch (vkCode) { |
| 38 | case VK_LWIN: |
| 39 | case VK_RWIN: |
| 40 | case VK_SNAPSHOT: |
| 41 | return true; |
| 42 | case VK_TAB: |
| 43 | if (GetAsyncKeyState(VK_MENU) & 0x8000) |
| 44 | return true; |
| 45 | case VK_ESCAPE: |
| 46 | if (GetAsyncKeyState(VK_MENU) & 0x8000) |
| 47 | return true; |
| 48 | if (GetAsyncKeyState(VK_CONTROL) & 0x8000) |
| 49 | return true; |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | LRESULT CALLBACK LowLevelKeyEventProc(int nCode, |
| 55 | WPARAM wParam, |
| 56 | LPARAM lParam) { |
| 57 | if (nCode >= 0) { |
| 58 | Lock l(windowLock); |
| 59 | HWND foreground = GetForegroundWindow(); |
| 60 | std::list<HWND>::iterator i; |
| 61 | for (i=windows.begin(); i!=windows.end(); i++) { |
| 62 | if (*i == foreground) { |
| 63 | UINT msgType = wParam; |
| 64 | KBDLLHOOKSTRUCT* msgInfo = (KBDLLHOOKSTRUCT*)lParam; |
| 65 | if (filterKeyEvent(msgInfo->vkCode)) { |
| 66 | vlog.debug("filtered event %lx(%lu) %lu", msgInfo->vkCode, msgInfo->vkCode, wParam); |
| 67 | PostMessage(*i, wParam, msgInfo->vkCode, (msgInfo->scanCode & 0xff) << 16); |
| 68 | return 1; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return CallNextHookEx(hook, nCode, wParam, lParam); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | bool rfb::win32::enableLowLevelKeyEvents(HWND hwnd) { |
| 78 | // *** return false; // *** THIS CODE IS EXPERIMENTAL, SO DISABLED BY DEFAULT! |
| 79 | Lock l(windowLock); |
| 80 | if (windows.empty() && !hook) |
| 81 | hook = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyEventProc, GetModuleHandle(0), 0); |
| 82 | if (hook) |
| 83 | windows.push_back(hwnd); |
| 84 | vlog.debug("enable %p -> %s", hwnd, hook ? "success" : "failure"); |
| 85 | return hook != 0; |
| 86 | } |
| 87 | |
| 88 | void rfb::win32::disableLowLevelKeyEvents(HWND hwnd) { |
| 89 | vlog.debug("disable %p", hwnd); |
| 90 | Lock l(windowLock); |
| 91 | windows.remove(hwnd); |
| 92 | if (windows.empty() && hook) { |
| 93 | UnhookWindowsHookEx(hook); |
| 94 | hook = 0; |
| 95 | } |
| 96 | } |