nsEventStatus status = nsEventStatus_eIgnore; nsKeyEvent event; event.eventStructType = NS_KEY_EVENT; switch (aEventType) { case eKeyPress:  event.message = NS_KEY_PRESS; break; case eKeyDown:   event.message = NS_KEY_DOWN; break; default:         event.message = NS_KEY_UP; break; } aKeyEvent->PreventBubble(); aKeyEvent->PreventCapture(); aKeyEvent->PreventDefault(); content->HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status);
nsXULKeyListenerImpl::HandleEventUsingKeyset(nsIDOMElement* aKeysetElement, nsIDOMKeyEvent* aKeyEvent, eEventType aEventType,                                         nsIDOMXULDocument* aDocument, PRBool& aHandledFlag){  nsAutoString trueString = "true";  nsAutoString falseString = "false";#undef DEBUG_XUL_KEYS#ifdef DEBUG_XUL_KEYS  if (aEventType == eKeyPress)  {      PRUint32 charcode, keycode;    keyEvent->GetCharCode(&charcode);    keyEvent->GetKeyCode(&keycode);    printf("DoKey [%s]: key code 0x%d, char code '%c', ",           (aEventType == eKeyPress ? "KeyPress" : ""), keycode, charcode);    PRBool ismod;    keyEvent->GetShiftKey(&ismod);    if (ismod) printf("[Shift] ");    keyEvent->GetCtrlKey(&ismod);    if (ismod) printf("[Ctrl] ");    keyEvent->GetAltKey(&ismod);    if (ismod) printf("[Alt] ");    keyEvent->GetMetaKey(&ismod);    if (ismod) printf("[Meta] ");    printf("\n");  }#endif /* DEBUG_XUL_KEYS */  // Given the DOM node and Key Event  // Walk the node's children looking for 'key' types  // XXX Use the key-equivalent CSS3 property to obtain the  // appropriate modifier for this keyset.  // TODO. For now it's hardcoded.  // If the node isn't tagged disabled  // Compares the received key code to found 'key' types  // Executes command if found  // Marks event as consumed  nsCOMPtr<nsIDOMNode> keyNode;  aKeysetElement->GetFirstChild(getter_AddRefs(keyNode));  while (keyNode) {    nsCOMPtr<nsIDOMElement> keyElement(do_QueryInterface(keyNode));    if (!keyElement)      continue;    nsAutoString property;    keyElement->GetNodeName(property);    //printf("keyNodeType [%s] \n", keyNodeType.ToNewCString()); // this leaks    if (property.Equals("key")) {      //printf("onkeypress [%s] \n", cmdToExecute.ToNewCString()); // this leaks      do {        property = falseString;        keyElement->GetAttribute(nsAutoString("disabled"), property);        if (property == trueString) {          break;        }        PRUint32 theChar;                nsAutoString keyName; // This should be phased out for keycode and charcode        keyElement->GetAttribute(nsAutoString("key"), keyName);        if ( !keyName.IsEmpty() ) {            if (aEventType != eKeyPress)                break;            aKeyEvent->GetCharCode(&theChar);        }        //printf("Found key [%s] \n", keyName.ToNewCString()); // this leaks        PRBool   gotCharCode = PR_FALSE;        PRBool   gotKeyCode  = PR_FALSE;                if ( keyName.IsEmpty() )        {	        keyElement->GetAttribute(nsAutoString("charcode"), keyName);	        if(keyName.IsEmpty()) {	          keyElement->GetAttribute(nsAutoString("keycode"), keyName);	          if(keyName.IsEmpty()) {	            // HACK for temporary compatibility	            if(aEventType == eKeyPress)	              aKeyEvent->GetCharCode(&theChar);	            else	              aKeyEvent->GetKeyCode(&theChar);	          } else {	            // We want a keycode	            aKeyEvent->GetKeyCode(&theChar);	            gotKeyCode = PR_TRUE;	          }	        } else {	          // We want a charcode	          aKeyEvent->GetCharCode(&theChar);	          gotCharCode = PR_TRUE;	        }        }		if(keyName.IsEmpty())			break;        char tempChar[2];        tempChar[0] = theChar;        tempChar[1] = 0;        nsAutoString tempChar2 = tempChar;        //printf("compare key [%s] \n", tempChar2.ToNewCString()); // this leaks        // NOTE - convert theChar and keyName to upper        keyName.ToUpperCase();        tempChar2.ToUpperCase();        PRBool isMatching;        if(gotCharCode){            isMatching = IsMatchingCharCode(tempChar2, keyName);        } else if(gotKeyCode){          isMatching = IsMatchingKeyCode(theChar, keyName);        }        // HACK for backward compatibility        if(!gotCharCode && ! gotKeyCode){          isMatching = IsMatchingCharCode(tempChar2, keyName);        }        if (!isMatching) {          break;        }        // This is gross -- we're doing string compares        // every time we loop over this list!        // Modifiers in XUL files are tri-state --        //   true, false, and unspecified.        // If a modifier is unspecified, we don't check        // the status of that modifier (always match).        // Get the attribute for the "xulkey" modifier.        nsAutoString xproperty = "";        keyElement->GetAttribute(nsAutoString("xulkey"),                                 xproperty);        // Is the modifier key set in the event?        PRBool isModKey = PR_FALSE;                  // Check whether the shift key fails to match:        aKeyEvent->GetShiftKey(&isModKey);        property = "";        keyElement->GetAttribute(nsAutoString("shift"), property);        if ((property == trueString && !isModKey)            || (property == falseString && isModKey))            break;        // and also the xul key, if it's specified to be shift:        if (xulKeyShift == mXULKeyModifier &&            ((xproperty == trueString && !isModKey)             || (xproperty == falseString && isModKey)))            break;        // and the control key:        aKeyEvent->GetCtrlKey(&isModKey);            property = "";        keyElement->GetAttribute(nsAutoString("control"), property);        if ((property == trueString && !isModKey)          || (property == falseString && isModKey))          break;        // and if xul is control:        if (xulKeyControl == mXULKeyModifier &&            ((xproperty == trueString && !isModKey)             || (xproperty == falseString && isModKey)))            break;        // and the alt key        aKeyEvent->GetAltKey(&isModKey);        property = "";        keyElement->GetAttribute(nsAutoString("alt"), property);        if ((property == trueString && !isModKey)          || (property == falseString && isModKey))          break;        // and if xul is alt:        if (xulKeyAlt == mXULKeyModifier &&          ((xproperty == trueString && !isModKey)           || (xproperty == falseString && isModKey)))          break;        // and the meta key        aKeyEvent->GetMetaKey(&isModKey);        property = "";        keyElement->GetAttribute(nsAutoString("meta"), property);        if ((property == trueString && !isModKey)          || (property == falseString && isModKey))          break;        // and if xul is meta:        if (xulKeyMeta == mXULKeyModifier &&          ((xproperty == trueString && !isModKey)           || (xproperty == falseString && isModKey)))          break;        // We know we're handling this.        aHandledFlag = PR_TRUE;        // Get the cancel attribute.        nsAutoString cancelValue;        keyElement->GetAttribute(nsAutoString("cancel"), cancelValue);        if (cancelValue == "true") {          return NS_OK;        }        // Modifier tests passed so execute onclick command        nsAutoString cmdToExecute;        nsAutoString oncommand;        switch(aEventType) {          case eKeyPress:            keyElement->GetAttribute(nsAutoString("onkeypress"), cmdToExecute);#if defined(DEBUG_saari)            printf("onkeypress = %s\n",                               cmdToExecute.ToNewCString());#endif            keyElement->GetAttribute(nsAutoString("oncommand"), oncommand);#if defined(DEBUG_saari)            printf("oncommand = %s\n", oncommand.ToNewCString());#endif          break;          case eKeyDown:            keyElement->GetAttribute(nsAutoString("onkeydown"), cmdToExecute);          break;          case eKeyUp:            keyElement->GetAttribute(nsAutoString("onkeyup"), cmdToExecute);          break;        }        // This code executes in every presentation context in which this        // document is appearing.        nsCOMPtr<nsIDocument> document = do_QueryInterface(aDocument);        nsCOMPtr<nsIContent> content = do_QueryInterface(keyElement);        if (aDocument != mDOMDocument) {          nsCOMPtr<nsIScriptEventHandlerOwner> handlerOwner = do_QueryInterface(keyElement);          if (handlerOwner) {            nsAutoString eventStr;            switch(aEventType) {              case eKeyPress:                eventStr = "onkeypress";                break;              case eKeyDown:                eventStr = "onkeydown";                break;              case eKeyUp:                eventStr = "onkeyup";                break;            }            // Look for a compiled event handler on the key element itself.            nsCOMPtr<nsIAtom> eventName = getter_AddRefs(NS_NewAtom(eventStr));            void* handler = nsnull;            handlerOwner->GetCompiledEventHandler(eventName, &handler);            nsCOMPtr<nsIScriptGlobalObject> masterGlobalObject;            nsCOMPtr<nsIDocument> masterDoc = do_QueryInterface(mDOMDocument);            masterDoc->GetScriptGlobalObject(getter_AddRefs(masterGlobalObject));            nsCOMPtr<nsIScriptContext> masterContext;            masterGlobalObject->GetContext(getter_AddRefs(masterContext));            if (!handler) {              // It hasn't been compiled before.              nsCOMPtr<nsIScriptGlobalObject> globalObject;              document->GetScriptGlobalObject(getter_AddRefs(globalObject));              if (!globalObject) {                NS_NewScriptGlobalObject(getter_AddRefs(globalObject));                document->SetScriptGlobalObject(globalObject);              }              nsCOMPtr<nsIScriptContext> context;              globalObject->GetContext(getter_AddRefs(context));              if (!context) {                NS_CreateScriptContext(globalObject, getter_AddRefs(context));                globalObject->SetNewDocument(aDocument);              }              nsCOMPtr<nsIScriptObjectOwner> owner = do_QueryInterface(keyElement);              void* scriptObject;              owner->GetScriptObject(context, &scriptObject);              nsCOMPtr<nsIContent> keyContent = do_QueryInterface(keyElement);              nsAutoString value;              keyContent->GetAttribute(kNameSpaceID_None, eventName, value);              if (value != "") {                  if (handlerOwner) {                      handlerOwner->CompileEventHandler(context, scriptObject, eventName, value, &handler);                  }                  else {                      context->CompileEventHandler(scriptObject, eventName, value,                                                   PR_TRUE, &handler);                  }              }            }            if (handler) {              nsCOMPtr<nsIDOMElement> rootElement;              mDOMDocument->GetDocumentElement(getter_AddRefs(rootElement));              nsCOMPtr<nsIScriptObjectOwner> owner = do_QueryInterface(rootElement);              void* scriptObject;              owner->GetScriptObject(masterContext, &scriptObject);              masterContext->BindCompiledEventHandler(scriptObject, eventName, handler);              nsCOMPtr<nsIDOMEventListener> eventListener;              NS_NewJSEventListener(getter_AddRefs(eventListener), masterContext, owner);              eventListener->HandleEvent(aKeyEvent);              masterContext->BindCompiledEventHandler(scriptObject, eventName, nsnull);              return NS_OK;            }          }        }        PRInt32 count = document->GetNumberOfShells();        for (PRInt32 i = 0; i < count; i++) {          nsCOMPtr<nsIPresContext> aPresContext;          { /* scope the shell variable (prevents the PresShell               from outliving its ViewManager if the key event               happens to delete the window.) */            // Retrieve the context in which our DOM event will fire.            nsCOMPtr<nsIPresShell> shell = getter_AddRefs(document->GetShellAt(i));            shell->GetPresContext(getter_AddRefs(aPresContext));          }          // Handle the DOM event          nsEventStatus status = nsEventStatus_eIgnore;          nsKeyEvent event;          event.eventStructType = NS_KEY_EVENT;          switch (aEventType)          {            case eKeyPress:  event.message = NS_KEY_PRESS; break;            case eKeyDown:   event.message = NS_KEY_DOWN; break;            default:         event.message = NS_KEY_UP; break;          }          aKeyEvent->PreventBubble();          aKeyEvent->PreventCapture();          aKeyEvent->PreventDefault();          content->HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status);          nsresult ret = NS_ERROR_BASE;          if (aEventType == eKeyPress) {            // Also execute the oncommand handler on a key press.            // Execute the oncommand event handler.            nsEventStatus stat = nsEventStatus_eIgnore;            nsMouseEvent evt;            evt.eventStructType = NS_EVENT;            evt.message = NS_MENU_ACTION;            content->HandleDOMEvent(aPresContext, &evt, nsnull, NS_EVENT_FLAG_INIT, &stat);          }          // Ok, we got this far and handled the event, so don't continue scanning nodes          //printf("Keybind executed \n");          return ret;        } // end for (PRInt32 i = 0; i < count; i++)      } while (PR_FALSE); // do { ...    } // end if (keyNodeType.Equals("key"))    nsCOMPtr<nsIDOMNode> oldkeyNode(keyNode);    oldkeyNode->GetNextSibling(getter_AddRefs(keyNode));  } // end while(keynode)  return NS_OK;}