2017-08-04 15:20:33 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-08-04 15:20:33 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
using System;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2019-09-05 09:00:41 +02:00
|
|
|
|
using SafeExamBrowser.Monitoring.Contracts.Mouse;
|
2019-10-01 11:30:53 +02:00
|
|
|
|
using SafeExamBrowser.Settings.Monitoring;
|
|
|
|
|
using SafeExamBrowser.WindowsApi.Contracts;
|
|
|
|
|
using SafeExamBrowser.WindowsApi.Contracts.Events;
|
2017-08-04 15:20:33 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Monitoring.Mouse
|
|
|
|
|
{
|
|
|
|
|
public class MouseInterceptor : IMouseInterceptor
|
|
|
|
|
{
|
2019-10-01 11:30:53 +02:00
|
|
|
|
private Guid? hookId;
|
2017-08-04 15:20:33 +02:00
|
|
|
|
private ILogger logger;
|
2019-10-01 11:30:53 +02:00
|
|
|
|
private INativeMethods nativeMethods;
|
2018-02-15 15:42:54 +01:00
|
|
|
|
private MouseSettings settings;
|
2017-08-04 15:20:33 +02:00
|
|
|
|
|
2019-10-01 16:24:10 +02:00
|
|
|
|
public MouseInterceptor(ILogger logger, INativeMethods nativeMethods, MouseSettings settings)
|
2017-08-04 15:20:33 +02:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
2019-10-01 11:30:53 +02:00
|
|
|
|
this.nativeMethods = nativeMethods;
|
2017-08-04 15:20:33 +02:00
|
|
|
|
this.settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-01 11:30:53 +02:00
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
hookId = nativeMethods.RegisterMouseHook(MouseHookCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
if (hookId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
nativeMethods.DeregisterMouseHook(hookId.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 14:03:43 +01:00
|
|
|
|
private bool MouseHookCallback(MouseButton button, MouseButtonState state, MouseInformation info)
|
2017-08-04 15:20:33 +02:00
|
|
|
|
{
|
|
|
|
|
var block = false;
|
|
|
|
|
|
2019-01-17 11:12:17 +01:00
|
|
|
|
block |= button == MouseButton.Auxiliary;
|
2019-01-09 16:01:56 +01:00
|
|
|
|
block |= button == MouseButton.Middle && !settings.AllowMiddleButton;
|
|
|
|
|
block |= button == MouseButton.Right && !settings.AllowRightButton;
|
2017-08-04 15:20:33 +02:00
|
|
|
|
|
|
|
|
|
if (block)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"Blocked {button.ToString().ToLower()} mouse button when {state.ToString().ToLower()}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return block;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|