如何在网络浏览器中禁用退格键

发布于 2024-11-14 23:03:19 字数 88 浏览 2 评论 0原文

当我按 Backspace 时,Web-browser 将转到上一页。如何在不阻止清除文本功能的情况下禁用它?

When I press Backspace, Web-browser will go to previous page. How can I disable it without preventing the clearing text ability?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尤怨 2024-11-21 23:03:19

TWebBrowser似乎没有OnKeydown事件,而且它还有很多其他问题。我通常会忘记它并转到 TMembeddedWB (嵌入式 Web 浏览器组件包),它具有更好的功能。

我编写了一些代码来尝试检测我们是否处于编辑控件中,并且仅有条件地阻止退格键,因为正如 Paktas 所说,简单地阻止 OnKey 事件会中断编辑
网页表单,使用TMembeddedWB,代码如下:

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  element: IHTMLElement;
  elementTagName: String;
begin
  // form contains field: EmbeddedWB1: TEmbeddedWB which is an improved version of TWebBrowser.
  element := EmbeddedWB1.GetActiveElement;
  if Assigned( element ) then
    begin
      elementTagName := element.tagName;
    end;

 if ( not SameText( elementTagName, 'INPUT' ) ) 
   and ( not SameText( elementTagName, 'TEXTAREA' ) ) then
  if    ((Key= VK_LEFT) and (Shift = [ssAlt]))
     or ((Key= VK_RIGHT) and (Shift = [ssAlt]))
     or (Key= VK_BROWSER_BACK)
     or (Key= VK_BROWSER_FORWARD)
     or(Key = VK_BACK) then
      begin
        Key := 0; { block backspace, but not in INPUT or TEXTAREA. }
        Exit;
      end;


end;

TWebBrowser doesn't seem to have an OnKeydown event, and it also has many other problems. I usually forget it and go to TEmbeddedWB (embedded web browser component pack), and it has better features.

I wrote some code to try to detect if we are in an edit control, and only conditionally block backspace keys, since as Paktas says, simply blocking the OnKey event would break editing in
web page forms, using TEmbeddedWB, the code looks like this:

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  element: IHTMLElement;
  elementTagName: String;
begin
  // form contains field: EmbeddedWB1: TEmbeddedWB which is an improved version of TWebBrowser.
  element := EmbeddedWB1.GetActiveElement;
  if Assigned( element ) then
    begin
      elementTagName := element.tagName;
    end;

 if ( not SameText( elementTagName, 'INPUT' ) ) 
   and ( not SameText( elementTagName, 'TEXTAREA' ) ) then
  if    ((Key= VK_LEFT) and (Shift = [ssAlt]))
     or ((Key= VK_RIGHT) and (Shift = [ssAlt]))
     or (Key= VK_BROWSER_BACK)
     or (Key= VK_BROWSER_FORWARD)
     or(Key = VK_BACK) then
      begin
        Key := 0; { block backspace, but not in INPUT or TEXTAREA. }
        Exit;
      end;


end;
花开半夏魅人心 2024-11-21 23:03:19

执行此操作的唯一方法是检查每次按键,如果检测到退格键,则忽略它。但是,您还需要监视您的任何输入是否具有焦点,以便用户仍然可以保留向后删除。

The only way for doing this is to check for each key press and if a backspace is detected ignore it. However you would also need to monitor whether any of your inputs are focused, so that users could stil preserve backwards deleting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文