Question

I have Two listboxes and both with Multiselect:=True. I have a popup menu that Copy/Paste to/from the Clipboard for the selected items. All works as intended.

I wanted to add keyboard CtrlC (Copy) and Ctrl+V (Paste) but after selecting items, then using Ctrl-C, the Selected items all lose selection and the first item in the list is selected and it gets copied to the Clipboard.

I am using the KeyPreview and main form OnKeyUp

if (ssCtrl in Shift) then
begin
  case Char(Key) of
   'c','C' : puCopyClick(Sender);
   'v','V' : puPasteClick(Sender);
  end;
  Exit;
end;
case Key of
  VK_Delete : puDeleteClick(Self);
end;
Exit;

How can I make the Ctrl+C etc work as the popup does?

Thanks

Was it helpful?

Solution

The best way to handle shortcut keys is to let the menu items handle them. You say that you have a popup menu that has these actions. Use the Shortcut property of the menu item to associate that menu item with the shortcut key.

That allows you to remove all the manual keyboard event handling and let the framework do it for you. That has many benefits. Not least of which is that the event will fire when the key goes down rather than when it goes up as you currently have it.

Even better would be to use actions which if I recall correctly do exist in Delphi 5. These allow you to associate a single action, for example copy to clipboard with multiple independent UI elements. For example that action can be associated with a main menu, a popup menu, and a shortcut key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top