Vâng, điều này có thể không được rất duyên dáng ... nhưng bạn có thể lấy lại hiện kiểm soát tập trung khá dễ dàng. Vì vậy, bạn có thể xem xét việc thiết lập một bộ đếm thời gian yêu cầu mỗi 1/2 giây hoặc lâu hơn "Trọng tâm hiện tại ở đâu?" ... Sau đó, bạn có thể quan sát các thay đổi. Ví dụ mã Delphi là dưới đây; nó sẽ khá dễ dàng để thích nghi, vì công việc thực sự là trong các cuộc gọi Windows API.
<snip>
function TForm1.GetCurrentHandle: integer;
var
activeWinHandle: HWND;
focusedThreadID : DWORD;
begin
//return the Windows handle of the currently focused control
Result := 0;
activeWinHandle := GetForegroundWindow;
focusedThreadID := GetWindowThreadProcessID(activeWinHandle,nil);
if AttachThreadInput(GetCurrentThreadID,focusedThreadID,true) then begin
try
Result := GetFocus;
finally
AttachThreadInput(GetCurrentThreadID, focusedThreadID, false);
end;
end; //if attached
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
//give notification if the handle changed
//(this code gets fired by a timer)
CurrentHandle := GetCurrentHandle;
if CurrentHandle <> PreviousHandle then begin
Label1.Caption := 'Last focus change occurred @ ' + DateTimeToStr(Now);
end;
PreviousHandle := CurrentHandle;
end;
<snip>
Trình theo dõi lấy nét dường như đã chuyển đến đây: https://github.com/Microsoft/WPF-Samples/tree/master/Accessibility/FocusTracker (Xin lỗi, tôi không có đủ danh tiếng để thêm nhận xét này làm nhận xét cho câu trả lời có liên quan ở trên). – greiginsydney