如何防止主窗体在另一个非模态窗体上的 TMemo 中捕获击键?
我有一个可以从主窗体打开非模式窗体的应用程序。非模态形式上有一个 TMemo。主窗体菜单使用“空格”作为其加速器字符之一。
当非模态表单打开并且备忘录具有焦点时,每次我尝试在非模态表单上的备忘录中输入空格时,“空格”快捷方式的主表单事件都会触发!
我尝试过将 MainForm.KeyPreview := false 打开,而另一个表单已打开但没有骰子。
有什么想法吗?
I have an app that opens a non-modal form from the main form. The non-modal form has a TMemo on it. The main form menu uses "space" as one of its accelerator characters.
When the non-modal form is open and the memo has focus, every time I try to enter a space into the memo on the non-modal form, the main form event for the "space" shortcut fires!
I have tried turning MainForm.KeyPreview := false while the other form is open but no dice.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当备忘录具有焦点时禁用主窗体上的菜单项,并在备忘录失去焦点时重新启用它。您可以通过
TMemo.OnEnter
和TMemo.OnExit
事件执行此操作。使用
Application.MainForm
和类型转换是为了防止在子表单中对表单变量名称进行硬编码。Disable the menu item on the main form while the memo has focus, and re-enable it when the memo loses it. You can do this from the
TMemo.OnEnter
andTMemo.OnExit
events.The use of
Application.MainForm
and the typecast are to prevent hard-coding in a form variable name in the child form.这可能是一个老话题,但我刚才遇到了同样的问题并寻找合适的解决方案。您的主题提出了,但没有提供我想要使用的解决方案。
我的问题是:我有一个带有很多快捷键(退格键、删除键等)的主窗体和带有编辑框的第二个窗体。编辑框没有任何关键操作,这些操作由主窗体快捷方式处理。
我的解决方案:将子窗体设置为 OnShortCut,它将在主窗体解释它们之前捕获快捷方式:
这对我来说很有效,子窗体捕获快捷方式并将它们作为常用关键消息处理。编辑框可以按预期使用。
This may be an old topic, but i had the same issue a moment ago and searched for a suitable solution. Your topic came up but not with a solution i would want to use.
My problem was: I have a main form with a lot of shortcuts (Backspace, Delete, etc) and a second form with an edit box. The edit box didn't get any key actions, which are handled by the main form shortcuts.
My solution: Set the child forms OnShortCut, wich will catch the shortcuts before they get interpreted by the main form with:
That did the trick for me, the child form catches the shortcuts and handles them as common key messages. The edit box can be used as intended.
如果您有许多菜单项或许多控件,则可能很难解决每个菜单项或控件的问题。
相反,您可以在主窗口的 FormActivate() 和 FormDeActivate() 方法中使用函数,以便以简单的方式清理和恢复所有快捷方式:
If you have many menu items, or many controls, could be very difficult to threat the problem for each of them.
Instead, you could use a function in FormActivate() and FormDeActivate() methods of main window in order to clean and restore all shortcuts in a easy and simple way: