Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2001-07-21
Medium Priority
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);
  if (shift = ([ssalt])) then showMessage('This Works');


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.
Question by:gemarti
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
  • 3
  • 2

Accepted Solution

felonious earned 150 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.

Expert Comment

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.


Expert Comment

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.

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

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 :

  TRedeclareForm = class(TCustomForm);

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);

LVL 21

Author Comment

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.


Expert Comment

ID: 6304970
Thanks, gemarti, good luck!


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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