Solved

Recieving keypresses in a component that's not focused

Posted on 2004-10-15
2
163 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
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now