Home > Back-end >  A component event small learning problems (haven't write for a long time, the problem)
A component event small learning problems (haven't write for a long time, the problem)

Time:09-20

The unit NoActionResponse;

Interface

USES the
The Vcl. ExtCtrls System. Classes, Winapi. Windows, Winapi. Messages, Vcl. Forms, Vcl. Dialogs;

Type
Time to no action//keyboard, mouse response, triggering event
TNoActionResponse=class (TTimer)
Private
FOnTimer: TNotifyEvent;
FNum: Integer;
Protected
Procedure Timer; Override.
Public
The constructor Create (AOwner: TComponent); Override.
The destructor Destroy; Override.
Procedure AppMessageHandler (var Msg: TMsg; Var Handled: Boolean);
Published
//all of the attributes of the events are a kind of, rely on Windows message to perform, and all the news event names are defined, or define your own message also triggers the corresponding event
The property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;
//run after a few time, triggering
The property Num: Integer read FNum write FNum default 1;
The end;

Procedure Register;

Implementation

Procedure TNoActionResponse. AppMessageHandler (var Msg: TMsg; Var Handled: Boolean);
The begin
If (Msg) message=WM_MOUSEMOVE) or (Msg) message=WM_KEYDOWN) then
The begin
//off to open, again the timing, the writing is not very good, is there a better way
Enabled:=False;
Enabled:=True;
The end;
The end;

The constructor TNoActionResponse. Create (AOwner: TComponent);
The begin
Inherited the Create (AOwner);
Application. The OnMessage:=AppMessageHandler;
The end;

Destructor TNoActionResponse. Destroy;
The begin

Inherited;
The end;

Procedure TNoActionResponse. The Timer;
The begin
FNum: FNum=1;
If (Assigned (FOnTimer)) and (FNum=0) then
FOnTimer (Self);
The end;

Procedure Register;
The begin
RegisterComponents (" test ", [TNoActionResponse]);
The end;

End.

This is nothing suddenly want to write a team, didn't write for so many years, have forgotten almost!

Demand is to write a time controls, within the prescribed period of time the keyboard, the mouse without any response or no movement, triggering event, such as watching players, within 3 according to the keyboard, the mouse nothing happened, what is automatically hide button,

After I finished the above components, installation testing have no reaction, I think it must be there to write wrong, so I ask you the reader

CodePudding user response:

Use win API GetLastInputInfo
I use so:
The function IsKMActionByInterval (const SpaceTime: Integer) : Boolean;
Var
ALastInputInfo: TLastInputInfo;
The begin
Result:=False;
ALastInputInfo. CbSize:=SizeOf (aLastInputInfo);
If GetLastInputInfo (aLastInputInfo) then
The begin
If GetTickCount - aLastInputInfo. DwTime & gt; SpaceTime then
Result:=True;
The end;
The end;

CodePudding user response:

reference 1st floor jankercsdn response:
use win API GetLastInputInfo
I use so:
The function IsKMActionByInterval (const SpaceTime: Integer) : Boolean;
Var
ALastInputInfo: TLastInputInfo;
The begin
Result:=False;
ALastInputInfo. CbSize:=SizeOf (aLastInputInfo);
If GetLastInputInfo (aLastInputInfo) then
The begin
If GetTickCount - aLastInputInfo. DwTime & gt; SpaceTime then
Result:=True;
The end;
The end;


See what you mean, but I just want to learn about the components of events is how to write, didn't write for a long time, want to learn

CodePudding user response:

Release OnTimer event of trying to cover the superclass of the same event, but it is invalid, can only be released a new event, from TTimer inheritance is mainly in order to reuse the Timer triggering event, when trigger would again, but only covers the Timer can't trigger, create a private process in the class, the OnTimer refers to the process, in which to write the code would, also can not from TTimer inheritance, and starting from TComponent, but still use the Timer component in the class, there are quite a few of the latter usage, would restore the problem can use a temporary integer variable to hold the incoming number, at the end of the would then assigned to FNum, restore the initial state,

CodePudding user response:

TTimer as private members, dynamically generated it, set the ontimer event handler,
Every move the mouse or keyboard, Reset TTimer (namely the enable:=false; Enable:=true)

CodePudding user response:

Whether can use the Application. The OnIdle events to handle
http://bbs.csdn.net/topics/370015169

CodePudding user response:

GetTickCount and GetLastInputInfo function, can get a keyboard, mouse idle time
http://bbs.csdn.net/topics/90372816
  • Related