Solved

ShortCut problem

Posted on 2000-02-14
6
183 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
ID: 2520577
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
ID: 2520763
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
ID: 2520832
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Expert Comment

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

Author Comment

by:petershaw9
ID: 2521143
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
ID: 2521166
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 141
Downloading email attachments 2 73
How to define IfThen functions in one common unit? 4 53
Delphi: Connect to running MS Outlook 4 85
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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