2009-02-11 09:24:56 +00:00
|
|
|
#include "Global.h"
|
2009-09-25 08:36:42 +00:00
|
|
|
#include "InputManager.h"
|
|
|
|
#include "Config.h"
|
2009-02-11 09:24:56 +00:00
|
|
|
|
|
|
|
#include "KeyboardHook.h"
|
|
|
|
#include "WindowsKeyboard.h"
|
|
|
|
#include "VKey.h"
|
|
|
|
#include "WndProcEater.h"
|
|
|
|
|
|
|
|
extern HINSTANCE hInst;
|
|
|
|
LRESULT CALLBACK IgnoreKeyboardHook(int code, WPARAM wParam, LPARAM lParam);
|
|
|
|
|
|
|
|
ExtraWndProcResult StartHooksWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *output);
|
|
|
|
|
|
|
|
class IgnoreKeyboardHookDevice;
|
|
|
|
static IgnoreKeyboardHookDevice *ikhd = 0;
|
|
|
|
|
|
|
|
class IgnoreKeyboardHookDevice : public WindowsKeyboard {
|
|
|
|
public:
|
|
|
|
HHOOK hHook;
|
|
|
|
// When binding, eat all keys
|
|
|
|
int binding;
|
|
|
|
|
|
|
|
IgnoreKeyboardHookDevice() : WindowsKeyboard(IGNORE_KEYBOARD, L"Ignore Keyboard") {
|
|
|
|
// Don't want to send keypress/keyrelease messages.
|
|
|
|
for (int i=0; i<numPhysicalControls; i++) {
|
|
|
|
physicalControls[i].vkey = 0;
|
|
|
|
}
|
|
|
|
hHook = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Update() {
|
|
|
|
// So I'll bind to left/right control/shift instead of generic ones.
|
|
|
|
virtualControlState[VK_SHIFT] = 0;
|
|
|
|
virtualControlState[VK_CONTROL] = 0;
|
|
|
|
virtualControlState[VK_MENU] = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-20 10:48:22 +00:00
|
|
|
int Activate(InitInfo *initInfo) {
|
2009-02-11 09:24:56 +00:00
|
|
|
if (ikhd) ikhd->Deactivate();
|
2009-09-20 10:48:22 +00:00
|
|
|
binding = initInfo->bindingIgnore;
|
2009-11-30 20:52:09 +00:00
|
|
|
hWndProc = initInfo->hWndProc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
printf( "(Lilypad) StartHook\n" );
|
|
|
|
hWndProc->Eat(StartHooksWndProc, EATPROC_NO_UPDATE_WHILE_UPDATING_DEVICES);
|
|
|
|
|
2009-02-11 09:24:56 +00:00
|
|
|
InitState();
|
|
|
|
ikhd = this;
|
|
|
|
active = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deactivate() {
|
2009-11-30 20:52:09 +00:00
|
|
|
printf( "(Lilypad) KillHook\n" );
|
|
|
|
|
2009-02-11 09:24:56 +00:00
|
|
|
FreeState();
|
|
|
|
if (active) {
|
|
|
|
if (hHook) {
|
|
|
|
UnhookWindowsHookEx(hHook);
|
|
|
|
hHook = 0;
|
|
|
|
}
|
|
|
|
if (ikhd == this) ikhd = 0;
|
|
|
|
active = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void EnumHookDevices() {
|
|
|
|
dm->AddDevice(new IgnoreKeyboardHookDevice());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Makes sure hooks are started in correct thread.
|
|
|
|
ExtraWndProcResult StartHooksWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *output) {
|
2009-03-24 00:12:39 +00:00
|
|
|
// Don't remove too quickly - could be other things happening in other threads.
|
|
|
|
// Attempted fix for keyboard input occasionally not working.
|
|
|
|
static int counter = 0;
|
2009-02-11 09:24:56 +00:00
|
|
|
if (ikhd && !ikhd->hHook && ikhd->active) {
|
2009-03-24 00:12:39 +00:00
|
|
|
counter = 0;
|
2009-11-30 20:52:09 +00:00
|
|
|
printf( "(Lilypad) SetHook\n" );
|
|
|
|
ikhd->hHook = SetWindowsHookEx(WH_KEYBOARD_LL, IgnoreKeyboardHook, hInst, 0);//GetCurrentThreadId());
|
2009-02-11 09:24:56 +00:00
|
|
|
if (ikhd->hHook == 0) ikhd->Deactivate();
|
|
|
|
}
|
2009-11-30 20:52:09 +00:00
|
|
|
printf( "(Lilypad) HookThinking!\n" );
|
2009-03-24 00:12:39 +00:00
|
|
|
counter ++;
|
|
|
|
if (counter % 1000 == 0)
|
|
|
|
return CONTINUE_BLISSFULLY_AND_RELEASE_PROC;
|
|
|
|
else
|
|
|
|
return CONTINUE_BLISSFULLY;
|
2009-02-11 09:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK IgnoreKeyboardHook(int code, WPARAM wParam, LPARAM lParam) {
|
|
|
|
HHOOK hHook = 0;
|
|
|
|
if (ikhd) {
|
2009-11-30 20:52:09 +00:00
|
|
|
printf( "(Lilypad) Hooked!!\n" );
|
2009-02-11 09:24:56 +00:00
|
|
|
hHook = ikhd->hHook;
|
|
|
|
if (hHook) {
|
|
|
|
if (code == HC_ACTION) {
|
|
|
|
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
|
|
|
|
KBDLLHOOKSTRUCT* key = (KBDLLHOOKSTRUCT*) lParam;
|
|
|
|
if (key->vkCode < 256) {
|
2009-03-15 15:00:22 +00:00
|
|
|
for (int i=0; i<ikhd->pads[0][0].numBindings; i++) {
|
|
|
|
if (ikhd->pads[0][0].bindings[i].controlIndex == key->vkCode) {
|
2009-02-11 09:24:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ikhd->binding){
|
|
|
|
ikhd->UpdateKey(key->vkCode, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2009-03-11 17:25:51 +00:00
|
|
|
if (config.vistaVolume) {
|
|
|
|
if (key->vkCode == VK_VOLUME_DOWN) {
|
|
|
|
SetVolume(config.volume-3);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (key->vkCode == VK_VOLUME_UP) {
|
|
|
|
SetVolume(config.volume+3);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 09:24:56 +00:00
|
|
|
}
|
|
|
|
else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
|
|
|
|
KBDLLHOOKSTRUCT* key = (KBDLLHOOKSTRUCT*) lParam;
|
|
|
|
if (ikhd->binding && key->vkCode < 256)
|
|
|
|
ikhd->UpdateKey(key->vkCode, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CallNextHookEx(hHook, code, wParam, lParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CallNextHookEx(hHook, code, wParam, lParam);
|
|
|
|
}
|
|
|
|
|