2017-08-03 15:35:22 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-08-03 15:35:22 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using SafeExamBrowser.WindowsApi.Constants;
|
2019-10-01 11:30:53 +02:00
|
|
|
|
using SafeExamBrowser.WindowsApi.Contracts.Events;
|
2018-08-16 11:23:37 +02:00
|
|
|
|
using SafeExamBrowser.WindowsApi.Delegates;
|
2017-08-04 08:25:49 +02:00
|
|
|
|
using SafeExamBrowser.WindowsApi.Types;
|
2017-08-03 15:35:22 +02:00
|
|
|
|
|
2019-03-29 07:46:21 +01:00
|
|
|
|
namespace SafeExamBrowser.WindowsApi.Hooks
|
2017-08-03 15:35:22 +02:00
|
|
|
|
{
|
|
|
|
|
internal class KeyboardHook
|
|
|
|
|
{
|
2017-08-04 12:19:56 +02:00
|
|
|
|
private bool altPressed, ctrlPressed;
|
2019-10-01 11:30:53 +02:00
|
|
|
|
private KeyboardHookCallback callback;
|
|
|
|
|
private IntPtr handle;
|
2018-09-27 11:24:13 +02:00
|
|
|
|
private HookDelegate hookDelegate;
|
2017-08-04 12:19:56 +02:00
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
internal Guid Id { get; private set; }
|
2017-08-03 15:35:22 +02:00
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
internal KeyboardHook(KeyboardHookCallback callback)
|
2017-08-03 15:35:22 +02:00
|
|
|
|
{
|
2019-10-01 11:30:53 +02:00
|
|
|
|
this.callback = callback;
|
|
|
|
|
this.Id = Guid.NewGuid();
|
2017-08-03 15:35:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Attach()
|
|
|
|
|
{
|
2018-09-25 12:10:34 +02:00
|
|
|
|
var process = System.Diagnostics.Process.GetCurrentProcess();
|
|
|
|
|
var module = process.MainModule;
|
|
|
|
|
var moduleHandle = Kernel32.GetModuleHandle(module.ModuleName);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
|
2019-03-06 16:10:00 +01:00
|
|
|
|
// IMPORTANT:
|
2017-08-04 12:19:56 +02:00
|
|
|
|
// Ensures that the hook delegate does not get garbage collected prematurely, as it will be passed to unmanaged code.
|
|
|
|
|
// Not doing so will result in a <c>CallbackOnCollectedDelegate</c> error and subsequent application crash!
|
2018-09-27 11:24:13 +02:00
|
|
|
|
hookDelegate = new HookDelegate(LowLevelKeyboardProc);
|
2019-10-01 11:30:53 +02:00
|
|
|
|
handle = User32.SetWindowsHookEx(HookType.WH_KEYBOARD_LL, hookDelegate, moduleHandle, 0);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool Detach()
|
|
|
|
|
{
|
2019-10-01 11:30:53 +02:00
|
|
|
|
return User32.UnhookWindowsHookEx(handle);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
|
|
|
|
|
{
|
|
|
|
|
if (nCode >= 0)
|
|
|
|
|
{
|
|
|
|
|
var keyData = (KBDLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
|
2017-08-04 12:19:56 +02:00
|
|
|
|
var modifier = GetModifiers(keyData, wParam.ToInt32());
|
2017-08-04 15:20:33 +02:00
|
|
|
|
var state = GetState(wParam.ToInt32());
|
2017-08-03 15:35:22 +02:00
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
if (callback((int) keyData.KeyCode, modifier, state))
|
2017-08-03 15:35:22 +02:00
|
|
|
|
{
|
|
|
|
|
return (IntPtr) 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
return User32.CallNextHookEx(handle, nCode, wParam, lParam);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 12:19:56 +02:00
|
|
|
|
private KeyState GetState(int wParam)
|
|
|
|
|
{
|
|
|
|
|
switch (wParam)
|
|
|
|
|
{
|
|
|
|
|
case Constant.WM_KEYDOWN:
|
|
|
|
|
case Constant.WM_SYSKEYDOWN:
|
|
|
|
|
return KeyState.Pressed;
|
|
|
|
|
case Constant.WM_KEYUP:
|
|
|
|
|
case Constant.WM_SYSKEYUP:
|
|
|
|
|
return KeyState.Released;
|
|
|
|
|
default:
|
2019-01-17 11:12:17 +01:00
|
|
|
|
return KeyState.Unknown;
|
2017-08-04 12:19:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private KeyModifier GetModifiers(KBDLLHOOKSTRUCT keyData, int wParam)
|
2017-08-03 15:35:22 +02:00
|
|
|
|
{
|
|
|
|
|
var modifier = KeyModifier.None;
|
|
|
|
|
|
2017-08-04 12:19:56 +02:00
|
|
|
|
TrackCtrlAndAlt(keyData, wParam);
|
|
|
|
|
|
|
|
|
|
if (altPressed || keyData.Flags.HasFlag(KBDLLHOOKSTRUCTFlags.LLKHF_ALTDOWN))
|
2017-08-03 15:35:22 +02:00
|
|
|
|
{
|
|
|
|
|
modifier |= KeyModifier.Alt;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 12:19:56 +02:00
|
|
|
|
if (ctrlPressed)
|
2017-08-03 15:35:22 +02:00
|
|
|
|
{
|
|
|
|
|
modifier |= KeyModifier.Ctrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return modifier;
|
|
|
|
|
}
|
2017-08-04 12:19:56 +02:00
|
|
|
|
|
|
|
|
|
private void TrackCtrlAndAlt(KBDLLHOOKSTRUCT keyData, int wParam)
|
|
|
|
|
{
|
|
|
|
|
var keyCode = keyData.KeyCode;
|
|
|
|
|
|
2019-03-06 16:10:00 +01:00
|
|
|
|
if (keyCode == (uint) VirtualKeyCode.LeftControl || keyCode == (uint) VirtualKeyCode.RightControl)
|
2017-08-04 12:19:56 +02:00
|
|
|
|
{
|
|
|
|
|
ctrlPressed = IsPressed(wParam);
|
|
|
|
|
}
|
2019-03-06 16:10:00 +01:00
|
|
|
|
else if (keyCode == (uint) VirtualKeyCode.LeftAlt || keyCode == (uint) VirtualKeyCode.RightAlt)
|
2017-08-04 12:19:56 +02:00
|
|
|
|
{
|
|
|
|
|
altPressed = IsPressed(wParam);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 16:10:00 +01:00
|
|
|
|
if (ctrlPressed && altPressed && keyCode == (uint) VirtualKeyCode.Delete)
|
2017-08-04 12:19:56 +02:00
|
|
|
|
{
|
|
|
|
|
// When the Secure Attention Sequence is pressed, the WM_KEYUP / WM_SYSKEYUP messages for CTRL and ALT get lost...
|
|
|
|
|
ctrlPressed = false;
|
|
|
|
|
altPressed = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsPressed(int wParam)
|
|
|
|
|
{
|
|
|
|
|
return wParam == Constant.WM_KEYDOWN || wParam == Constant.WM_SYSKEYDOWN;
|
|
|
|
|
}
|
2017-08-03 15:35:22 +02:00
|
|
|
|
}
|
|
|
|
|
}
|