1
votes

I'm building an editor which uses a TImage to display a picture and has mouse events to be able to draw, move, and resize boxes on the image. All this works perfectly. Now I'm trying to implement the ability to use the arrows on the keyboard to move the selected box, but A) TImage does not get any focus, and B) TImage does not have any key events (because it cannot get focus). I guess I could cheat and switch on the form's KeyPreview property and catch them there, but there's many other controls on this form and I'd need to make sure the user is intending to work with the image. For example, if user has focus in the TEdit control, the arrow keys shall only affect this memo, and not modifying the image.

So is there any way to put or fake some kind of focus in the TImage to recognize key events?

1
If you are serious about building an image editor you should really build your own (windowed) control descending from TCustomControl. (And, by the way, the reason why TImage cannot receive focus is this: TImage is a graphic VCL control, which means that it has no window handle; it is not a window.) - Andreas Rejbrand
Well it's not any huge image editor, there's only one purpose of this editor, to draw layered boxes on the image. Layered meaning they're not actually part of the image. I'm making this an "Image Map" editor for HTML. You do have a point, but I don't think it's worth all the work of building a custom control just for this. - Jerry Dodge
Building a custom control is trivial. Painting it is trivial. - David Heffernan
@DavidHeffernan Indeed, doing a custom control is a great idea as always, but this is one tiny little hurdle which I need to jump - you don't need to jump 100 feet over a 3 foot hurdle :P - Jerry Dodge
You don't need to use any control or component for like this project! Just the TForm enough for it :) you have to learn what's mean of image processing ! Just 1 form and canvas of this form enough - relativ

1 Answers

4
votes

Only controls that inherit from TWinControl can receive keyboard focus.
TImage descents from TGraphicControl and cannot receive keyboard events.

You can put the Image on top of a panel which sits on top of another control e.g. TEdit and give that focus if the Image is selected.
Then just use the OnKeyPress event of the non-visible edit.
Make sure to disallow the tab key if you don't want that to change the focus to another control.

procedure TForm8.Image1Click(Sender: TObject);
begin
  Edit1.SetFocus;
end;

procedure TForm8.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #9 then Key = #0; //disable tab key.
  case key of
    //do stuff here
  end; {case}
end;