I\'m working on a control derived from TCustomControl
class which can get focus and which has some inner elements inside. Those inner elem
Remy's answer is likely your solution, but in case you're trying to do this without the restriction of encapsulating it into a control and found yourself here:
You could handle this with a three step process, as I've shown below.
The key things here are:
KeyPreview
propertyI've used a button to illustrate the process. Be sure to set your form's KeyPreview
to True
.
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
myControl: TControl;
begin
// If they pressed CTRL while over the control
if ssCtrl in Shift then
begin
myControl := ControlAtPos(ScreenToClient(Mouse.CursorPos), False, True);
// is handles nil just fine
if (myControl is TButton) then
begin
myControl.Cursor := crSizeAll;
end;
end;
end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
myControl: TControl;
begin
// If they released CTRL while over the control
if not(ssCtrl in Shift) then
begin
myControl := ControlAtPos(ScreenToClient(Mouse.CursorPos), False, True);
if (myControl is TButton) then
begin
myControl.Cursor := crDefault;
end;
end;
end;
procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
// If they move over the button, consider current CTRL key state
if ssCtrl in Shift then
begin
Button1.Cursor := crSizeAll;
end
else
begin
Button1.Cursor := crDefault;
end;
end;