Delphi的KeyPreview如何工作? [英] How does Delphi's KeyPreview work?

查看:189
本文介绍了Delphi的KeyPreview如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个同事和我自己正在辩论,哪个方式对系统资源的负担不大。 (注意:这不是我想要的答案,而是以粗体表示的标题和下面的行是我寻求答案的问题。)


  1。使用KeyPreview获取表单上的按键。 



  2。使用定义OnMessage过程并处理它。 


乍看之下,KeyPreview似乎不再是系统负担因为定义一个OnMessage过程会导致我们的程序检查每个发送的消息。我们不关心的注释消息将使它由第一个if语句跳出。这是最坏的情况,我们将为每条消息执行一个if语句。



但是我们想知道Delphi如何处理KeyPreview属性...我们想知道如果Delphi内部定义了自己的OnMessage并查看消息,然后触发与按键相关的事件。



如果是这样的话方法大致相同?

解决方案

KeyPreview功能:



当前活动控件的KeyUp-,KeyDown-和KeyPress-事件处理程序检查当前活动窗体的KeyPreview属性。即:任何控件中的按键都可以检查表单的KeyPreview属性。



如果该属性为True,则有问题的事件处理程序会调用该事件处理程序在本身之前的形式。如果表单的事件处理程序没有将键值更改为0(或#0,取决于KeyPress或KeyDown / KeyUp),那么活动控件的事件处理程序将重新接管,否则该事件被视为已处理。



与Application.OnMessage相比:



所以在表单的事件处理程序中将键值设置为0 /#0是设置Application.OnMessage的处理参数。在这方面,实际上没有区别。但是由于OnMessage在调度过程中很早就被调用,所以在性能上有一个理论上的增益,因为消息没有进一步调度。当您将处理为假时,根本没有区别,因为KeyPreview属性始终被检查,无论是否设置。



主要区别在于您必须将állForms的KeyPreview设置为True,然后为每个表单实施和维护相应的事件处理程序。比较一下只有一个事件处理程序为Application.OnMessage。那就是:假设你可以用一个例程来完成所有的表格。


A colleague and myself were debating over which way was less of a burden on the system resources. (Note: this is not the question I want an answer to. Rather the title and the line below in bold is the question I seek an answer for.)

1. Using KeyPreview to get the keypresses on a form.

or

2. Using defining an OnMessage procedure and handling it there.

At first glance it seems KeyPreview would be less of a system burden since defining an OnMessage procedure results in our program checking every message that comes in. Note messages we don't care about would cause it to jump out by the first if statement. That is at worst we would have an if statement executed for every message.

But we're wondering how Delphi deals with the KeyPreview property... We wonder if Delphi internally defines its own OnMessage and looks at the messages to then trigger the events that are related to keypresses.

If this is the case then would both approaches be about the same?

解决方案

The KeyPreview functioning:

The KeyPreview property of the current active form is checked for the KeyUp-, KeyDown- and KeyPress- event handlers of the current active control. I.e.: a key press in any control results in the check of the form's KeyPreview property.

If that property is True, the event handler in question invokes the event handler of the form prior to that of itself. If the form's event handler does not change the key value to 0 (or #0, depending on KeyPress or KeyDown/KeyUp), then the active control's event handler takes back over, otherwise the event is considered handled.

Compared to Application.OnMessage:

So setting the key value to 0/#0 in a form's event handler is synonymous to setting the Handled parameter of Application.OnMessage. In this, there is virtually no difference. But since OnMessage is called very early in the dispatching process, there is a theoretical gain in performance because the message is not being dispatched any further. When you leave Handled to False, there is no difference at all, because the KeyPreview property is always checked, whether it is set or not.

The main difference that is left is that you have to set the KeyPreview of áll Forms to True, ánd implement and maintain for each of all forms the appropriate event handlers. Compare this to having just one event handler for Application.OnMessage. That is: assuming you could do with just one routine for all of your forms.

这篇关于Delphi的KeyPreview如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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