Hi,

My embedded browser app loads a page which contains two frames. The
first frame has a function to process key event while the second frame
has
onkeydown handler in the BODY tag. The handler is refering to the
function
in the first frame. Now, the key event is forged from my application
instead
of from keyboard. So far, I tried three means:

case 1: from webbrowser, I got base window handle and sent the message
to it.

case 2: from browser, I got contentDOMwindow and DOMwindowcollection of
frames and got the expected frame by its name. I also got PresContext
and
scriptglobalobject and call on its handleDOMEvent.

case 3: I tried to go through getfocusedwindow interface.

However, none of them is working. I mean that either get null pointers
back or the second frame never gets the forged key event. I believe
there must be a gentle way to get around the problem. Any help will be
highly appreciated.

I give the code segaments case by case as the followings:

case 1:
    nsresult rv;
    nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(webBrowser));
    nsCOMPtr<nsIWidget> mainWidget;
    rv = baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
    if (!NS_FAILED(rv))
    {
     HWND hwnd = (HWND)mainWidget->GetNativeData(NS_NATIVE_WINDOW);
     hwnd = ::GetWindow(hwnd, GW_CHILD);
     ::PostMessage(hwnd, WM_KEYDOWN, wParam, 1);
    }

case 2:
    nsCOMPtr<nsIDOMWindow> aActiveWindow;
    nsresult rv =
webBrowser->GetContentDOMWindow(getter_AddRefs(aActiveWindow));
    if (!NS_FAILED(rv))
    {
     nsCOMPtr<nsIDOMWindowCollection> frames;
     rv = aActiveWindow->GetFrames(getter_AddRefs(frames));
     if(!NS_FAILED(rv))
     {
      nsCOMPtr<nsIDOMWindow> aWindow;
      PRUint32 aLength;
      rv = frames->GetLength(&aLength);
      rv = frames->NamedItem((const nsAReadableString&)aName,
getter_AddRefs(aWindow));

      if(!NS_FAILED(rv))
      {
       nsCOMPtr<nsIDOMDocument> domDocument;
       rv = aWindow->GetDocument(getter_AddRefs(domDocument));
          nsCOMPtr<nsIDocument> aDocument =
do_QueryInterface(domDocument);
       nsCOMPtr<nsIPrincipal> docPrincipal;
       rv =  aDocument->GetPrincipal(getter_AddRefs(docPrincipal));
       if (NS_FAILED(rv))
       {
        printf("couldn't get principal\n");
        return TRUE;
       }

       nsCOMPtr<nsIScriptGlobalObject> scriptGlobalObject;
       rv =
aDocument->GetScriptGlobalObject(getter_AddRefs(scriptGlobalObject));
       nsCOMPtr<nsIDocShell> aDocShell;
       rv = scriptGlobalObject->GetDocShell(getter_AddRefs(aDocShell));
       nsCOMPtr<nsIPresContext> aPresContext;
       rv = aDocShell->GetPresContext(getter_AddRefs(aPresContext));
       struct nsKeyEvent akeyEvent;
       akeyEvent.keyCode = wParam;
          akeyEvent.charCode = 0;
       akeyEvent.isChar = 1;
       nsEventStatus status = nsEventStatus_eConsumeDoDefault;
       rv = scriptGlobalObject->HandleDOMEvent( aPresContext.get(),

&akeyEvent,
                                                                nsnull,


NS_EVENT_FLAG_CAPTURE,

&status);

case 3:
    nsresult rv;
    nsCOMPtr<nsIDOMWindowInternal> focusedWindow;

    nsCOMPtr<nsIDOMWindow> domWindowExternal;
    rv =
webBrowser->GetContentDOMWindow(getter_AddRefs(domWindowExternal));
    if (NS_FAILED(rv)) return rv;
    nsCOMPtr<nsPIDOMWindow> piWin(do_QueryInterface(domWindowExternal,
&rv));
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIFocusController> focusController;
    piWin->GetRootFocusController(getter_AddRefs(focusController));
    if (focusController)
      rv =
focusController->GetFocusedWindow(getter_AddRefs(focusedWindow));

    nsCOMPtr<nsIWidget> aWindow = do_QueryInterface(focusedWindow);
//it asserts here because focusedWindow is never retrieved successfully.

    HWND hwnd = (HWND)aWindow->GetNativeData(NS_NATIVE_WINDOW);
    ::SendMessage(hwnd, WM_KEYDOWN, wParam, 1);


Reply via email to