Example usage for com.google.gwt.dom.client NativeEvent getCharCode

List of usage examples for com.google.gwt.dom.client NativeEvent getCharCode

Introduction

In this page you can find the example usage for com.google.gwt.dom.client NativeEvent getCharCode.

Prototype

public final int getCharCode() 

Source Link

Document

Gets the Unicode codepoint of the character generated by this key event.

Usage

From source file:com.badlogic.gdx.backends.gwt.GwtInput.java

License:Apache License

private void handleEvent(NativeEvent e) {
    if (e.getType().equals("mousedown")) {
        if (!e.getEventTarget().equals(canvas) || touched[0]) {
            float mouseX = getRelativeX(e, canvas);
            float mouseY = getRelativeY(e, canvas);
            if (mouseX < 0 || mouseX > Gdx.graphics.getWidth() || mouseY < 0
                    || mouseY > Gdx.graphics.getHeight()) {
                hasFocus = false;// w w w.  jav  a 2 s  . c o  m
            }
            return;
        }
        hasFocus = true;
        this.justTouched = true;
        this.touched[0] = true;
        this.pressedButtons.add(getButton(e.getButton()));
        this.deltaX[0] = 0;
        this.deltaY[0] = 0;
        if (isCursorCatched()) {
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        if (processor != null)
            processor.touchDown(touchX[0], touchY[0], 0, getButton(e.getButton()));
    }

    if (e.getType().equals("mousemove")) {
        if (isCursorCatched()) {
            this.deltaX[0] = (int) getMovementXJSNI(e);
            this.deltaY[0] = (int) getMovementYJSNI(e);
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
            this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        if (processor != null) {
            if (touched[0])
                processor.touchDragged(touchX[0], touchY[0], 0);
            else
                processor.mouseMoved(touchX[0], touchY[0]);
        }
    }

    if (e.getType().equals("mouseup")) {
        if (!touched[0])
            return;
        this.pressedButtons.remove(getButton(e.getButton()));
        this.touched[0] = pressedButtons.size > 0;
        if (isCursorCatched()) {
            this.deltaX[0] = (int) getMovementXJSNI(e);
            this.deltaY[0] = (int) getMovementYJSNI(e);
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
            this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        this.touched[0] = false;
        if (processor != null)
            processor.touchUp(touchX[0], touchY[0], 0, getButton(e.getButton()));
    }
    if (e.getType().equals(getMouseWheelEvent())) {
        if (processor != null) {
            processor.scrolled((int) getMouseWheelVelocity(e));
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("keydown") && hasFocus) {
        // System.out.println("keydown");
        int code = keyForCode(e.getKeyCode());
        if (code == 67) {
            e.preventDefault();
            if (processor != null) {
                processor.keyDown(code);
                processor.keyTyped('\b');
            }
        } else {
            if (!pressedKeys[code]) {
                pressedKeyCount++;
                pressedKeys[code] = true;
                keyJustPressed = true;
                justPressedKeys[code] = true;
                if (processor != null) {
                    processor.keyDown(code);
                }
            }
        }
    }

    if (e.getType().equals("keypress") && hasFocus) {
        // System.out.println("keypress");
        char c = (char) e.getCharCode();
        if (processor != null)
            processor.keyTyped(c);
    }

    if (e.getType().equals("keyup") && hasFocus) {
        // System.out.println("keyup");
        int code = keyForCode(e.getKeyCode());
        if (pressedKeys[code]) {
            pressedKeyCount--;
            pressedKeys[code] = false;
        }
        if (processor != null) {
            processor.keyUp(code);
        }
    }

    if (e.getType().equals("touchstart")) {
        this.justTouched = true;
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId;
            touchMap.put(real, touchId = getAvailablePointer());
            touched[touchId] = true;
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            deltaX[touchId] = 0;
            deltaY[touchId] = 0;
            if (processor != null) {
                processor.touchDown(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchmove")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (processor != null) {
                processor.touchDragged(touchX[touchId], touchY[touchId], touchId);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchcancel")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            touchMap.remove(real);
            touched[touchId] = false;
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (processor != null) {
                processor.touchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchend")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            touchMap.remove(real);
            touched[touchId] = false;
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (processor != null) {
                processor.touchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
            }
        }
        this.currentEventTimeStamp = TimeUtils.nanoTime();
        e.preventDefault();
    }
    // if(hasFocus) e.preventDefault();
}

From source file:com.sencha.gxt.cell.core.client.form.NumberInputCell.java

License:sencha.com license

@Override
protected void onKeyPress(Context context, Element parent, N value, NativeEvent event,
        ValueUpdater<N> valueUpdate) {
    super.onKeyPress(context, parent, value, event, valueUpdate);

    char key = (char) event.getCharCode();

    if (event.<XEvent>cast().isSpecialKey(lastKeyCode) || event.getCtrlKey()) {
        return;// w w  w .  j a  v  a  2s .  c  om
    }

    boolean paste = key == 'v' && event.<XEvent>cast().getCtrlOrMetaKey();
    if (!paste && !allowed.contains(key)) {
        event.stopPropagation();
        event.preventDefault();
    }
}

From source file:com.trollworks.webkeycodes.KeyCapturePanel.java

License:Open Source License

private void logKey(String msg, NativeEvent nativeEvent) {
    StringBuilder buffer = new StringBuilder();
    buffer.append(msg);//from  www  .  jav a2 s . c  o m
    buffer.append(" [char code: ");
    buffer.append(nativeEvent.getCharCode());
    buffer.append(", key code:");
    buffer.append(nativeEvent.getKeyCode());
    if (nativeEvent.getAltKey()) {
        buffer.append(", ALT");
    }
    if (nativeEvent.getCtrlKey()) {
        buffer.append(", CTRL");
    }
    if (nativeEvent.getMetaKey()) {
        buffer.append(", META");
    }
    if (nativeEvent.getShiftKey()) {
        buffer.append(", SHIFT");
    }
    buffer.append("]");
    String text = mLog.getText();
    if (text.trim().length() > 0) {
        buffer.append("\n");
        buffer.append(text);
    }
    mLog.setText(buffer.toString());
}

From source file:com.vaadin.addon.spreadsheet.client.SpreadsheetWidget.java

@Override
public void onSheetKeyPress(NativeEvent event, String enteredCharacter) {
    // Here we need to also check for char code 13 (which is code for enter)
    // since for some reason the enter key is reported having both
    // KeyCodes.ENTER and the char code 13, whereas other such non-character
    // keys here have no char codes. Enter key must be detected here to
    // start editing a cell.
    if ((event.getCharCode() == 0 && event.getKeyCode() != KeyCodes.KEY_SPACE) || event.getCharCode() == 13) {
        switch (event.getKeyCode()) {
        case KeyCodes.KEY_BACKSPACE:
        case KeyCodes.KEY_DELETE:
            checkEditableAndNotify();//from   w  w w  . j  a  v a  2s. c  o  m
            if (!cellLocked) {
                spreadsheetHandler.deleteSelectedCells();
                formulaBarWidget.setCellPlainValue("");
            }
            break;
        case KeyCodes.KEY_DOWN:
            if (event.getShiftKey()) {
                selectionHandler.increaseVerticalSelection(true);
            } else {
                selectionHandler.moveSelectionDown(true);
            }
            break;
        case KeyCodes.KEY_LEFT:
            if (event.getShiftKey()) {
                selectionHandler.increaseHorizontalSelection(false);
            } else {
                selectionHandler.moveSelectionLeft(true);
            }
            break;
        case KeyCodes.KEY_TAB:
            if (event.getShiftKey()) {
                selectionHandler.moveSelectionLeft(isSelectedCellHidden());
            } else {
                selectionHandler.moveSelectionRight(isSelectedCellHidden());
            }
            break;
        case KeyCodes.KEY_RIGHT:
            if (event.getShiftKey()) {
                selectionHandler.increaseHorizontalSelection(true);
            } else {
                selectionHandler.moveSelectionRight(true);
            }
            break;
        case KeyCodes.KEY_UP:
            if (event.getShiftKey()) {
                selectionHandler.increaseVerticalSelection(false);
            } else {
                selectionHandler.moveSelectionUp(true);
            }
            break;
        case KeyCodes.KEY_ALT:
        case KeyCodes.KEY_CTRL:
        case KeyCodes.KEY_END:
        case KeyCodes.KEY_ESCAPE:
        case KeyCodes.KEY_HOME:
        case KeyCodes.KEY_PAGEDOWN:
        case KeyCodes.KEY_PAGEUP:
        case KeyCodes.KEY_SHIFT:
            break;
        case KeyCodes.KEY_F2:
        case KeyCodes.KEY_ENTER:
            if (KeyCodes.KEY_ENTER == event.getKeyCode()) {
                if (isSelectedCellHidden()) {
                    selectionHandler.moveSelectionDown(true);
                    break;
                } else {
                    if (sheetWidget.getSelectionLeftCol() != sheetWidget.getSelectionRightCol()
                            || sheetWidget.getSelectionTopRow() != sheetWidget.getSelectionBottomRow()) {
                        if (event.getShiftKey()) {
                            selectionHandler.moveSelectionUp(false);
                        } else {
                            selectionHandler.moveSelectionDown(false);
                        }
                        break;
                    }
                }
            }
            checkEditableAndNotify();
            if (!sheetWidget.isSelectedCellCustomized() && !inlineEditing && !cellLocked
                    && !customCellEditorDisplayed) {
                cachedCellValue = sheetWidget.getSelectedCellLatestValue();
                formulaBarWidget.cacheFormulaFieldValue();
                formulaBarEditing = false;
                inlineEditing = true;
                sheetWidget.startEditingCell(true, true, formulaBarWidget.getFormulaFieldValue());
                formulaBarWidget.startInlineEdit(true);
            }
            break;
        }
    } else {
        if (!isSelectedCellHidden()) {
            checkEditableAndNotify();

            if (!sheetWidget.isSelectedCellCustomized() && !inlineEditing && !cellLocked
                    && !customCellEditorDisplayed) {
                // cache value and start editing cell as empty
                inlineEditing = true;
                cachedCellValue = sheetWidget.getSelectedCellLatestValue();

                formulaBarWidget.startInlineEdit(true);

                if (cachedCellValue.endsWith("%") || sheetWidget.isSelectedCellPergentage()) {

                    if (isNumericChar(enteredCharacter)) {
                        enteredCharacter = enteredCharacter + "%";
                    }
                    sheetWidget.startEditingCell(true, true, enteredCharacter);
                } else {
                    sheetWidget.startEditingCell(true, true, enteredCharacter);
                    formulaBarWidget.cacheFormulaFieldValue();
                }
                formulaBarWidget.setCellPlainValue(enteredCharacter);
            }
        }
    }
}

From source file:com.vaadin.client.ui.SuperTreeWidget.java

License:Apache License

@Override
public void onKeyPress(KeyPressEvent event) {
    NativeEvent nativeEvent = event.getNativeEvent();
    int keyCode = nativeEvent.getKeyCode();
    if (keyCode == 0 && nativeEvent.getCharCode() == ' ') {
        // Provide a keyCode for space to be compatible with FireFox
        // keypress event
        keyCode = CHARCODE_SPACE;/*from w ww  .  ja v  a2s  .  c o  m*/
    }
    if (handleKeyNavigation(keyCode, event.isControlKeyDown() || event.isMetaKeyDown(),
            event.isShiftKeyDown())) {
        event.preventDefault();
        event.stopPropagation();
    }
}

From source file:com.vaadin.terminal.gwt.client.ui.VTree.java

License:Open Source License

public void onKeyPress(KeyPressEvent event) {
    NativeEvent nativeEvent = event.getNativeEvent();
    int keyCode = nativeEvent.getKeyCode();
    if (keyCode == 0 && nativeEvent.getCharCode() == ' ') {
        // Provide a keyCode for space to be compatible with FireFox
        // keypress event
        keyCode = CHARCODE_SPACE;//from  w  w w .j  a  va2 s. co  m
    }
    if (handleKeyNavigation(keyCode, event.isControlKeyDown() || event.isMetaKeyDown(),
            event.isShiftKeyDown())) {
        event.preventDefault();
        event.stopPropagation();
    }
}

From source file:de.lilawelt.zmachine.client.CommandLine.java

License:Open Source License

public boolean HandleReadLineEvent(NativePreviewEvent preview, final MachineInterface mi) {
    NativeEvent event = preview.getNativeEvent();
    if (event.getMetaKey() || event.getCtrlKey() || event.getAltKey()) {
        // we do not handle this, give it to the browser
        return false;
    }//from  w w w  . j a  v  a 2s  . co m

    Character keyCode = (char) event.getKeyCode();
    if (keyCode == 0) {
        keyCode = (char) event.getCharCode();
    }
    Log.debug("Got keypress event: " + event.getKeyCode());

    // backspace and keys is handled via down-event, everything else via keypress:
    if (preview.getTypeInt() == Event.ONKEYDOWN && (event.getKeyCode() != KEY_BACKSPACE
            && event.getKeyCode() != KEY_LEFT && event.getKeyCode() != KEY_RIGHT && event.getKeyCode() != KEY_UP
            && event.getKeyCode() != KEY_DOWN)) {
        return false;
    } else if (preview.getTypeInt() == Event.ONKEYPRESS && (event.getKeyCode() == KEY_BACKSPACE
            || event.getKeyCode() == KEY_LEFT || event.getKeyCode() == KEY_RIGHT || event.getKeyCode() == KEY_UP
            || event.getKeyCode() == KEY_DOWN)) {
        return false;
    }

    switch (keyCode) {
    case KEY_ALT:
    case KEY_LEFT:
    case KEY_PAGEUP:
    case KEY_RIGHT:
    case KEY_PAGEDOWN:
    case KEY_TAB:
    case KEY_END:
    case KEY_ESCAPE:
    case KEY_HOME:
    case KEY_CTRL:
        // case KeyboardListener.MODIFIER_CTRL:
        // case KeyboardListener.MODIFIER_ALT:
        // case KeyboardListener.MODIFIER_META:
        return false;
    }

    if (keyCode > 1000) {
        return false;
    }

    boolean terminate = false;

    //Log.debug("I am here");
    if (keyCode == KEY_ENTER) {
        terminate = true;
    } else if (mi.getTermChars() != null) {
        //Log.debug("and here");
        for (Integer c : mi.getTermChars()) {
            Log.debug("Trying additional terminating characters...: " + c.intValue() + " (code) "
                    + event.getKeyCode());
            if (c.equals(event.getKeyCode())) {
                Log.debug("Found terminating character");
                terminate = true;
            }
        }
    }

    if (keyCode == KEY_UP) {
        Log.debug("Handled KEY_UP");
        // we have no history or are at the last element.
        if (history.size() == 0 || currPos == 0) {
            return true;
        }

        currPos--;
        mi.lastLineSb = new StringBuffer(history.get(currPos));
        mi.lastLine.setHTML(mi.lastLineBase + MachineInterface.htmlEscape(mi.lastLineSb.toString()));

        Log.debug("Now at position: " + currPos);

        return true;
    } else if (keyCode == KEY_DOWN) {
        Log.debug("Handled KEY_DOWN");
        if (history.size() == 0 || currPos >= history.size()) {
            return true;
        }

        currPos++;
        Log.debug("Now at position: " + currPos);

        if (currPos == history.size()) {
            mi.lastLineSb = new StringBuffer();
            mi.lastLine.setHTML(mi.lastLineBase);
        }

        mi.lastLineSb = new StringBuffer(history.get(currPos));
        mi.lastLine.setHTML(mi.lastLineBase + MachineInterface.htmlEscape(mi.lastLineSb.toString()));

        return true;
    } else if (keyCode == KEY_BACKSPACE) {
        if (mi.lastLineSb.length() > 0) {
            mi.lastLineSb.deleteCharAt(mi.lastLineSb.length() - 1);
            mi.lastLine.setHTML(mi.lastLineBase + MachineInterface.htmlEscape(mi.lastLineSb.toString()));
        }
    } else if (terminate) {

        if (mi.readMode == READMODES.READLINE_TIMED) {
            mi.readTimer.cancel();
            mi.readTimer = null;
        }

        history.add(mi.lastLineSb.toString()); // add it to history

        mi.lastLineBase = mi.lastLineBase + MachineInterface.htmlEscape(mi.lastLineSb.toString() + "\n");
        mi.lastLine.setHTML(mi.lastLineBase);
        final int lastkey = keyCode;
        Log.debug("trying to continue execution...1");
        mi.previewHandler.removeHandler();
        mi.previewHandler = null;
        Log.debug("Queueing");
        mi.readMode = READMODES.ERROR;
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
            public void execute() {
                Log.debug("trying to continue execution...2");
                mi.getCpu().zop_read_continue(mi.lastLineSb.toString(), lastkey);
                mi.lastLineSb = null;
                Log.debug("trying to continue execution...3");
                Machine.get().continueExecution();
            }
        });
    } else {
        String key = keyCode.toString();
        mi.lastLineSb.append(key);
        mi.lastLine.setHTML(mi.lastLineBase + MachineInterface.htmlEscape(mi.lastLineSb.toString()));
    }
    return true;
}

From source file:de.lilawelt.zmachine.client.MachineInterface.java

License:Open Source License

public boolean HandleReadCharEvent(NativePreviewEvent preview) {
    NativeEvent event = preview.getNativeEvent();

    if (event.getMetaKey() || event.getCtrlKey() || event.getAltKey()) {
        // we do not handle this, give it to the browser
        return false;
    }/* w  ww.  j av  a  2s  .c om*/

    // backspace and keys is handled via down-event, everything else via keypress:
    if (preview.getTypeInt() == Event.ONKEYDOWN && (event.getKeyCode() != KEY_BACKSPACE
            && event.getKeyCode() != KEY_LEFT && event.getKeyCode() != KEY_RIGHT && event.getKeyCode() != KEY_UP
            && event.getKeyCode() != KEY_DOWN)) {
        return false;
    } else if (preview.getTypeInt() == Event.ONKEYPRESS && (event.getKeyCode() == KEY_BACKSPACE
            || event.getKeyCode() == KEY_LEFT || event.getKeyCode() == KEY_RIGHT || event.getKeyCode() == KEY_UP
            || event.getKeyCode() == KEY_DOWN)) {
        return false;
    }

    int key = event.getKeyCode();
    Character keyCode = (char) event.getKeyCode();
    if (keyCode == 0) {
        keyCode = (char) event.getCharCode();
    }
    Log.debug("KeyCode " + key);

    if (key == KEY_UP) {
        keyCode = 129;
        Log.debug("up");
    } else if (key == KEY_DOWN) {
        keyCode = 130;
    } else if (key == KEY_LEFT) {
        keyCode = 131;
    } else if (key == KEY_RIGHT) {
        keyCode = 132;
    }

    if (readMode == READMODES.READCHAR_TIMED) {
        readTimer.cancel(); // remove unneeded timer.
        readTimer = null;
    }

    final int lastkey = keyCode;
    Log.debug("trying to continue execution...1");
    previewHandler.removeHandler();
    previewHandler = null;
    readMode = READMODES.ERROR;
    Log.debug("Queueing");
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        public void execute() {
            Log.debug("trying to continue execution...2");
            getCpu().zop_read_char_continue(lastkey);
            Log.debug("trying to continue execution...3");
            Machine.get().continueExecution();
        }
    });

    return true;
}

From source file:org.parallax3d.parallax.platforms.gwt.GwtInput.java

License:Open Source License

private void handleEvent(NativeEvent e) {
    if (e.getType().equals("mousedown")) {
        if (!e.getEventTarget().equals(canvas) || touched[0]) {
            float mouseX = getRelativeX(e, canvas);
            float mouseY = getRelativeY(e, canvas);
            if (mouseX < 0 || mouseX > canvas.getWidth() || mouseY < 0 || mouseY > canvas.getHeight()) {
                hasFocus = false;//  w w  w.  j  av a  2s.  c  om
            }
            return;
        }
        hasFocus = true;
        this.justTouched = true;
        this.touched[0] = true;
        this.pressedButtons.add(getButton(e.getButton()));
        this.deltaX[0] = 0;
        this.deltaY[0] = 0;
        if (isCursorCatched()) {
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = Duration.nanoTime();
        if (touchDownHandler != null)
            touchDownHandler.onTouchDown(touchX[0], touchY[0], 0, getButton(e.getButton()));
    }

    if (e.getType().equals("mousemove")) {
        if (isCursorCatched()) {
            this.deltaX[0] = (int) getMovementXJSNI(e);
            this.deltaY[0] = (int) getMovementYJSNI(e);
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
            this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = Duration.nanoTime();

        if (touchMoveHandler != null)
            touchMoveHandler.onTouchMove(touchX[0], touchY[0], 0);
        if (touchDraggedHandler != null && touched[0])
            touchDraggedHandler.onTouchDragged(touchX[0], touchY[0], 0);
    }

    if (e.getType().equals("mouseup")) {
        if (!touched[0])
            return;
        this.pressedButtons.remove(getButton(e.getButton()));
        this.touched[0] = pressedButtons.size() > 0;
        if (isCursorCatched()) {
            this.deltaX[0] = (int) getMovementXJSNI(e);
            this.deltaY[0] = (int) getMovementYJSNI(e);
            this.touchX[0] += getMovementXJSNI(e);
            this.touchY[0] += getMovementYJSNI(e);
        } else {
            this.deltaX[0] = getRelativeX(e, canvas) - touchX[0];
            this.deltaY[0] = getRelativeY(e, canvas) - touchY[0];
            this.touchX[0] = getRelativeX(e, canvas);
            this.touchY[0] = getRelativeY(e, canvas);
        }
        this.currentEventTimeStamp = Duration.nanoTime();
        this.touched[0] = false;
        if (touchUpHandler != null)
            touchUpHandler.onTouchUp(touchX[0], touchY[0], 0, getButton(e.getButton()));
    }
    if (e.getType().equals(getMouseWheelEvent())) {
        if (scrolledHandler != null) {
            scrolledHandler.onScrolled((int) getMouseWheelVelocity(e));
        }
        this.currentEventTimeStamp = Duration.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("keydown") && hasFocus) {
        // System.out.println("keydown");
        int code = e.getKeyCode();
        if (code == 67) {
            e.preventDefault();
            if (keyDownHandler != null)
                keyDownHandler.onKeyDown(code);
            if (keyTypedHandler != null)
                keyTypedHandler.onKeyTyped('\b');
        } else {
            if (!pressedKeys[code]) {
                pressedKeyCount++;
                pressedKeys[code] = true;
                keyJustPressed = true;
                justPressedKeys[code] = true;
                if (keyDownHandler != null)
                    keyDownHandler.onKeyDown(code);
            }
        }
    }

    if (e.getType().equals("keypress") && hasFocus) {
        // System.out.println("keypress");
        char c = (char) e.getCharCode();
        if (keyTypedHandler != null)
            keyTypedHandler.onKeyTyped(c);
    }

    if (e.getType().equals("keyup") && hasFocus) {
        // System.out.println("keyup");
        int code = e.getKeyCode();
        if (pressedKeys[code]) {
            pressedKeyCount--;
            pressedKeys[code] = false;
        }

        if (keyUpHandler != null)
            keyUpHandler.onKeyUp(code);
    }

    if (e.getType().equals("touchstart")) {
        this.justTouched = true;
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId;
            touchMap.put(real, touchId = getAvailablePointer());
            touched[touchId] = true;
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            deltaX[touchId] = 0;
            deltaY[touchId] = 0;

            if (touchDownHandler != null)
                touchDownHandler.onTouchDown(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
        }
        this.currentEventTimeStamp = Duration.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchmove")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (touchDraggedHandler != null)
                touchDraggedHandler.onTouchDragged(touchX[touchId], touchY[touchId], touchId);
        }
        this.currentEventTimeStamp = Duration.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchcancel")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            touchMap.remove(real);
            touched[touchId] = false;
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (touchUpHandler != null)
                touchUpHandler.onTouchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
        }
        this.currentEventTimeStamp = Duration.nanoTime();
        e.preventDefault();
    }
    if (e.getType().equals("touchend")) {
        JsArray<Touch> touches = e.getChangedTouches();
        for (int i = 0, j = touches.length(); i < j; i++) {
            Touch touch = touches.get(i);
            int real = touch.getIdentifier();
            int touchId = touchMap.get(real);
            touchMap.remove(real);
            touched[touchId] = false;
            deltaX[touchId] = getRelativeX(touch, canvas) - touchX[touchId];
            deltaY[touchId] = getRelativeY(touch, canvas) - touchY[touchId];
            touchX[touchId] = getRelativeX(touch, canvas);
            touchY[touchId] = getRelativeY(touch, canvas);
            if (touchUpHandler != null)
                touchUpHandler.onTouchUp(touchX[touchId], touchY[touchId], touchId, Buttons.LEFT);
        }
        this.currentEventTimeStamp = Duration.nanoTime();
        e.preventDefault();
    }
    // if(hasFocus) e.preventDefault();
}

From source file:playn.html.HtmlKeyboard.java

License:Apache License

public void init() {
    // Key handlers.
    HtmlPlatform.captureEvent("keydown", new EventHandler() {
        @Override//from www. j  ava 2 s  . co  m
        public void handleEvent(NativeEvent nativeEvent) {
            if (listener != null) {
                Event.Impl event = new Event.Impl(PlayN.currentTime(), keyForCode(nativeEvent.getKeyCode()));
                listener.onKeyDown(event);
                if (event.getPreventDefault()) {
                    nativeEvent.preventDefault();
                }
            }
        }
    });

    HtmlPlatform.captureEvent("keypress", new EventHandler() {
        public void handleEvent(NativeEvent nativeEvent) {
            if (listener != null) {
                TypedEvent.Impl event = new TypedEvent.Impl(PlayN.currentTime(),
                        (char) nativeEvent.getCharCode());
                listener.onKeyTyped(event);
                if (event.getPreventDefault()) {
                    nativeEvent.preventDefault();
                }
            }
        }
    });

    HtmlPlatform.captureEvent("keyup", new EventHandler() {
        @Override
        public void handleEvent(NativeEvent nativeEvent) {
            if (listener != null) {
                Event.Impl event = new Event.Impl(PlayN.currentTime(), keyForCode(nativeEvent.getKeyCode()));
                listener.onKeyUp(event);
                if (event.getPreventDefault()) {
                    nativeEvent.preventDefault();
                }
            }
        }
    });
}