blob: e80ebedcdb6a86ad97db5a4f41937817317d2296 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* 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
25using namespace rfb;
26using namespace win32;
27
28static LogWriter vlog("LowLevelKeyEvents");
29
30
31HHOOK hook = 0;
32std::list<HWND> windows;
33Mutex windowLock;
34
35
36static 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
54LRESULT 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)) {
Peter Åstrandddcdbea2008-12-10 10:43:53 +000066 vlog.debug("filtered event %lx(%lu) %lu", msgInfo->vkCode, msgInfo->vkCode, msgType);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000067 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
77bool 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
88void 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}