When I press Backspace
, Web-browser
will go to previous page. How can I disable it without preventing the clearing text ability?
Asked
Active
Viewed 1,765 times
2

Toon Krijthe
- 52,876
- 38
- 145
- 202

Kermia
- 4,171
- 13
- 64
- 105
-
3What about Alt+Left? What about right-clicking and selecting Back? What about the "Back" special key on the mouse or keyboard? – Andreas Rejbrand Jun 13 '11 at 11:48
-
1You probably want to implement some OnNavigating event and deny those navigations you don't like. But I'm not familiar with the details of the WebBrowser control. – CodesInChaos Jun 13 '11 at 12:03
-
@Andreas : I have blocked `Alt` and `Right-click`. – Kermia Jun 13 '11 at 12:15
-
1What about if the web-page has a "javascript:history.go(-1)" link? Do you have control of the web pages or not? – Stuart Jun 13 '11 at 13:02
-
Andreas... check my code. I got the special browser backwards/forwards keys as seen on many multimedia/microsoft-natural keyboards... – Warren P Jun 13 '11 at 18:38
2 Answers
3
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;
-
2note that this will not catch any javascript going back in the history. – Jeroen Wiert Pluimers Jun 13 '11 at 18:03
-
2Also you have to disable right click context menus because they contain a back command. – Warren P Jun 13 '11 at 18:35
1
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.

Toon Krijthe
- 52,876
- 38
- 145
- 202

Paktas
- 312
- 3
- 10