Solved

Recieving keypresses in a component that's not focused

Posted on 2004-10-15
2
172 Views
Last Modified: 2010-04-04
Hi, I have created my own XP-style button, wich descends from TCustomControl. I've got it working almost perfectly, except for the Default-property. I'm not really sure how to implement it, because I need to recieve keypresses to my button even when it is not focused to accomplish this... Thinking about maybe using Parent.OnKeyDown (which would mean the Form's KeyDown-event), but it doesn't seem like the right way to go... Is there an easier/better way?
0
Comment
Question by:mmmMange
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12326260
I think you need to use the form's keypreview facility.  Quoted from Delphi help:-

"If KeyPreview is True, keyboard events occur on the form before they occur on the active control. (The active control is specified by the ActiveControl property.)

If KeyPreview is False, keyboard events occur only on the active control.

Navigation keys (Tab, BackTab, the arrow keys, and so on) are unaffected by KeyPreview because they do not generate keyboard events.

KeyPreview is False by default."

Even though your button is not the active control, you can still pre process all keypresses to see whether they are relevant to your control before allowing them to be passed on to other controls on the form.
0
 
LVL 6

Accepted Solution

by:
vadim_ti earned 250 total points
ID: 12326358
i think you must make something like this (from tbutton source)
procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;


procedure TYourButton.CMDialogKey(var Message: TCMDialogKey);
begin
  with Message do
    if  (((CharCode = VK_RETURN) and FActive) or
      ((CharCode = VK_ESCAPE) and FCancel)) and
      (KeyDataToShiftState(Message.KeyData) = []) and CanFocus then
    begin
      Click;
      Result := 1;
    end else
      inherited;
end;

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question