Solved

OnKeyDown in Delphi 5 and 6 Pro isn't working on my system

Posted on 2001-07-21
6
428 Views
Last Modified: 2010-04-04
I created a form. Set its style to fsMDIForm.
I set the keypreview property to true;

I placed a Statusbar on the form with 6 panels (CAP, Capstatus, NumbLock, NumbLockStatus, Insert, InsStatus)

I placed a ActionList on the form and created a custom action item (Action1). This item will check the status of the keyboard and will change the CapStatus, NumbLockStatus, and InsStatus to either on or off.

When the form is created Action1 is executed and everything works as it should.

But...When I press on the Keyboard key Caps Lock....nothing happens. The OnKeyDown event is Action1.Execute;

I have also tried this code in the OnKeyDown Event just to see if any code will work:

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (shift = ([ssalt])) then showMessage('This Works');
end;

Still...Nothing.

Any ideas?

BTW - If I put a timer component on the form and have it fire Action1.Execute (every 1000 ms) it picks up the change to the keyboard state when I change it.




I have tried this in both D5 and D6.
0
Comment
Question by:gemarti
  • 3
  • 2
6 Comments
 

Accepted Solution

by:
felonious earned 50 total points
ID: 6304893
I think the problem is that you set your form as fsMDIForm.  I am currently also working on an MDI project and when I tried the mouse down even it didn't even trigger for just the MainForm.  Now if i go and open a MDIChild everything works great.  I know this doesn't help you, but it seems to be the standard behaviour of fsMDIForms.  I'll see if I can figure out a work around, but it seems to me that a nasty work around is that you could use the above code (and it would work when there is at least one MDIChild open) and also put in a timer to check for changes.
0
 
LVL 3

Expert Comment

by:nnbbb09
ID: 6304901

I think is has something to do with it being an MDI app. If the focused control is in the MDI parent then the OnKeyDown event for the MDI child won't fire. Try setting focus to a control in the MDI form and then testing it.
Let me know what happens.

Jo
0
 
LVL 3

Expert Comment

by:nnbbb09
ID: 6304932

I think is has something to do with it being an MDI app. If the focused control is in the MDI parent then the OnKeyDown event for the MDI child won't fire. Try setting focus to a control in the MDI form and then testing it.
Let me know what happens.

Jo
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:nnbbb09
ID: 6304953

Sorry about the double post. I've also re-read your question and realised that the problem is the other way around to what I had originally thought. ie : The MDI Parent has the OnKeyDown event but the MDIChild has focus.
One possible workaround (not elegant but it works) is to declare a class of TCustomForm in your MDI child. You can then typecast Application.MainForm to this class and gain access to the protected methods, one of which is KeyDown.
eg :

type
  TRedeclareForm = class(TCustomForm);

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  TRedeclareForm(Application.MainForm).KeyDown(Key,Shift);
end;

Jo
0
 
LVL 21

Author Comment

by:gemarti
ID: 6304955
I'm going to accept both your answer and nbbb09's answer.

I created a child form.
I put a command in the Mainform.OnCreate to set focus on the StatusBar1.
I put a command in the OnMouseMove event to trigger Action1 and the status bar started working .

Thanks for both of your help.

0
 

Expert Comment

by:felonious
ID: 6304970
Thanks, gemarti, good luck!

-felonious
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

9 Experts available now in Live!

Get 1:1 Help Now