Solved

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

Posted on 2001-07-21
6
430 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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