Search code examples
delphikeypress

Backspace press detection


Is there a possibility for detection backspace key pressing (like an event) only while a cursor is over Timage component? This shortcut has to trigger dedicated image-processing related to TImage.


Solution

  • I would just enable/disable they key-pressing detection event when the mouse enters or leaves the image (OnMouseEnter, OnMouseLeave).

    You just need to have a BackDetection function (compatible with TKeyEvent) on your Form :

    procedure MyForm.BackDetection(Sender: TObject; var Key: word; Shift: TShiftState);
    begin
      if Key = VK_BACK then begin
        ... 
        ... Your image-processing code
        ...
      end;   
    end;
    

    This does require that KeyPreview is True.

    Then you just set this event, or disable it, when the mouse enter or leaves your image.

    procedure MyForm.MyImageOnMouseEnter(Sender: TObject);
    begin
      OnKeyPress := BackDetection;
    end;
    
    procedure MyForm.MyImageOnMouseLeave(Sender: TObject);
    begin
      OnKeyPress := nil;
    end;