Solved

Recieving keypresses in a component that's not focused

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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