?
Solved

Recieving keypresses in a component that's not focused

Posted on 2004-10-15
2
Medium Priority
?
173 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 1000 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

Independent Software Vendors: 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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