The following code is able to lock mouse events successfully. This was my attempt to prevent the mouse from turning on the monitor when it is off, but the monitor always turns on when executing any action with the mouse, ex: left button click.
Is there some solution to this?
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MouseHook: HHOOK;
implementation
{$R *.dfm}
function LowLevelMouseProc(nCode: Integer; WParam: WParam; LParam: LParam): LRESULT; stdcall;
begin
Result := 1{CallNextHookEx(MouseHook, nCode, WParam, LParam)};
case WParam of
WM_LBUTTONDOWN:
Form1.Memo1.Lines.Add('Mouse Left Button Down');
WM_LBUTTONUP:
Form1.Memo1.Lines.Add('Mouse Left Button Up');
WM_LBUTTONDBLCLK:
Form1.Memo1.Lines.Add('Mouse Left Button Double Click');
WM_RBUTTONDOWN:
Form1.Memo1.Lines.Add('Mouse Right Button Down');
WM_RBUTTONUP:
Form1.Memo1.Lines.Add('Mouse Right Button Up');
WM_RBUTTONDBLCLK:
Form1.Memo1.Lines.Add('Mouse Right Button Double Click');
WM_MBUTTONDOWN:
Form1.Memo1.Lines.Add('Mouse Middle Button Down');
WM_MBUTTONUP:
Form1.Memo1.Lines.Add('Mouse Middle Button Up');
WM_MBUTTONDBLCLK:
Form1.Memo1.Lines.Add('Mouse Middle Button Double Click');
WM_MOUSEMOVE:
Form1.Memo1.Lines.Add('Mouse Move');
WM_MOUSEWHEEL:
Form1.Memo1.Lines.Add('Mouse Wheel');
else
Form1.Memo1.Lines.Add('Unknown Event');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
MONITOR_ON = -1;
MONITOR_OFF = 2;
MONITOR_STANDBY = 1;
begin
MouseHook := SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseProc, HInstance, 0);
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
// SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnhookWindowsHookEx(MouseHook);
end;
Edit:
I need only turn off monitor without turn off system (hibernate or suspend). And prevent mouse actions revert it.
After analyze and test @AmigoJack's suggestion, seems that disable the device (like is made using Windows Device Manager) is a possible solution to me, even if some device cannot be disabled. I'm leaving the code to help future readers with this same doubt.
DeviceCtrl
DeviceTest