Delphi: Cannot capture Ctrl+C if user pressed the sequence too quickly

Ctrl+C is translated to a character message. So you better use a OnKeyPress handler (which is fired in response to a WM_CHAR):

procedure <anObject>.KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = ^C then
    begin
      // Copy code
    end;
end;


update: I believe what's happening is this: when pressing quickly, the user is pressing 'Ctrl', then pressing 'C', then releasing 'Ctrl', lastly releasing 'C'. As you can see when the OnKeyUp for 'C' is fired the 'Ctrl' key is already released. You won't have this kind of problem with the translated message, if the OS registered the 'copy' key then OnKeyPress will be fired.


Usually, OnKeyDown is more preferable then OnKeyup for such combo. Because users usually know to press those shift key before the char key but don't have a strict sense of which one to release first. Also, you can change the var Key to 0 to prevent the keys to be further interpreted by other levels of key events to override some default behaviour.


It is not a sequence, it is a key combination. This means that Ctrl and C must be pressed at the same time. If the user doesn't do that, it can't be captured as Ctrl+C.

But I am guessing. I can't tell what the user is doing. Perhaps there is also a problem with the keyboard or the driver for it.


To account for what Rob said (accidently accepting other shift keys), change your code to:

if (Shift = [ssCtrl]) and (Upcase(Char(Key)) = 'C') then

Tags:

Delphi