diff --git a/artifacts/scripting/hookscripts.txt b/artifacts/scripting/hookscripts.txt index a02a99c4..b9be5b8c 100644 --- a/artifacts/scripting/hookscripts.txt +++ b/artifacts/scripting/hookscripts.txt @@ -340,11 +340,14 @@ HOOK_KEYPRESS (hs_keypress.int) Runs once every time when any key was pressed or released. DX codes: (see dik.h header) VK codes: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx +NOTE: if you want to override a key, the new key DX scancode should be the same for both pressed and released events. int arg1 - event type: 1 - pressed, 0 - released int arg2 - key DX scancode int arg3 - key VK code (very similar to ASCII codes) +int ret1 - overrides the pressed key (a new key DX scancode or 0 for no override) + ------------------------------------------- HOOK_MOUSECLICK (hs_mouseclick.int) diff --git a/sfall/HookScripts.cpp b/sfall/HookScripts.cpp index 35e9a63b..a4286845 100644 --- a/sfall/HookScripts.cpp +++ b/sfall/HookScripts.cpp @@ -25,6 +25,7 @@ #include "FalloutEngine.h" #include "HookScripts.h" #include "Inventory.h" +#include "LoadGameHook.h" #include "Logging.h" #include "PartyControl.h" #include "ScriptExtender.h" @@ -761,18 +762,27 @@ void __declspec(naked) AmmoCostHookWrapper() { } } -void _stdcall KeyPressHook( DWORD dxKey, bool pressed, DWORD vKey ) -{ +DWORD _stdcall KeyPressHook(DWORD dxKey, bool pressed, DWORD vKey) { + if (!IsMapLoaded()) { + return 0; + } + DWORD result = 0; BeginHook(); ArgCount = 3; args[0] = (DWORD)pressed; args[1] = dxKey; args[2] = vKey; RunHookScript(HOOK_KEYPRESS); + if (cRet != 0) dxKey = result = rets[0]; InventoryKeyPressedHook(dxKey, pressed, vKey); EndHook(); + return result; } + void _stdcall MouseClickHook(DWORD button, bool pressed) { + if (!IsMapLoaded()) { + return; + } BeginHook(); ArgCount = 2; args[0] = (DWORD)pressed; diff --git a/sfall/HookScripts.h b/sfall/HookScripts.h index 656f2c47..9ae27eb0 100644 --- a/sfall/HookScripts.h +++ b/sfall/HookScripts.h @@ -63,5 +63,5 @@ void HookScriptClear(); extern DWORD InitingHookScripts; extern void __declspec() AmmoCostHookWrapper(); void _stdcall MouseClickHook(DWORD button, bool pressed); -void _stdcall KeyPressHook(DWORD dxKey, bool pressed, DWORD vKey); +DWORD _stdcall KeyPressHook(DWORD dxKey, bool pressed, DWORD vKey); void _stdcall RunHookScriptsAtProc(DWORD procId); \ No newline at end of file diff --git a/sfall/dinput.cpp b/sfall/dinput.cpp index ca41bf86..c83bb1ca 100644 --- a/sfall/dinput.cpp +++ b/sfall/dinput.cpp @@ -257,12 +257,19 @@ public: RunGlobalScripts2(); - if (!b || bufferedPresses.empty() || (d&DIGDD_PEEK)) { + if (!b || bufferedPresses.empty() || (d & DIGDD_PEEK)) { HRESULT hr = RealDevice->GetDeviceData(a, b, c, d); if (FAILED(hr) || !b || !(*c)) return hr; + DWORD keyOverride, oldState; for (DWORD i = 0; i < *c; i++) { + oldState = KeysDown[b[i].dwOfs]; KeysDown[b[i].dwOfs] = b[i].dwData & 0x80; - KeyPressHook(b[i].dwOfs, (b[i].dwData & 0x80) > 0, MapVirtualKeyEx(b[i].dwOfs, MAPVK_VSC_TO_VK, keyboardLayout)); + keyOverride = KeyPressHook(b[i].dwOfs, (b[i].dwData & 0x80) > 0, MapVirtualKeyEx(b[i].dwOfs, MAPVK_VSC_TO_VK, keyboardLayout)); + if (keyOverride != 0) { + KeysDown[b[i].dwOfs] = oldState; + b[i].dwOfs = keyOverride; + KeysDown[b[i].dwOfs] = b[i].dwData & 0x80; + } } if (KeysDown[DebugEditorKey]) RunDebugEditor(); return hr;