如何检测 Embarcadero C++Builder 中的 Ctrl+P 组合键

How to detect the Ctrl+P key combination in Embarcadero C++Builder

我正在使用 C++Builder 10.4.1。我想用Ctrl+P组合键打开PrintDialog,但是不知道怎么检测Ctrl+P组合键。

使用OnKeyDown event it returns 16 位扩展键码(所以你还有数字键盘箭头和其他东西)...

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
    {
//  Caption=Key; // this will print aktually pressed key code to caption
    if ((Key=='P')&&(Shift.Contains(ssCtrl)))
        Caption="print"; // here open your dialog
    }

因此对于键测试 Key 值,对于特殊键和按钮测试 Shift(您知道 shift、ctrl、alt、鼠标按钮)

还有:

void __fastcall TForm1::FormShortCut(TWMKey &Msg, bool &Handled)
    {
    static bool ctrl=false;
    if (Msg.CharCode=='P')
        {
        Caption="print2";
        Handled=true;
        }
    ctrl=(Msg.CharCode==VK_LCONTROL)||(Msg.CharCode==VK_RCONTROL);
    }

优先于其他 key/mouse 事件。但是,它只能识别我不喜欢的 TWMKey 代码,对于不受支持的组合键,您需要像我在上面的示例中那样进行破解...

如果您想要更多组合或跟踪按下了哪些键(移动控件...),那么您必须实现一个键映射 table,如下所示:

对其进行 TActionList or TActionManager onto your Form, and then add the standard TPrintDlg 操作。然后,您可以将操作的 ShortCut 属性 设置为 Ctrl+P,当按下该键盘快捷键时,VCL 将自动为您调用操作:

TPrintDlg is the standard action for displaying a print dialog.

Add TPrintDlg to an action list to add a print dialog to your application. Controls such as menu items and tool buttons linked to this action cause the application to display the print dialog (TPrintDialog) when invoked. Write an OnAccept event handler to perform the actual printing when the user clicks OK. You can read details about the user's selections in the dialog from the Dialog property.

有关详细信息,请参阅 Embarcadero 的文档:

Handling VCL Actions Using an Action List

Handling VCL Actions Using an Action Manager