Author: Jonas Bilinkevicius
When I hand over focus with key TAB to ListView, ListView not select first item.
How on it?
Answer:
We have two ListView components. First component with name ListView1 contains two
events.
1 procedure TForm1.ListView1Enter(Sender: TObject);
2 begin3 HookHandle := SetWindowsHookEx(WH_KEYBOARD, @MsgHook, 0, GetCurrentThreadID);
4 end;
5 6 procedure TForm1.ListView1Exit(Sender: TObject);
7 begin8 UnHookWindowsHookEx(HookHandle);
9 end;
Second component with name ListView2 is activate with procedure MsgHook.
10 11 function MsgHook(Code: Integer; WParam: WPARAM; LParam: LPARAM): Integer; stdcall;
12 const13 KeyTAB: Integer = 983041;
14 begin15 if (Code = HC_ACTION) then16 begin17 if LParam = KeyTAB then18 begin19 frmMain.lsvCCM.SetFocus;
20 SendMessage(Form1.ListView2.Handle, WM_KEYDOWN, VK_DOWN, 1);
21 22 Result := -1;
23 Exit;
24 end;
25 end;
26 27 Result := CallNextHookEx(HookHandle, Code, WParam, LParam);
28 end;