TMenuItem-快捷方式会覆盖控件的快捷方式(TMemo) [英] TMenuItem-Shortcuts overwrite Shortcuts from Controls (TMemo)

查看:124
本文介绍了TMenuItem-快捷方式会覆盖控件的快捷方式(TMemo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

菜单项的快捷键不会覆盖本地控件的快捷键,该怎么办?
想象一下屏幕截图中的这个简单应用程序。它具有一个撤消菜单项,并为其分配了快捷键CTRL + Z(德语为Strg + Z)。当我在备忘录中编辑一些文本并按CTRL + Z时,我假定备忘录中的最后一个输入已还原,但执行菜单项。

What can I do that shortcuts for menu items don't overwrite those from local controls? Imagine this simple app in the screenshot. It has one "undo" menu item with the shortcut CTRL+Z (Strg+Z in German) assigned to it. When I edit some text in the memo and press CTRL+Z I assume that the last input in the memo is reverted, but instead the menu item is executed.

在这个虚构的应用程序中,这尤其糟糕,因为撤消功能现在将删除我上次编辑的属性 Item 3。

This is especially bad in this fictional application because the undo function will now delete my last added "Item 3" which properties I was editing.

CTRL + Z只是一个例子。其他流行的快捷方式也会引起类似的问题(复制和粘贴:CTRL + X / C / V,全选:CTRL + A)。

CTRL+Z is just an example. Other popular shortcuts cause similar problems (Copy&Paste: CTRL+X/C/V, Select all: CTRL+A).

带有菜单项的迷你演示,该菜单项带有CTRL + Z快捷键http://img31.imageshack.us/img31/9074/ctrlz问题。 png

推荐答案

VCL旨在为菜单项快捷方式赋予优先级。但是,您可以编写您的项目单击处理程序(或动作执行处理程序),以便在ActiveControl为TCustomEdit(调用Undo等)时进行一些特殊处理。

The VCL is designed to give menu item shortcuts precedence. You can, however, write your item click handler (or action execute handler) to do some special handling when ActiveControl is TCustomEdit (call Undo, etc.)

Edit:I了解您不喜欢在代码的许多位置(所有菜单项或操作处理程序)处理所有可能的特殊情况。恐怕无法给您一个完全令人满意的答案,但这也许会帮助您找到更通用的解决方案。在您的窗体上尝试以下OnShortCut事件处理程序:

I understand you don't like handling all possible special cases in many places in your code (all menu item or action handlers). I'm afraid I can't give you a completely satisfactory answer but perhaps this will help you find a bit more generic solution. Try the following OnShortCut event handler on your form:

procedure TMyForm.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
var
  Message: TMessage absolute Msg;
  Shift: TShiftState;
begin
  Handled := False;
  if ActiveControl is TCustomEdit then
  begin
    Shift := KeyDataToShiftState(Msg.KeyData);
    // add more cases if needed
    Handled := (Shift = [ssCtrl]) and (Msg.CharCode in [Ord('C'), Ord('X'), Ord('V'), Ord('Z')]);
    if Handled then
      TCustomEdit(ActiveControl).DefaultHandler(Message);
  end
  else if ActiveControl is ... then ... // add more cases as needed
end;

您还可以类似的方式重写IsShortCut方法,并从这个新的TCustomForm后代派生项目的表单。

You could also override IsShortCut method in a similar way and derive your project's forms from this new TCustomForm descendant.

这篇关于TMenuItem-快捷方式会覆盖控件的快捷方式(TMemo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆