| |

VerySource

 Forgot password?
 Register
Search
View: 1083|Reply: 7

How to detect how long the user has not operated with the mouse and keyboard?

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-2 09:00:02
| Show all posts |Read mode
How to detect how long the user has not operated with the mouse and keyboard? Just like a screen saver.

My program will automatically lock the program if the user does not operate within the set time.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-15 09:00:01
| Show all posts
Just use the keyboard and mouse hooks!
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-15 21:45:01
| Show all posts
Agree upstairs
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-19 19:15:01
| Show all posts
procedure TForm1.Timer1Timer(Sender: TObject);
var
  vLastInputInfo: TLastInputInfo;
begin
  vLastInputInfo.cbSize := SizeOf(TLastInputInfo);
  GetLastInputInfo(vLastInputInfo);
  Caption := Format('The user has not moved the keyboard and mouse for %d seconds',
    [(GetTickCount-vLastInputInfo.dwTime) div 1000]);
end;
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-27 17:30:01
| Show all posts
I admire it!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-29 15:45:01
| Show all posts
I think the general idea is to see if the mouse and keyboard send out event messages of movement and press...

   You can check to see if the message can handle such an event...
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-31 17:30:01
| Show all posts
var
  Form1: TForm1;
  RecordHook: HHOOK; // Hook handle
  Timer: Integer = 0; // Cumulative time, in seconds
  State: Boolean = TRUE; // Is it'online'
  //=========
   Msg: TMsg;
  WndClass: TWndClass;
  HMainWnd: HWND;
implementation

{$R *.dfm}
// form function
function WindowProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  MousePos: TPoint; // Mouse position
begin
  case (uMsg) of
    WM_TIMER:
      if (State = TRUE) then
      begin
        Inc(Timer);
        if (Timer >= 5) then // think to leave after 5 seconds
        begin
          State := FALSE;
          form1.Button1.Caption:='Leave';
        end;
      end;
    end;
  Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
end;

  // hook function
function JournalRecordProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  Msg: LongWord;
begin
  if (nCode = HC_ACTION) then // lParam points to the message structure
  begin
    Msg := PEventMsg(lParam)^.message;
    if ((Msg >= WM_KEYFIRST) and (Msg <= WM_KEYLAST)) or // keyboard message
       ((Msg >= WM_MOUSEFIRST) and (Msg <= WM_MOUSELAST)) then // mouse message
    begin
      Timer := 0;
      if (State = FALSE) then //'Leave' ->'Online'
      begin
        State := TRUE;
        form1.Button1.Caption:='Come back';
       end;
    end;
  end;
  Result := CallNextHookEx(RecordHook, nCode, wParam, lParam); // next hook
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
      // uninstall hook
  UnHookWindowsHookEx(RecordHook);
  // delete clock
  KillTimer(HMainWnd, 6);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
     // install the clock
   SetTimer(HMainWnd, 6, 1000, nil);

   // install hook
  RecordHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalRecordProc, HInstance, 0);

   // message loop
   while GetMessage(Msg, 0, 0, 0) do
  begin
    if (Msg.message = WM_CANCELJOURNAL) then // need to hook again at this time
      RecordHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalRecordProc, HInstance, 0)
    else
      DispatchMessage(Msg);
     end;
end;

end.


Probably so...
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-9-3 15:00:01
| Show all posts
usefull
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list