views:

368

answers:

3

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;
A: 

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.

There might be a simpler solution. Have you tried setting the form's KeyPreview property to True?

Wim Coenen
Yes, that doesn't work.
Steve
A: 

Well, both forms are signed up to receive the keyboard notice, so they both close. You need to put code in there to decide "is this ESC for me?". Maybe by determining if you're the window with focus or not. If it's not your ESCape, then don't close.

But, this all seems rather drastic. There must be a simpler, non-obtrusive way to detect the ESC within THIS APP, without having to monitor the keyboard for the whole system.

Chris Thornton
Hello! That's why I'm using GetForegroundWindow (see code), but it seems that sometimes both windows receive the keystrokes. (but not always, and this is what's driving me crazy!) Once I process the keystroke, the other window shouldn't be getting it. Why is this happening? Note: I have Googled for hours but didn't find better solutions other than using a hook...
Steve
@Steve - perhaps I'm mis-reading your code, but to me, it looks like you check only looks to see if you're looking at ANY TEmailDetailsForm, not THIS TEmailDetailsForm. I also don't get why you have that code in there, and also in the CheckInstance function. Seems redundant, and incorrect. Actually, that's probably one of your problems. The CheckInstance in the FormCreate seems like it'll prevent THIS instance from hooking the keyboard. Why do that? Shouldn't each instance handle their own processing?
Chris Thornton
Hello Chris. As I understand KeyboardHookProc has to be in the unit, not inside the class. (it didn't work otherwise) Thus, I have to use Global variables (KeyboardHook: HHook) but then each instance of the class would overwrite the same global variable in FormCreate, creating two keyboard hooks. Am I being wrong here?
Steve
+1  A: 

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.

Craig Peterson
Thank you, this seems to be working well!
Steve