【问题标题】:Why the shortcut doesn't work in my Delphi program?为什么快捷方式在我的 Delphi 程序中不起作用?
【发布时间】:2021-12-15 09:25:14
【问题描述】:

我在 Delphi 10.4 中编写了一个程序。 UI 的主要部分只是一个 TMemo。当用户在其中输入内容时,应用程序会自动将 TMemo 中的文本复制到剪贴板。它看起来像这样:

此自动复制部分效果很好。但是,我也想让用户通过快捷方式更改深色主题或浅色主题。我启用了深色主题和浅色主题。

代码如下所示:

unit Unit1;

interface

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
    System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Clipbrd, System.Actions,
    Vcl.ActnList, Vcl.Themes;

type
    TForm1 = class(TForm)
        txt: TMemo;
        ActionList1: TActionList;
        act_change_theme: TAction;
        procedure txtChange(Sender: TObject);
        procedure act_change_themeExecute(Sender: TObject);
        procedure FormCreate(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

var
    Form1: TForm1;

var
    is_dark: Boolean;

implementation

{$R *.dfm}

function ShortCut(Key: Word; Shift: TShiftState): TShortCut;
begin
    Result := 0;
    if HiByte(Key) <> 0 then
        Exit; // if Key is national character then it can't be used as shortcut
    Result := Key;
    if ssShift in Shift then
        Inc(Result, scShift); // this is identical to "+" scShift
    if ssCtrl in Shift then
        Inc(Result, scCtrl);
    if ssAlt in Shift then
        Inc(Result, scAlt);
end;

procedure TForm1.act_change_themeExecute(Sender: TObject);
begin
    if is_dark then
    begin
        TStyleManager.TrySetStyle('Windows', false);
        is_dark := false;
    end
    else
    begin
        TStyleManager.TrySetStyle('Carbon', false);
        is_dark := true;
    end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    is_dark := false;
    act_change_theme.ShortCut := ShortCut(Word('d'), [ssCtrl]);
end;

procedure TForm1.txtChange(Sender: TObject);
begin
    try
        Clipboard.AsText := txt.Lines.GetText;
    except
        on E: Exception do
    end;

end;

end.

但是,当我按下 ctrl+d 时,什么也没发生。我尝试调试它,发现 ctrl+d 永远不会触发操作的快捷方式。为什么会这样?如何解决?我以前用过快捷功能,效果很好。

【问题讨论】:

    标签: delphi keyboard-shortcuts action hotkeys


    【解决方案1】:

    尝试Word('D'),或常量vkD,而不是Word('d')。快捷方式使用虚拟键代码,字母使用其大写值表示为虚拟键。在编辑控件中键入大写或小写字母使用相同的虚拟键,当键转换为文本字符时,由当前的转换状态决定字母的大小写。

    另请注意,VCL 在Vcl.Menus 单元中有自己的ShortCut() 函数(还有TextToShortCut()),用于创建TShortCut 值,因此您无需编写自己的函数。

    Representing Keys and Shortcuts,尤其是Representing Shortcuts as Instances of TShortCut

    此外,您的TAction 在设计时明确放置在表单上,​​因此您应该使用对象检查器而不是在代码中简单地分配其ShortCut。然后框架会自动为您处理这些细节。

    【讨论】:

    • 谢谢!我不知道我可以直接在对象检查器中分配快捷方式。现在可以了。顺便说一句,您个人资料中的链接 forums.embarcadero.com 无效。
    • @user130268 这是因为 Embarcadero 不久前关闭了他们的论坛。我必须更新我的个人资料。
    • Embarcardero 关闭官方论坛后最活跃的 Delphi 论坛在哪里?
    • @user130268 blogs.embarcadero.com/community。 Delphi.Praxis 和 StackOverflow 可能是目前最活跃的网站
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2022-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多