Java Code Examples for android.view.KeyEvent#KEYCODE_A

The following examples show how to use android.view.KeyEvent#KEYCODE_A . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IMEService.java    From TVRemoteIME with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if(handleKeyboardFocusEvent(keyCode)) return true;
	if (Environment.needDebug) {
		Environment.debug(TAG, "keydown-event:" + keyCode);
	}
	//同步软键盘状态处理代码:不处理以下按键事件则有可能物理键盘字符输入与软键盘的大小写状态不同步
	if(keyCode == KeyEvent.KEYCODE_CAPS_LOCK) capsOn = !capsOn;
	if ((keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9)) {
		if(commitText(String.valueOf(keyCode - KeyEvent.KEYCODE_0))) return true;
	} else if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
		if (commitText(String.valueOf((char) ((capsOn ? 65 : 97) + keyCode - KeyEvent.KEYCODE_A))))
			return true;
	}
	return super.onKeyDown(keyCode, event);
}
 
Example 2
Source File: LuaEditor.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean onKeyShortcut(int keyCode, KeyEvent event) {
    final int filteredMetaState = event.getMetaState() & ~KeyEvent.META_CTRL_MASK;
    if (KeyEvent.metaStateHasNoModifiers(filteredMetaState)) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_A:
                selectAll();
                return true;
            case KeyEvent.KEYCODE_X:
                cut();
                return true;
            case KeyEvent.KEYCODE_C:
                copy();
                return true;
            case KeyEvent.KEYCODE_V:
                paste();
                return true;
        }
    }
    return super.onKeyShortcut(keyCode, event);
}
 
Example 3
Source File: MatrixKeypadTest.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the callback will retrieve the expected values.
 * @throws IOException
 */
@Test
public void testCallbackKeyDown() throws IOException {
    final int[] index = {0};
    final int[] testKeys = new int[] {KeyEvent.KEYCODE_A, KeyEvent.KEYCODE_NUMPAD_1,
            KeyEvent.KEYCODE_PERIOD};

    mMatrixKeypad = new MatrixKeypad(mRowPins, mColPins, mKeyCodes,
            PowerMockito.mock(Handler.class));
    mMatrixKeypad.setKeyCallback(new OnKeyEventListener() {
        @Override
        public void onKeyEvent(MatrixKey matrixKey) {
            assertEquals(testKeys[index[0]++], matrixKey.getKeyCode());
            assertTrue(matrixKey.isPressed());
        }
    });

    for (int key : testKeys) {
        mMatrixKeypad.keyDown(key);
    }
}
 
Example 4
Source File: Performance.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Method to start tracking processing latency for a key event.
 *
 * @param event A key event just received by TalkBack
 * @return An event id that can be used to track performance through later stages.
 */
public EventId onEventReceived(@NonNull KeyEvent event) {
  int keycode = event.getKeyCode();
  EventId eventId = new EventId(event.getEventTime(), EVENT_TYPE_KEY, keycode);
  if (!mEnabled) {
    return eventId;
  }

  // Segment key events based on key groups.
  String label = "KeyEvent-other";
  if (KeyEvent.KEYCODE_0 <= keycode && keycode <= KeyEvent.KEYCODE_9) {
    label = "KeyEvent-numeric";
  } else if (KeyEvent.KEYCODE_A <= keycode && keycode <= KeyEvent.KEYCODE_Z) {
    label = "KeyEvent-alpha";
  } else if (KeyEvent.KEYCODE_DPAD_UP <= keycode && keycode <= KeyEvent.KEYCODE_DPAD_CENTER) {
    label = "KeyEvent-dpad";
  } else if (KeyEvent.KEYCODE_VOLUME_UP <= keycode && keycode <= KeyEvent.KEYCODE_VOLUME_DOWN) {
    label = "KeyEvent-volume";
  }
  String[] labels = {label};

  onEventReceived(eventId, labels);
  return eventId;
}
 
Example 5
Source File: ScanGunKeyEventHelper.java    From scangon with Apache License 2.0 5 votes vote down vote up
private char getInputCode(KeyEvent event) {

        int keyCode = event.getKeyCode();

        char aChar;

        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
            //字母
            aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
        } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
            //数字
            aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
        } else {
            //其他符号
            switch (keyCode) {
                case KeyEvent.KEYCODE_PERIOD:
                    aChar = '.';
                    break;
                case KeyEvent.KEYCODE_MINUS:
                    aChar = mCaps ? '_' : '-';
                    break;
                case KeyEvent.KEYCODE_SLASH:
                    aChar = '/';
                    break;
                case KeyEvent.KEYCODE_BACKSLASH:
                    aChar = mCaps ? '|' : '\\';
                    break;
                default:
                    aChar = 0;
                    break;
            }
        }

        return aChar;

    }
 
Example 6
Source File: WeatherStationActivity.java    From weatherstation with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_A) {
        mDisplayMode = DisplayMode.TEMPERATURE;
        updateDisplay(mLastTemperature);
        try {
            mLed.setValue(false);
        } catch (IOException e) {
            Log.e(TAG, "error updating LED", e);
        }
        return true;
    }
    return super.onKeyUp(keyCode, event);
}
 
Example 7
Source File: UndoRedoSupportEditText.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (DLog.DEBUG) {
        Log.w(TAG, "onKeyUp " + event);
    }
    if (handleControlKey(keyCode, event, false)) {
        return true;
    }
    if (event.isCtrlPressed() || mKeyListener.mControlKey.isActive()) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_A:
            case KeyEvent.KEYCODE_X:
            case KeyEvent.KEYCODE_C:
            case KeyEvent.KEYCODE_V:
            case KeyEvent.KEYCODE_Z:
            case KeyEvent.KEYCODE_Y:
            case KeyEvent.KEYCODE_S:
            case KeyEvent.KEYCODE_R:
            case KeyEvent.KEYCODE_F:
            case KeyEvent.KEYCODE_L:
                return true;
        }
    } else {
        switch (keyCode) {
            case KeyEvent.KEYCODE_TAB:
                return true;
        }
    }
    return super.onKeyUp(keyCode, event);

}
 
Example 8
Source File: Launcher.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKeyShortcut(int keyCode, KeyEvent event) {
    if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_A:
                if (mState == State.WORKSPACE) {
                    showAppsView(true, false);
                    return true;
                }
                break;
            case KeyEvent.KEYCODE_S: {
                View focusedView = getCurrentFocus();
                if (focusedView instanceof BubbleTextView
                        && focusedView.getTag() instanceof ItemInfo
                        && mAccessibilityDelegate.performAction(focusedView,
                                (ItemInfo) focusedView.getTag(),
                                LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
                    PopupContainerWithArrow.getOpen(this).requestFocus();
                    return true;
                }
                break;
            }
            case KeyEvent.KEYCODE_O:
                if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
                    return true;
                }
                break;
        }
    }
    return super.onKeyShortcut(keyCode, event);
}
 
Example 9
Source File: Utils.java    From HgLauncher with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handles common input shortcut from an EditText.
 *
 * @param activity The activity to reference for copying and pasting.
 * @param editText The EditText where the text is being copied/pasted.
 * @param keyCode  Keycode to handle.
 *
 * @return True if key is handled.
 */
public static boolean handleInputShortcut(AppCompatActivity activity, EditText editText, int keyCode) {
    // Get selected text for cut and copy.
    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    final String text = editText.getText().toString().substring(start, end);

    switch (keyCode) {
        case KeyEvent.KEYCODE_A:
            editText.selectAll();
            return true;
        case KeyEvent.KEYCODE_X:
            editText.setText(editText.getText().toString().replace(text, ""));
            return true;
        case KeyEvent.KEYCODE_C:
            ActivityServiceUtils.copyToClipboard(activity, text);
            return true;
        case KeyEvent.KEYCODE_V:
            editText.setText(
                    editText.getText().replace(Math.min(start, end), Math.max(start, end),
                            ActivityServiceUtils.pasteFromClipboard(activity), 0,
                            ActivityServiceUtils.pasteFromClipboard(activity).length()));
            return true;
        default:
            // Do nothing.
            return false;
    }
}
 
Example 10
Source File: RobocarActivity.java    From robocar with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_A) { //29
        if (!mKeyPressed) {
            mKeyPressed = true;
            mResetHandler.postDelayed(mDisconnectRunnable, DISCONNECT_DELAY);
            mResetHandler.postDelayed(mResetRunnable, RESET_DELAY);

        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 11
Source File: WeatherStationActivity.java    From weatherstation with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_A) {
        mDisplayMode = DisplayMode.PRESSURE;
        updateDisplay(mLastPressure);
        try {
            mLed.setValue(true);
        } catch (IOException e) {
            Log.e(TAG, "error updating LED", e);
        }
        return true;
    }
    return super.onKeyUp(keyCode, event);
}
 
Example 12
Source File: SimpleFormPostActivity.java    From TvAppRepo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_A) {
        submitForm();
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 13
Source File: Utils.java    From HgLauncher with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handles common input shortcut from an EditText.
 *
 * @param activity The activity to reference for copying and pasting.
 * @param editText The EditText where the text is being copied/pasted.
 * @param keyCode  Keycode to handle.
 *
 * @return True if key is handled.
 */
public static boolean handleInputShortcut(AppCompatActivity activity, EditText editText, int keyCode) {
    // Get selected text for cut and copy.
    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    final String text = editText.getText().toString().substring(start, end);

    switch (keyCode) {
        case KeyEvent.KEYCODE_A:
            editText.selectAll();
            return true;
        case KeyEvent.KEYCODE_X:
            editText.setText(editText.getText().toString().replace(text, ""));
            return true;
        case KeyEvent.KEYCODE_C:
            ActivityServiceUtils.copyToClipboard(activity, text);
            return true;
        case KeyEvent.KEYCODE_V:
            editText.setText(
                    editText.getText().replace(Math.min(start, end), Math.max(start, end),
                            ActivityServiceUtils.pasteFromClipboard(activity), 0,
                            ActivityServiceUtils.pasteFromClipboard(activity).length()));
            return true;
        default:
            // Do nothing.
            return false;
    }
}
 
Example 14
Source File: GUIListeners.java    From Beats with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static public int keyCode2Direction(int keyCode) {
	/* interprets a keyCode as a direction
	 * input:   keyCode  - a key code passed to handler           
	 * output: [0 1 2 3] -> [left down up right]; -1 -> unknown
	 */
	switch (keyCode) {
	// WASD, ZX-NM spread, AS-KL spread
	// 12-90 spread, D-Pad
	// Headphone music controller
	case KeyEvent.KEYCODE_A: case KeyEvent.KEYCODE_Z:
	case KeyEvent.KEYCODE_1: case KeyEvent.KEYCODE_DPAD_LEFT:
	case KeyEvent.KEYCODE_MEDIA_PREVIOUS: case KeyEvent.KEYCODE_MEDIA_REWIND:
	case KeyEvent.KEYCODE_BUTTON_X:
		return 0;
	case KeyEvent.KEYCODE_S: case KeyEvent.KEYCODE_X:
	case KeyEvent.KEYCODE_2: case KeyEvent.KEYCODE_DPAD_DOWN:
	case KeyEvent.KEYCODE_MEDIA_STOP:
	case KeyEvent.KEYCODE_BUTTON_A:
		return 1;
	case KeyEvent.KEYCODE_W: case KeyEvent.KEYCODE_N: case KeyEvent.KEYCODE_K:
	case KeyEvent.KEYCODE_9: case KeyEvent.KEYCODE_DPAD_UP:
	case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
	case KeyEvent.KEYCODE_BUTTON_Y:
		return 2;
	case KeyEvent.KEYCODE_D: case KeyEvent.KEYCODE_M: case KeyEvent.KEYCODE_L:
	case KeyEvent.KEYCODE_0: case KeyEvent.KEYCODE_DPAD_RIGHT:
	case KeyEvent.KEYCODE_MEDIA_NEXT: case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
	case KeyEvent.KEYCODE_BUTTON_B:
		return 3;
	default:
		return -1;
	}
}
 
Example 15
Source File: VideoPlayerActivity.java    From VCL-Android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_BUTTON_B)
        return super.onKeyDown(keyCode, event);
    if (mIsLoading) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_S:
            case KeyEvent.KEYCODE_MEDIA_STOP:
                exitOK();
                return true;
        }
        return false;
    }
    showOverlayTimeout(OVERLAY_TIMEOUT);
    switch (keyCode) {
    case KeyEvent.KEYCODE_F:
    case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
    case KeyEvent.KEYCODE_MEDIA_NEXT:
        seekDelta(10000);
        return true;
    case KeyEvent.KEYCODE_R:
    case KeyEvent.KEYCODE_MEDIA_REWIND:
    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        seekDelta(-10000);
        return true;
    case KeyEvent.KEYCODE_BUTTON_R1:
        seekDelta(60000);
        return true;
    case KeyEvent.KEYCODE_BUTTON_L1:
        seekDelta(-60000);
        return true;
    case KeyEvent.KEYCODE_BUTTON_A:
        if (mOverlayProgress.getVisibility() == View.VISIBLE)
            return false;
    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
    case KeyEvent.KEYCODE_MEDIA_PLAY:
    case KeyEvent.KEYCODE_MEDIA_PAUSE:
    case KeyEvent.KEYCODE_SPACE:
        if (mIsNavMenu)
            return navigateDvdMenu(keyCode);
        else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) //prevent conflict with remote control
            return super.onKeyDown(keyCode, event);
        else
            doPlayPause();
        return true;
    case KeyEvent.KEYCODE_O:
    case KeyEvent.KEYCODE_BUTTON_Y:
    case KeyEvent.KEYCODE_MENU:
        showAdvancedOptions(mAdvOptions);
        return true;
    case KeyEvent.KEYCODE_V:
    case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
    case KeyEvent.KEYCODE_BUTTON_X:
        onAudioSubClick(mTracks);
        return true;
    case KeyEvent.KEYCODE_N:
        showNavMenu();
        return true;
    case KeyEvent.KEYCODE_A:
        resizeVideo();
        return true;
    case KeyEvent.KEYCODE_M:
    case KeyEvent.KEYCODE_VOLUME_MUTE:
        updateMute();
        return true;
    case KeyEvent.KEYCODE_S:
    case KeyEvent.KEYCODE_MEDIA_STOP:
        exitOK();
        return true;
    case KeyEvent.KEYCODE_DPAD_UP:
    case KeyEvent.KEYCODE_DPAD_DOWN:
    case KeyEvent.KEYCODE_DPAD_LEFT:
    case KeyEvent.KEYCODE_DPAD_RIGHT:
    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_ENTER:
        if (mIsNavMenu)
            return navigateDvdMenu(keyCode);
        else
            return super.onKeyDown(keyCode, event);
    case KeyEvent.KEYCODE_J:
        delayAudio(-50000l);
        return true;
    case KeyEvent.KEYCODE_K:
        delayAudio(50000l);
        return true;
    case KeyEvent.KEYCODE_G:
        delaySubs(-50000l);
        return true;
    case KeyEvent.KEYCODE_H:
        delaySubs(50000l);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (mMute) {
            updateMute();
            return true;
        } else
            return false;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 16
Source File: SoftKeyboard.java    From Android-tv-widget with Apache License 2.0 4 votes vote down vote up
/**
 * 判断是否为字母
 */
private boolean isLetter(SoftKey sKey) {
	return ((sKey.getKeyCode() >= KeyEvent.KEYCODE_A) && (sKey.getKeyCode() <= KeyEvent.KEYCODE_Z));
}
 
Example 17
Source File: FastEdit.java    From fastedit with Apache License 2.0 4 votes vote down vote up
/**
 * 快捷方式
 *
 * @param ctrl
 * @param alt
 * @param shift
 * @param keyCode
 */
protected boolean onShortKey(boolean ctrl, boolean alt, boolean shift, int keyCode) {
    if (ctrl) {
        if (keyCode == KeyEvent.KEYCODE_A) {
            selectModel.select(0, length());
            postInvalidate();
            return true;


        }
        if (keyCode == KeyEvent.KEYCODE_C) {
            if (selectModel.hasSelect()) {
                try {
                    copy();
                } catch (Exception e) {
                }
                return true;
            }
        }
        if (keyCode == KeyEvent.KEYCODE_X) {
            if (selectModel.hasSelect()) {
                cut();
                return true;
            }
        }
        if (keyCode == KeyEvent.KEYCODE_Z) {
            undo();
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_Y) {
            redo();
            return true;
        }

    }
    if (shift) {
        try {
            if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                if (!selectModel.hasSelect()) {
                    selectModel.start(cursorPos);
                }
                selectModel.toLeft(1);
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                if (!selectModel.hasSelect()) {
                    selectModel.start(cursorPos);
                }
                selectModel.toRight(1);
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                if (!selectModel.hasSelect()) {
                    selectModel.start(cursorPos);
                }
                selectModel.toUp(1);
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                if (!selectModel.hasSelect()) {
                    selectModel.start(cursorPos);
                }
                selectModel.toDown(1);
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_MOVE_HOME) {
                if (!selectModel.hasSelect()) {
                    selectModel.start(cursorPos);
                }
                selectModel.toHome();
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_MOVE_END) {
                if (!selectModel.hasSelect()) {
                    selectModel.start(cursorPos);
                }
                selectModel.toEnd();
                return true;
            }

        } finally {
            postInvalidate();
        }
    }
    return false;
}
 
Example 18
Source File: UndoRedoSupportEditText.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
     * CTRL + C copy
     * CTRL + V paste
     * CTRL + B: compile
     * CTRL + R generate
     * CTRL + X cut
     * CTRL + Z undo
     * CTRL + Y redo
     * CTRL + Q quit
     * CTRL + S save
     * CTRL + O open
     * CTRL + F find
     * CTRL + H find and replace
     * CTRL + L format code
     * CTRL + G: goto line
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (DLog.DEBUG) Log.w(TAG, "onKeyDown: " + keyCode + " " + event);
        if (handleControlKey(keyCode, event, false)) {
            return true;
        }
        if (event.isCtrlPressed() || mKeyListener.mControlKey.isActive()) {
//            Log.i(TAG, "onKeyDown: process");
            switch (keyCode) {
                case KeyEvent.KEYCODE_A:
                    selectAll();
                    return true;
                case KeyEvent.KEYCODE_X:
                    cut();
                    return true;
                case KeyEvent.KEYCODE_C:
                    copy();
                    return true;
                case KeyEvent.KEYCODE_V:
                    paste();
                    return true;
                case KeyEvent.KEYCODE_G: //go to line
                    if (editorControl != null)
                        editorControl.goToLine();
                    return true;
                case KeyEvent.KEYCODE_L: //format
                    if (editorControl != null)
                        editorControl.formatCode();
                    return true;
                case KeyEvent.KEYCODE_Z:
                    if (canUndo()) {
                        undo();
                    }
                    return true;
                case KeyEvent.KEYCODE_Y:
                    if (canRedo()) {
                        redo();
                    }
                    return true;
                case KeyEvent.KEYCODE_S:
                    if (editorControl != null)
                        editorControl.saveCurrentFile();
                    return true;
                case KeyEvent.KEYCODE_N:
                    if (editorControl != null)
                        editorControl.saveAs();
                    return true;
                default:
                    return super.onKeyDown(keyCode, event);
            }
        } else {
            switch (keyCode) {
                case KeyEvent.KEYCODE_TAB:
                    String textToInsert = mSettings.getTabStr();
                    int start, end;
                    start = Math.max(getSelectionStart(), 0);
                    end = Math.max(getSelectionEnd(), 0);
                    getText().replace(Math.min(start, end), Math.max(start, end),
                            textToInsert, 0, textToInsert.length());
                    return true;
                default:
                    try {
                        return super.onKeyDown(keyCode, event);
                    } catch (Exception e) {
                    }
                    return false;

            }
        }
    }
 
Example 19
Source File: Editor.java    From turbo-editor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {

    if (event.isCtrlPressed()) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_A:
                return onTextContextMenuItem(ID_SELECT_ALL);
            case KeyEvent.KEYCODE_X:
                return onTextContextMenuItem(ID_CUT);
            case KeyEvent.KEYCODE_C:
                return onTextContextMenuItem(ID_COPY);
            case KeyEvent.KEYCODE_V:
                return onTextContextMenuItem(ID_PASTE);
            case KeyEvent.KEYCODE_Z:
                if (getCanUndo()) {
                    return onTextContextMenuItem(ID_UNDO);
                }
            case KeyEvent.KEYCODE_Y:
                if (getCanRedo()) {
                    return onTextContextMenuItem(ID_REDO);
                }
            case KeyEvent.KEYCODE_S:
                getMainActivity().saveTheFile(false);
                return true;
            default:
                return super.onKeyDown(keyCode, event);
        }
    } else {
        switch (keyCode) {
            case KeyEvent.KEYCODE_TAB:
                String textToInsert = "  ";
                int start, end;
                start = Math.max(getSelectionStart(), 0);
                end = Math.max(getSelectionEnd(), 0);
                getText().replace(Math.min(start, end), Math.max(start, end),
                        textToInsert, 0, textToInsert.length());
                return true;
            default:
                return super.onKeyDown(keyCode, event);
        }
    }
}
 
Example 20
Source File: TextProcessor.java    From CodeEditor with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.isCtrlPressed()) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_X: // CTRL+X - Cut
                    cut();
                    return true;
                case KeyEvent.KEYCODE_C: // CTRL+C - Copy
                    copy();
                    return true;
                case KeyEvent.KEYCODE_V: // CTRL+V - Paste
                    paste();
                    return true;
                case KeyEvent.KEYCODE_Z: // CTRL+Z - Undo
                    undo();
                    return true;
                case KeyEvent.KEYCODE_Y: // CTRL+Y - Redo
                    redo();
                    return true;
                case KeyEvent.KEYCODE_A: // CTRL+A - Select All
                    selectAll();
                    return true;
                case KeyEvent.KEYCODE_DEL: // CTRL+Delete - Delete Line
                    deleteLine();
                    return true;
                case KeyEvent.KEYCODE_D: // CTRL+D - Duplicate Line
                    duplicateLine();
                    return true;
//                case KeyEvent.KEYCODE_S: // CTRL+S - Save File
//                    codeEditor.saveFile();
//                    return true;
                default:
                    return super.onKeyDown(keyCode, event);
            }
        } else {
            switch (keyCode) {
                case KeyEvent.KEYCODE_TAB: // TAB
                    int start, end;
                    start = Math.max(getSelectionStart(), 0);
                    end = Math.max(getSelectionEnd(), 0);
                    getText().replace(Math.min(start, end),
                            Math.max(start, end), TAB_STR, 0, TAB_STR.length());
                    return true;
                default:
                    try {
                        return super.onKeyDown(keyCode, event);
                    } catch (Exception e) {
                        Logger.error(TAG, e);
                    }
                    return false;
            }
        }
    }