Android KeyEvent - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android KeyEvent class.

This code example shows how to use the following methods: getAction, getRepeatCount, isCanceled
        mDialog.show();
    } 
     
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                Window win = mDialog.getWindow();
                if (win != null) {
                    View decor = win.getDecorView();
                    if (decor != null) {
                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
                        if (ds != null) {
                            ds.startTracking(event, this);
                            return true; 
                        } 
                    } 
                } 
            } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
                Window win = mDialog.getWindow();

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getAction, getKeyCode, isCanceled, isTracking, handleKeyUp
        getAudioManager().preDispatchKeyEvent(event, AudioManager.USE_DEFAULT_STREAM_TYPE);
    } 
 
    public boolean dispatchKeyEvent(KeyEvent event) {
 
        final int action = event.getAction();
        final int keyCode = event.getKeyCode();
 
        if (action == KeyEvent.ACTION_DOWN) {
            return onKeyDown(keyCode, event);
        } else { 
            return onKeyUp(keyCode, event);
        } 
    } 
     
    boolean onKeyDown(int keyCode, KeyEvent event) {
        /* **************************************************************************** 
         * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES. 
         * See the comment in PhoneWindow.onKeyDown 
         * ****************************************************************************/ 

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getAction, getRepeatCount, isCanceled
        mDialog.show();
    } 
     
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                Window win = mDialog.getWindow();
                if (win != null) {
                    View decor = win.getDecorView();
                    if (decor != null) {
                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
                        if (ds != null) {
                            ds.startTracking(event, this);
                            return true; 
                        } 
                    } 
                } 
            } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
                Window win = mDialog.getWindow();

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getAction, getKeyCode
        } 
    } 
 
    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) {
        boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_BACK:
            if (!down) {
                mService.animateCollapsePanels();
            } 
            return true; 
        } 
        return super.dispatchKeyEvent(event);
    } 
 
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept = false;
        if (mNotificationPanel.isFullyExpanded() && mScrollView.getVisibility() == View.VISIBLE) {
Full Snippet Info
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getDevice
@RunWith(TestRunners.WithDefaults.class) 
public class KeyEventTest { 
    @Test 
    public void canSetInputDeviceOnKeyEvent() throws Exception { 
        InputDevice myDevice = ShadowInputDevice.makeInputDeviceNamed("myDevice");
        KeyEvent keyEvent = new KeyEvent(1, 2);
        shadowOf(keyEvent).setDevice(myDevice);
        assertThat(keyEvent.getDevice().getName()).isEqualTo("myDevice");
    } 
} 
Full Snippet Info
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: sendKeySync
        // foreground, and steal UI focus 
        // unfortunately this means the next key event will get directed to the 
        // 'volume adjustment' UI, instead of this test's activity 
        // for this reason this test must be signed with platform certificate, to grant this test 
        // permission to inject key events to another process 
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
                KeyEvent.KEYCODE_VOLUME_DOWN));
        getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
                KeyEvent.KEYCODE_POWER));
    } 
 
    /** 
     * Get the directory where screenshot images are stored. 
     */ 
    private File getScreenshotDir() {
        // TODO: get this dir location from a constant 
        return new File(Environment.getExternalStorageDirectory(), "Pictures" + File.separator +
                "Screenshots"); 
    } 
 
Full Snippet Info
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getAction, getKeyCode
	} 
 
	@Override 
	public boolean dispatchKeyEvent(KeyEvent event) {
 
        int action = event.getAction();
        int keyCode = event.getKeyCode();
 
        if ( action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK && isDrawerOpen() ) {
            closeNavigationDrawer(); 
            return true; 
        } 
 
        if ( readingFragment.dispatchKeyEvent(event) ) {
            return true; 
        } 
 
        return super.dispatchKeyEvent(event);
	} 
 

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getAction, getKeyCode, getMetaState, getUnicodeChar
        super.onDestroy(); 
    } 
 
    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getAction()) {
            case KeyEvent.ACTION_DOWN:
                int uni = event.getUnicodeChar(event.getMetaState());
                return handleKeyDown(event.getKeyCode(), uni);
            case KeyEvent.ACTION_UP:
                return handleKeyUp(event.getKeyCode());
            default: 
                return false; 
        } 
    } 
 
    @Override 
    public void setTitle(CharSequence title) {
        mTitle.setText(title);
    } 
Full Snippet Info
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getKeyCode, isDown
     */ 
    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (DBG) log("dispatchKeyEvent(" + event + ")...");
 
        int keyCode = event.getKeyCode();
        if (mDialer != null) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                case KeyEvent.KEYCODE_CALL:
                    return event.isDown() ? mDialer.onKeyDown(keyCode, event) :
                        mDialer.onKeyUp(keyCode, event);
            } 
        } 
 
        if (DBG) log("==> dispatchKeyEvent: forwarding event to the DTMFDialer");
        return super.dispatchKeyEvent(event);
    } 
 
    private void log(String msg) {
Full Snippet Info
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getAction, getKeyCode, getRepeatCount, isCanceled
		parent_ = (MainActivity)context;
	} 
 
	@Override 
		public boolean dispatchKeyEventPreIme(KeyEvent event) {
			if (parent_ != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
				KeyEvent.DispatcherState state = getKeyDispatcherState();
				if (state != null) {
					if (event.getAction() == KeyEvent.ACTION_DOWN &&
							event.getRepeatCount() == 0) {
						state.startTracking(event, this);
						return true; 
					} else if (event.getAction() == KeyEvent.ACTION_UP &&
							!event.isCanceled() && state.isTracking(event)) {
						parent_.hideScreenKeyboard(0);
						return true; 
					} 
				} 
			} 
			return super.dispatchKeyEventPreIme(event);
Full Snippet Info
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Android KeyEvent Questions & Answers:

Other Android KeyEvent Examples

27:		
28:		@Override
29:		public boolean onKeyDown(int keyCode, KeyEvent event) {
30:		    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
31:		    	webview.goBack();

Read More

2:	editText.setOnEditorActionListener(new OnEditorActionListener() {
3:	    @Override
4:	    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
5:	        boolean handled = false;
6:	        if (actionId == EditorInfo.IME_ACTION_SEND) {

Read More

See Code Examples for Android KeyEvent Methods: