Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1364
  • Last Modified:

Key Events in an MDI Frame form

We have an MDI Application.  We'd like to trap key presses in the application when there are no children forms displayed/created.  In other words, when only the MDI Frame form is displayed, we'd like to track the key press events to start a function.

I've added handlers to the Key* events (KeyPress, KeyDown and KeyUp) on the Frame form (FormStyle fsMDIForm).  However, it never gets the events.  I'm presuming that, by creating the form as a MDI frame, windows sends the key events to the active child form.

Is there a way to, when there is no active child form, to get these events sent to the frame window?  I've tried to watch the HandleMessage procedure, but I wasn't having much luck.

Thanks
0
EdHillmann
Asked:
EdHillmann
  • 3
  • 3
1 Solution
 
hubdogCommented:
you can process the application.onmessage event by yourself like this
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  RxHook, AppEvnts;

type
  TForm1 = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    procedure ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
begin
  if (Msg.message=wm_keydown) and (mdichildcount=0)  then
  begin
    showmessage('keydown');
    handled:=true;
  end;
end;

end.

good luck
hubdog
0
 
EdHillmannAuthor Commented:
Hubdog...

That did the trick.  Here's my successful test.  This is with a form whose style is fsMDIForm....


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure MessageHandler(var Msg: TMsg; var Handled: Boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MessageHandler(var Msg: TMsg; var Handled: Boolean);
begin
  if (Msg.message = wm_keydown) and (mdichildcount = 0)  then begin
    case Msg.wParam of
      VK_F2: begin
        ShowMessage('F2 Pressed!');
        Handled := true;
      end;
      VK_F3: begin
        ShowMessage('F3 Pressed!');
        Handled := true;
      end;
      VK_TAB: begin
        ShowMessage('Tab Pressed!');
        Handled := true;
      end;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := MessageHandler;
end;

end.


Thanks!!
0
 
sevenofnineCommented:
Did either of you ever get any problems using this?  Once I added it to my app, I started getting errors like 'A WIN32 API function failed'?  Any suggestions?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
EdHillmannAuthor Commented:
Weird.  I didn't get any errors like that.  I'll clarify that I'm using D5, although I don't think there's anything specific to D5 in that.

Maybe first comment out the ShowMessage calls to see if that causes it.  Just comment out the code and run, incrementally adding code back in to find the culprit?

Did you try this example, cause there's not much in it.  Might be easier to find the nasty line.
0
 
sevenofnineCommented:
Yeah, I tried the code.  The problem is whenever I try to create more than one or two MDI child forms - it's like there's a memory leak?
0
 
sevenofnineCommented:
Just rebooted my PC and the problem has mysteriously gone away.  Sorry to bother you!
0
 
EdHillmannAuthor Commented:
Aahh, Windows.  When all else fails, reboot!  Have fun!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now