Search in sources :

Example 1 with KeyboardEvent

use of com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent in project htmlunit by HtmlUnit.

the class HtmlElement method type.

/**
 * Simulates typing the specified character while this element has focus, returning the page contained
 * by this element's window after typing. Note that it may or may not be the same as the original page,
 * depending on the JavaScript event handlers, etc. Note also that for some elements, typing <tt>'\n'</tt>
 * submits the enclosed form.
 *
 * @param c the character you wish to simulate typing
 * @param lastType is this the last character to type
 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
 * @exception IOException if an IO error occurs
 */
private Page type(final char c, final boolean lastType) throws IOException {
    if (isDisabledElementAndDisabled()) {
        return getPage();
    }
    // make enclosing window the current one
    getPage().getWebClient().setCurrentWindow(getPage().getEnclosingWindow());
    final HtmlPage page = (HtmlPage) getPage();
    if (page.getFocusedElement() != this) {
        focus();
    }
    final boolean isShiftNeeded = KeyboardEvent.isShiftNeeded(c, shiftPressed_);
    final Event shiftDown;
    final ScriptResult shiftDownResult;
    if (isShiftNeeded) {
        shiftDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, KeyboardEvent.DOM_VK_SHIFT, true, ctrlPressed_, altPressed_);
        shiftDownResult = fireEvent(shiftDown);
    } else {
        shiftDown = null;
        shiftDownResult = null;
    }
    final Event keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
    final ScriptResult keyDownResult = fireEvent(keyDown);
    if (!keyDown.isAborted(keyDownResult)) {
        final Event keyPress = new KeyboardEvent(this, Event.TYPE_KEY_PRESS, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
        final ScriptResult keyPressResult = fireEvent(keyPress);
        if ((shiftDown == null || !shiftDown.isAborted(shiftDownResult)) && !keyPress.isAborted(keyPressResult)) {
            doType(c, lastType);
        }
    }
    final WebClient webClient = page.getWebClient();
    if (this instanceof HtmlTextInput || this instanceof HtmlTextArea || this instanceof HtmlTelInput || this instanceof HtmlNumberInput || this instanceof HtmlSearchInput || this instanceof HtmlPasswordInput) {
        fireEvent(new KeyboardEvent(this, Event.TYPE_INPUT, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_));
    }
    HtmlElement eventSource = this;
    if (!isAttachedToPage()) {
        final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
        if (browserVersion.hasFeature(HTMLELEMENT_DETACH_ACTIVE_TRIGGERS_NO_KEYUP_EVENT)) {
            eventSource = null;
        } else {
            eventSource = page.getBody();
        }
    }
    if (eventSource != null) {
        final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
        eventSource.fireEvent(keyUp);
        if (isShiftNeeded) {
            final Event shiftUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, KeyboardEvent.DOM_VK_SHIFT, false, ctrlPressed_, altPressed_);
            eventSource.fireEvent(shiftUp);
        }
    }
    final HtmlForm form = getEnclosingForm();
    if (form != null && c == '\n' && isSubmittableByEnter()) {
        final HtmlSubmitInput submit = form.getFirstByXPath(".//input[@type='submit']");
        if (submit != null) {
            return submit.click();
        }
        form.submit((SubmittableElement) this);
        webClient.getJavaScriptEngine().processPostponedActions();
    }
    return webClient.getCurrentWindow().getEnclosedPage();
}
Also used : KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) WebClient(com.gargoylesoftware.htmlunit.WebClient) ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Example 2 with KeyboardEvent

use of com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent in project htmlunit by HtmlUnit.

the class HtmlElement method type.

private Page type(final int keyCode, final boolean fireKeyDown, final boolean fireKeyPress, final boolean fireKeyUp, final boolean lastType) {
    if (isDisabledElementAndDisabled()) {
        return getPage();
    }
    final HtmlPage page = (HtmlPage) getPage();
    if (page.getFocusedElement() != this) {
        focus();
    }
    final Event keyDown;
    final ScriptResult keyDownResult;
    if (fireKeyDown) {
        keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        keyDownResult = fireEvent(keyDown);
    } else {
        keyDown = null;
        keyDownResult = null;
    }
    final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
    final Event keyPress;
    final ScriptResult keyPressResult;
    if (fireKeyPress && browserVersion.hasFeature(KEYBOARD_EVENT_SPECIAL_KEYPRESS)) {
        keyPress = new KeyboardEvent(this, Event.TYPE_KEY_PRESS, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        keyPressResult = fireEvent(keyPress);
    } else {
        keyPress = null;
        keyPressResult = null;
    }
    if (keyDown != null && !keyDown.isAborted(keyDownResult) && (keyPress == null || !keyPress.isAborted(keyPressResult))) {
        doType(keyCode, lastType);
    }
    if (this instanceof HtmlTextInput || this instanceof HtmlTextArea || this instanceof HtmlTelInput || this instanceof HtmlNumberInput || this instanceof HtmlSearchInput || this instanceof HtmlPasswordInput) {
        final Event input = new KeyboardEvent(this, Event.TYPE_INPUT, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        fireEvent(input);
    }
    if (fireKeyUp) {
        final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        fireEvent(keyUp);
    }
    // }
    return page.getWebClient().getCurrentWindow().getEnclosedPage();
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Aggregations

BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)2 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)2 KeyboardEvent (com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)1