0

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

Behdadsoft
  • 77
  • 9
  • `TWebBrowser` is just a wrapper for [WebBrowser Class](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser?view=windowsdesktop-6.0) therefore it only creates the new window with the use of exposed WinAPI calls. But in the end all rendering and input calls are handled by the underlying API and not in your Delphi application. So I don't thing there is an easy solution for what you want to achieve. – SilverWarior May 25 '22 at 01:27
  • There is possibility to [Disable Text Selection Highlighting In HTML Using CSS](https://www.arungudelli.com/tutorial/css/disable-text-selection-in-html-using-user-select-css-property/) which might be a possibility for you if you are displaying your own web content in `TWebBrowser` component and thus have full control over it. – SilverWarior May 25 '22 at 01:30
  • "_copying information inside_" is also available using `Ctrl`+`Ins` or right clicking with mouse and selecting the appropriate context menu item. Or executing that in JS. Disabling the keyboard entirely kills it for handicapped users and potential logins/searches, where you **need** to type keys. – AmigoJack May 25 '22 at 06:54

2 Answers2

1

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

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
0

@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;

Behdadsoft
  • 77
  • 9
  • Doesn't this prevent way too much? Now you can't paste anymore, and with pretty much every non US keyboard layout you're unable to use legal shorcuts such as **Alt Gr+M** to get `µ`. Additionally content can still be recorded simply through a screenshot (**Print** key). – AmigoJack Jul 25 '22 at 01:24
  • Yes, But print key it's not matter for me now. – Behdadsoft Aug 01 '22 at 10:25