summaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/LowLevelKeyEvents.cxx
blob: e80ebedcdb6a86ad97db5a4f41937817317d2296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#include <windows.h>
#include <rfb_win32/LowLevelKeyEvents.h>
#include <rfb/Threading.h>
#include <rfb/LogWriter.h>
#include <list>

using namespace rfb;
using namespace win32;

static LogWriter vlog("LowLevelKeyEvents");


HHOOK hook = 0;
std::list<HWND> windows;
Mutex windowLock;


static bool filterKeyEvent(int vkCode) {
  switch (vkCode) {
  case VK_LWIN:
  case VK_RWIN:
  case VK_SNAPSHOT:
    return true;
  case VK_TAB:
    if (GetAsyncKeyState(VK_MENU) & 0x8000)
      return true;
  case VK_ESCAPE:
    if (GetAsyncKeyState(VK_MENU) & 0x8000)
      return true;
    if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
      return true;
  }
  return false;
}

LRESULT CALLBACK LowLevelKeyEventProc(int nCode,
                                      WPARAM wParam,
                                      LPARAM lParam) {
  if (nCode >= 0) {
    Lock l(windowLock);
    HWND foreground = GetForegroundWindow();
    std::list<HWND>::iterator i;
    for (i=windows.begin(); i!=windows.end(); i++) {
      if (*i == foreground) {
        UINT msgType = wParam;
        KBDLLHOOKSTRUCT* msgInfo = (KBDLLHOOKSTRUCT*)lParam;
        if (filterKeyEvent(msgInfo->vkCode)) {
          vlog.debug("filtered event %lx(%lu) %lu", msgInfo->vkCode, msgInfo->vkCode, msgType);
          PostMessage(*i, wParam, msgInfo->vkCode, (msgInfo->scanCode & 0xff) << 16);
          return 1;
        }
      }
    }
  }
  return CallNextHookEx(hook, nCode, wParam, lParam);
}


bool rfb::win32::enableLowLevelKeyEvents(HWND hwnd) {
// ***  return false; // *** THIS CODE IS EXPERIMENTAL, SO DISABLED BY DEFAULT!
  Lock l(windowLock);
  if (windows.empty() && !hook)
    hook = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyEventProc, GetModuleHandle(0), 0);
  if (hook)
    windows.push_back(hwnd);
  vlog.debug("enable %p -> %s", hwnd, hook ? "success" : "failure");
  return hook != 0;
}

void rfb::win32::disableLowLevelKeyEvents(HWND hwnd) {
  vlog.debug("disable %p", hwnd);
  Lock l(windowLock);
  windows.remove(hwnd);
  if (windows.empty() && hook) {
    UnhookWindowsHookEx(hook);
    hook = 0;
  }
}