2

First question. Help format it, if needed, please.

Context
I have a TWebBrowser in the main form that is used to behave like it was a printer.
So I load some HTML text in it as user do some commands in the real printer...
I want the user to be able to click and select text from the WebBrowser.

Problem
When the user clicks in the WebBrowser some of the shortcuts registered from the actions don't work anymore. For example, there is an action with shortcut F7. If the user clicks in the WebBrowser and presses F7, it does not invoke my shortcut.
I know that this is by design of the WebBrowser.

So, I thought: I want to send every key combination back to the form.
The question is: How?
If it was another control, I could use a perform(WM_KeyDown,...) in the OnKeyDown event.

Alternatives or suggestions would be appreciated too. I am very tired these past 2 days, so I could be missing something.

Johan
  • 74,508
  • 24
  • 191
  • 319
EMBarbosa
  • 1,473
  • 1
  • 22
  • 76
  • 3
    Maybe this will show you the way http://stackoverflow.com/questions/3446440/how-can-i-avoid-refresh-with-twebbrowser/3447422#3447422 –  Apr 26 '11 at 20:40
  • 3
    +1 @daemon_x using the `IHTMLDocument2::onkeydown` event (http://msdn.microsoft.com/en-us/library/aa752616%28VS.85%29.aspx) is the way to go. – RRUZ Apr 26 '11 at 20:47
  • @RRUZ - the questions are different, so repost (or slightly modify) your answer here to get more upvotes :) I gave you one there because I can't believe it's even possible to do this. And it works fine (I'll implement it very next day to one of my applications). Thanks –  Apr 26 '11 at 21:05
  • @daemon_x, if you can modify the original answer (http://stackoverflow.com/questions/3446440/how-can-i-avoid-refresh-with-twebbrowser/3447422#3447422) to fit with this question, feel free to elaborate a new answer ;) – RRUZ Apr 26 '11 at 21:14
  • @RRUZ @daemon_x, many thanks. I will take time to test it. Sorry I didn't found it before. I searched and read about more than 15 questions and answers about TWebBrowser, did not found that one. Thanks again. – EMBarbosa Apr 28 '11 at 15:04
  • @RRUZ @daemon_x Worked for me. Just a thing. As explained, in my case, I am the one that supply the webbrowser with HTML. So I used the onDocumentComplete to assign the event handler instead of onNavigateComplete2. Would any of you answer it here so I could accept the answer? Thanks. Also Thanks to @Johan who revised text of the question. I am pleased with that kind of corrections. :) – EMBarbosa May 03 '11 at 14:13

2 Answers2

4

Derivate TWebBrowser with an implementation of IDocHostUIHandler or use the famous EmbeddedWB

Implement the interface with an event OnTranslateAccelerator called in TranslateAccelerator

Set the event on your brwoser instance

Detect your key(s) like this:

function TBrowserPageIE.DoTranslateAccelerator(const lpMsg: PMSG; const pguidCmdGroup: PGUID; const nCmdID: DWORD): HRESULT;
begin
  result := S_FALSE;

  if lpMsg.message = WM_KEYDOWN then begin
    if lpMsg.wParam = VK_F7 then begin
      // do something here...
      result := S_OK;
    end;
  end;
end;
Matthias Alleweldt
  • 2,453
  • 17
  • 16
  • Hi Matthias. I will take the time to test that way. Thanks. I am avoiding EmbeddedWB for some reasons. I never worked with it, so I gone to forum to see what about it. I found that not many of the questions are being answered there. As it is open-source, I could take the code myself and work on it. But I am involved with others open-sources components and have not much time this exactly now. So I fear take some responsibility that I will not be able to accomplish. For example, some people are saying that it is not IE9 compatible... – EMBarbosa Apr 28 '11 at 14:52
  • Can I accept more than one answer? Cause I am using @daemon_x @RRUZ suggestion. – EMBarbosa May 04 '11 at 14:32
1

An option that I tested and worked is to trap the key_code on HTML/javascript and then send that to the form using changing the document title. I will let it here hoping that help someone...

You will need add the javascript to trap the keys in the Header of the HTML page like this:

<script = ''javascript''>
  function keypresed() {
    var tecla=window.event.keyCode;

    document.title = "Command"+tecla;
    event.keyCode=0;
    event.returnValue=false;
  }
  document.onkeydown=keypresed;
</script>

Then in Webbrowser you use the onTitleChangeEvent to use the key.

var
 s:string;
begin
  if Copy(Text,0,7) = 'Command' then
  begin
    //get the key...
    s:= Copy(Text,8,Length(Text));

    // if before the webbrowser get the focus edit1 was the focused control, you will need remove that focus first...
    dummy.setfocus; 
    edit1.setfocus;

    //perform keydown
    keybd_event(StrToInt(s), 1,0,0)
  end;
end;

Well, this can be used to perform any other custom command. :)

EMBarbosa
  • 1,473
  • 1
  • 22
  • 76
  • A very good solution and 'trick'. In my case, I needed VK_F11 and VK_ESCAPE for Fullscreen display and Fullscreen exit. – Peter Aug 19 '15 at 07:05