Solved

ShortCut problem

Posted on 2000-02-14
6
181 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:petershaw9
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
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
 

Author Comment

by:petershaw9
Comment Utility
Yes, The shortcut get the key before the locate method. so what can we do?

PeterShaw
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 17

Expert Comment

by:inthe
Comment Utility
hi,
not sure if it will help but have you tried changing the forms keypreview setting in object inspector?
0
 

Author Comment

by:petershaw9
Comment Utility
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
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

12 Experts available now in Live!

Get 1:1 Help Now