Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 707709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:18:32+00:00 2026-05-14T04:18:32+00:00

The background: My form has a TWebBrowser. I want to close the form with

  • 0

The background: My form has a TWebBrowser. I want to close the form with ESC but the TWebBrowser eats the keystrokes – so I decided to go with a keyboard hook.

The problem is that the Form can be open in multiple instances at the same time.

No matter what I do, in some situations, if there are two instances open of my form, closing one of them closes the other as well.

I’ve attached some sample code. Any ideas on what causes the issue?

var
  EmailDetailsForm: TEmailDetailsForm;
  KeyboardHook: HHook;

implementation

function KeyboardHookProc(Code: Integer; wParam, lParam: LongInt): LongInt; stdcall;
var
  hWnd: THandle;
  I: Integer;
  F: TForm;
begin
  if Code < 0 then
    Result := CallNextHookEx(KeyboardHook, Code, wParam, lParam)
  else begin
    case wParam of
      VK_ESCAPE:  
        if (lParam and $80000000) <> $00000000 then
        begin
          hWnd := GetForegroundWindow;
          for I := 0 to Screen.FormCount - 1 do
          begin
            F := Screen.Forms[I];
            if F.Handle = hWnd then
              if F is TEmailDetailsForm then
              begin
                PostMessage(hWnd, WM_CLOSE, 0, 0);
                Result := HC_SKIP;
                break;
              end;
          end; //for
        end; //if
      else
        Result := CallNextHookEx(KeyboardHook, Code, wParam, lParam);
    end;  //case
  end;  //if
end;

function TEmailDetailsForm.CheckInstance: Boolean;
var
  I, J: Integer;
  F: TForm;
begin
  Result := false;

  J := 0;

  for I := 0 to Screen.FormCount - 1 do
  begin
    F := Screen.Forms[I];
    if F is TEmailDetailsForm then
    begin
      J := J + 1;
      if J = 2 then
      begin
        Result := true;
        break;
      end;
    end;
  end;
end;

procedure TEmailDetailsForm.FormCreate(Sender: TObject);
begin
    if not CheckInstance then    
      KeyboardHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, 0, GetCurrentThreadId());
end;

procedure TEmailDetailsForm.FormDestroy(Sender: TObject);
begin
    if not CheckInstance then
      UnHookWindowsHookEx(KeyboardHook);
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:18:33+00:00Added an answer on May 14, 2026 at 4:18 am

    You could do this with TApplicationEvents.OnMessage instead. Drop a TApplicationEvents component on your application’s main form with this code:

    procedure TMainForm.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    var
      C: TControl;
      H: HWND;
    begin
      if (Msg.message = WM_KEYDOWN) and (Msg.wParam = VK_ESCAPE) then begin
        H := Msg.hwnd;
        while GetParent(H) <> 0 do
          H := GetParent(H);
        C := FindControl(H);
        if C is TEmailDetailsForm then begin
          TEmailDetailsForm(C).Close;
          Handled := True;
        end;
      end;
    end;
    

    If you want to keep using a keyboard hook instead, you should only hook it once, rather than once for each form, especially since you’re overwriting a global variable. Try adding a HookCount global variable, and only hook/unhook if it’s the only form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: This page is a form that has a table to select permisson for
I'm working on creating an HTML form that has an image in the background.
I'm trying to make a button for my form which has an image background
Background, Part 1 I have a form that collects both frequency and duration from
I have a VB.NET form with a red background and white text. I want
I am trying to make an input form have a background image, but as
Form 1 has a transparency key for custom title bar with a background image.
I am working on a drupal site that has a form submit ajax call
I created a gradient for my form background but Im having this issue now.
I have an order entry form that has a ListBox with a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.