在keydown处理程序中将Ctrl-Ins更改为Ctrl-C

问题描述 投票:0回答:1

我有一个第三方控件(DevExpress TcxVirtualTreeList)讨厌接收Ins keypress(访问冲突)并且它也处理老式的Ctrl-Ins(复制到剪贴板)。

所以我想在其OnKeyDown处理程序中将Ctrl-Ins更改为Ctrl-C:

 if (Key=VK_INSERT) and (Shift=[]) then              // Insert
 begin  
    // Handle insert ourselves
    ...
    Key := 0;
 end
 else if (Key=VK_INSERT) and (Shift=[ssCtrl]) then   // Ctrl-Ins
 begin
    Key := 67; // 'C'   
 end

但这不起作用。所选文本(在TcxVirtualTreeList的内部编辑器中)不会像Ctrl-C那样复制到剪贴板。

我做错了什么以及如何获得理想的结果?

  • 我已经确认C确实是67(BTW是VK_C在任何单位中定义的吗?)
  • Form上有一个OnKeydown处理程序,其中KeyPreview=false处理Ctrl-Shift-C和Ctrl-Shift-V。禁用该处理程序没有任何区别(如预期的那样)。

[用普通TEdit编辑测试用例] 对不起,我提到它是DevEx控件让我感到困惑。这与这个问题无关。

我设法在一个带有TEdit的小应用程序中测试它,反过来(从Form到Edit)和Form.KeyPreview=true

procedure TFrmChangeKeyInDown.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
   if (Key=VK_INSERT) and (Shift=[]) then
   begin
      Memo1.Lines.Add('FormKeyDown: Ins detected');
   end
   else if (Key=VK_INSERT) and (Shift=[ssCtrl]) then
   begin
      Memo1.Lines.Add('FormKeyDown: Ctrl-Ins detected, changing to Ctrl-C');
      Key := 67;
   end;
end;

procedure TFrmChangeKeyInDown.EditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
   if (Key=VK_INSERT) and (Shift=[]) then
   begin
      Memo1.Lines.Add('EditKeyDown: Ins detected');
   end
   else if (Key=VK_INSERT) and (Shift=[ssCtrl]) then
   begin
      Memo1.Lines.Add('EditKeyDown: Ctrl-Ins detected');
   end
   else if (Key=67) and (Shift=[ssCtrl]) then
   begin
      Memo1.Lines.Add('EditKeyDown: Ctrl-C detected');
   end;
end;

在编辑控件中键入内容,选择一个单词,按Ctrl-Ins,备忘录显示:

FormKeyDown: Ctrl-Ins detected, changing to Ctrl-C
EditKeyDown: Ctrl-C detected

但我的剪贴板缓冲区的内容现在不是那个词。使用Ctrl-C执行此操作,它工作正常。

delphi delphi-xe2
1个回答
1
投票

不知道这是否是可接受的解决方案,但如果你添加

Edit.CopyToClipboard;

到EditKeyDown处理程序,就在(或代替)之后


    Memo1.Lines.Add('EditKeyDown: Ctrl+C detected');

然后它会将文本复制到剪贴板,就像你自己按下Ctrl + C一样。如果您正在使用的TreeList控件上没有CopyToClipboard,那么您可以使用

Clipboard.AsText:=<Text to copy to clipboard>

如果您可以从ListView访问当前选定的文本。

Ctrl + C处理不是由VCL完成的,而是由Windows,AFAIK完成的,所以你不能只将“Ctrl + C / Ctrl + V”输入到VCL控件中,并期望它执行剪贴板操作。

© www.soinside.com 2019 - 2024. All rights reserved.