Disable keyboard on TWebBrowser in Delphi

227 views Asked by At

I want Disable Keyboard for TWebBrowser and avoid copying information inside it using Ctrl+C. but I couldn't find any option for disable keyboard in TWebBrowser properties.

Is there a way to do this?

EDIT: I saw this solution but it doesn't work. Disable All Keypresses

2

There are 2 answers

2
Behdadsoft On BEST ANSWER

@Fabrizio

Thank you for your code. this code can not Disable Keyboard for TWebBrowser. For this problem I found a component called EmbeddedWB. It have options for disable context menu.

Now Compound Options with your code (with a little change) makes text copying completely disabled.

procedure TMainForm.ApplicationEventsMessage(var Msg: tagMSG;
 var Handled: Boolean);
begin
 if ((Msg.message=WM_RBUTTONDOWN) or (Msg.message=WM_RBUTTONUP) or 
    (Msg.message=WM_KEYDOWN) or (Msg.message=WM_KEYUP)) and 
    IsChild(WebBrowser.Handle,Msg.hwnd) then
    begin
     PopupMenu.Popup(Msg.pt.X,Msg.pt.Y);
     Handled:=true;
 end;

end;

0
Fabrizio On

You can do that at the application level, preventing some messages to be forwarded to the TWebBrowser component. For example by using a TApplicationEvents component and its OnMessage event handler:

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
begin
  if (
    //keyboard
    (Msg.message = WM_KEYDOWN)
    //right click, for avoid copy-paste from popupmenu
    (Msg.Message = WM_RBUTTONDOWN) or
    (Msg.Message = WM_RBUTTONDBLCLK) or
  ) then
  begin
    if IsChild(WebBrowser1.Handle, Msg.hwnd) then
    begin
      Handled := True;
    end;
  end;
end;

A cleaner solution could be to suppress such messages at the component level, but unforntunately I've never found a way to make that works with the TWebBrowser component