当TEdit处于焦点状态时按Escape键,如何避免发出叮声? [英] How to avoid the ding sound when Escape is pressed while a TEdit is focused?

查看:61
本文介绍了当TEdit处于焦点状态时按Escape键,如何避免发出叮声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几年前开发的代码中,我经常使用此命令随时关闭Escape键以关闭当前表单:

In code I have developed some years ago I have been using this a lot to close the current form on pressing the Escape key at any moment:

procedure TSomeForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
    if key = #27 then close;
end;

此行为是为TForm定义的.表单的 KeyPreview 属性设置为 True ,以使表单对按键的反应比其他任何组件都要早.对于程序的最佳部分,这一切都很好地工作,但是,当TEdit组件被聚焦时按下Escape键时,会发出声音(Windows用来表示无效操作的 ding 声音).它仍然可以正常工作,但是我从来没有设法摆脱这种声音.

This behaviour is defined for the TForm. The form's KeyPreview property is be set to True to let the form react to key presses before any other components. It all works perfectly well for the best part of the program, however, when the Escape key is pressed while a TEdit component is focused a sound (a ding sound used by Windows to signify invalid operation) is issued. It still works fine but I have never quite managed to get rid of the sound.

这是什么问题?

重新创建步骤:

  • 新的VCL表单应用程序,将表单的KeyPreview设置为true
  • 在事件"选项卡上,双击onKeyPress事件并输入虚拟代码:

  • new VCL Forms application, set the form's KeyPreview to true
  • on the Events tab double-click the onKeyPress event and enter dummy code:

如果key =#27然后;

if key=#27 then ;

在窗体中添加一个TListBox,TCheckBox,TEdit并运行该应用程序

add a TListBox, TCheckBox, TEdit to the form and run the application

推荐答案

由于在输入中保留了ESC,因此得到了提示.看看Key是 var 的方式吗?将其设置为#0,即可消除叮声.这将其从进一步处理中删除.

You get the ding because you left the ESC in the input. See how Key is a var? Set it to #0 and you eliminate the ding. That removes it from further processing.

procedure TSomeForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
    if key = #27 then 
    begin
      key := #0;
      close;
    end;
end;

KeyPreview就是这样,除非您停止它,否则将传递给控件的预览.

KeyPreview is just that, a preview of what will be passed to the controls unless you stop it.

这篇关于当TEdit处于焦点状态时按Escape键,如何避免发出叮声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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