Catch Ctrl-V before processed by the interface lazarus / 13-General ---------------------------------------------------------------------- carlangas · 2009-03-05 17:29 How could I catch the Ctrl-V event before it pastes the clipboard into a StringGrid? I manage this key with a OnKeyPress event, but it seems that the connected procedure is called after pasting the clipboard content, so I need to know the previous content of the cells, if I want to make it possible to undo changes ---------------------------------------------------------------------- Leledumbo · 2009-03-06 02:57 Might not be a good solution: What about saving the DoPasteFromClipboard in a procedural variable then override it? You can call the saved method in OnKeyPress to apply pasting. ---------------------------------------------------------------------- carlangas · 2009-03-06 09:18 I think that's what I need, but sorry, I'm just a newbie... Couldn't you post an example? ---------------------------------------------------------------------- Leledumbo · 2009-03-06 09:36 Not really sure (can't test here) but let's see... Code: [Select]type   TMethod = procedure of object; var   OrigDoPasteFromClipboard: TMethod; ... { You can do this in another method, but I prefer here   because it's one of the earliest method that gets called } procedure TForm1.FormCreate(Sender: TObject); begin   // Assuming the TStringGrid named sg1   OrigDoPasteFromClipboard:=@sg1.DoPasteFromClipboard;   // Eliminate the handler   sg1.DoPasteFromClipboard:=nil; end; ... procedure TForm1.KeyPress(Sender: TObject; var Key: Char); begin   // You can save the sg1 contents here   ...   // Call the saved handler   OrigDoPasteFromClipboard; end; ... hope it works... ---------------------------------------------------------------------- Bart · 2009-03-06 14:46 Quote from: carlangas on March 05, 2009, 06:29:46 pmHow could I catch the Ctrl-V event before it pastes the clipboard into a StringGrid? I manage this key with a OnKeyPress event, but it seems that the connected procedure is called after pasting the clipboard content, so I need to know the previous content of the cells, if I want to make it possible to undo changes What then do you do in the KeyPress handler with Ctrl+V? AFAIK the PasteFromClipboard method is not called automagically if on presses Ctr+V in the grid, the widget (OS?) just pastes the contents of the clipboard there. If you want to do handle the pasting yourself (safe undo infromation) you could do something like: Code: [Select]procedure MyGridKeyPress(Sender: TObject; var Key: Char); begin   ...   if (Key = ^V) then   begin     SaveUndoInformation;     MyGrid.Cells[Col, Row] := ClipBoard.AsText; //or something similar     Key := #0; //this should prevent the OS from handling the Ctrl+V and also pasting the text   end; end; If you want to intercept the pasting process (not only with Ctrl-+V, but also via OS context menu) you would want some BeforePaste message, which is currently not yet available  Bart ---------------------------------------------------------------------- carlangas · 2009-03-11 23:01 First of all, thanks for your help and sorry, I've been so busy to try your suggestions these days. I have tried both. For Bart's one, I have tried with Code: [Select]  if (Key = ^V) then begin   showmessage (StringGrid1.Cells[1,1]);   Key:=#0; end;      in the KeyPress method, and I get the overwritten value (the value it takes after pasting process). For Leledumbo's, I get an error: unit1.pas(46,40) Error: identifier idents no member "DoPasteFromClipboard" ---------------------------------------------------------------------- carlangas · 2009-03-13 09:44 Well, I have found it. I must manage this key press with the OnKeyDown event, not OnKeyPress. Looking into lazarus' source code, I saw in lazarus/lcl/grids.pas that ^C, ^V, ^X, and others are already handled  in KeyDown, so it seems than are firstly processed by that, and after doing its job by KeyPress. ---------------------------------------------------------------------- Vincent Snijders · 2009-03-13 09:51 For more information about key handling, see wiki.