jiggling the mouse

Did you ever want to prevent the screen saver to start because your program is displaying something and you didn’t want to force the user to move the mouse every now and then?

Until Windows XP it was possible to prevent the screen saver from becoming active by just handling the WM_SYSCOMMAND message with wParam = SC_SCREENSAVE like this:

procedure Tf_MeasurementGraph.AppMessage(var _Msg: TMsg; var _Handled: Boolean);
  if FIsRecording then
    // prevent screen saver from starting
    if ((WM_SYSCOMMAND = _Msg.Message) and (SC_SCREENSAVE = _Msg.wParam)) then
      _Handled := True;

This apparently stopped working with Windows Vista. Other options introduced then only work if there was no password protection on the screen saver. So I asked the Google oracle and found this answer on Stack Overflow.

My solution then is this procedure:

procedure JiggleMouse;
  Inpt: TInput;
  Inpt.Itype := INPUT_MOUSE;
  Inpt.mi.dx := 0;
  Inpt.mi.dy := 0;
  Inpt.mi.mouseData := 0;
  Inpt.mi.dwFlags := MOUSEEVENTF_MOVE;
  Inpt.mi.time := 0;
  Inpt.mi.dwExtraInfo := 0;
  SendInput(1, Inpt, SizeOf(Inpt));

Call it in regular intervals and the screen saver will not start.

This is now (or soon will be) in the u_dzOsUtils unit which is part of my dzlib utility library.