Solved

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

Posted on 2001-07-21
6
427 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
Comment Utility
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
Comment Utility

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
Comment Utility

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:nnbbb09
Comment Utility

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
Comment Utility
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
Comment Utility
Thanks, gemarti, good luck!

-felonious
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

11 Experts available now in Live!

Get 1:1 Help Now