ShortCut problem

In a form, there are a dbgrid1, a btnOK, a btnClose. The btnOK's caption is &OK, btnClose's caption is &Close. The btnOK onClick does something, and the btnClose OnClick close the form. For the dbGrid1, I set the dgRowselect := true, and I write a code for DBGrid1KeyPress. in the DBGrid1KeyPress, I use dataset.Locate. so when I press a key, It locates the first matched record.
The problem is : when dbgrid is focused, and  I press the 'c', it triggers btnClose OnClick event and closes the form, if I press the 'O', then it triggers the btnOK OnClick event.
I know if I set dbgrid1's dgRowSelect := False, It will not have this problem. But I  really want dgRowSelect := True. So how can I solve the problem?
Thanks

PeterShaw9
petershaw9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
You could try setting Key to #0 like this:

// Do your lookup stuff here
   Key := #0; // Swallow the key stroke to stop shortcuts...

But the shortcuts MAY get the key before your DB lookup code does.

Cheers,

Raymond.
0
petershaw9Author Commented:
Yes, The shortcut get the key before the locate method. so what can we do?

PeterShaw
0
rwilson032697Commented:
Another simple way of doing this would be to Put a TApplicationEvents component on your form (from the additional tab) the writing a OnShortCut event handler. In the handler you can determine if the DBGrid is the active control and if so call the keypress event and then set handled to true to block further processing of the key.

Cheers,

Raymond.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

intheCommented:
hi,
not sure if it will help but have you tried changing the forms keypreview setting in object inspector?
0
petershaw9Author Commented:
I test KeyPreview, it doesn't work, because it comes after the shortcut.

I tried RayMond way, it works. I wrote the code like these in the application.onshortcut. but it has a minus defect: if I press the 'Alt' key, then releaseed then press 'c', the form is still be closed.

My code:
const cAlt: Boolean = False;
var wCh : word;
begin
  HourGlassCursor;
  if Msg.CharCode = VK_Menu then
  begin
    cAlt := True;
    ArrowCursor;
    Exit;
  end;

  wCh := Msg.CharCode;

  if dbgrid1.Focused and (not cAlt) and (wCh > 41) and (wCh < 127) then
  begin
    if Msg.CharCode = VK_Escape then
    begin
        Handled := False;
        cAlt := False;
        ArrowCursor;
        exit;
    end;

    dmSupport.oeQryOrgHead.Locate('OH_SHORT', Char(wCh), [loCaseInsensitive, loPartialKey]);
    Handled := True;
  end;

  cAlt := false;
  ArrowCursor;
end;


peterShaw9
0
rwilson032697Commented:
Hi Peter,

From the Delphi help on TWMKey:

The KeyData field indicates the repeat count, scan code, extended-key flag, context code, and so on. See the Windows help system for a keystroke message such as WM_KEYDOWN for details on the possible values.

You can use the keydata field to determine if the alt key is held down...

Cheers,

Raymond.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.