Asked  7 Months ago    Answers:  5   Viewed   29 times

What is the difference between these three events? Upon googling I found that:

  • The onKeyDown event is triggered when the user presses a key.
  • The onKeyUp event is triggered when the user releases a key.
  • The onKeyPress event is triggered when the user presses & releases a key (onKeyDown followed by onKeyUp).

I understand the first two, but isn't onKeyPress the same as onKeyUp? Is it possible to release a key (onKeyUp) without pressing it (onKeyDown)?

This is a bit confusing, can someone clear this up for me?



Check here for the archived link originally used in this answer.

From that link:

In theory, the onKeyDown and onKeyUp events represent keys being pressed or released, while the onKeyPress event represents a character being typed. The implementation of the theory is not same in all browsers.

Tuesday, June 1, 2021
answered 7 Months ago

This works for me in Firefox:

var DOMContentLoaded_event = document.createEvent("Event")
DOMContentLoaded_event.initEvent("DOMContentLoaded", true, true)
Saturday, July 3, 2021
answered 6 Months ago

Update 23 August

This has been fixed again in the v23.0.0 of appcompat-v7 support library. Update to the last version to see this fixed.

Update 19 July

Unfortunately AppCompat v22.2.1 broke the onKeyDown and onKeyUp events again. I just updated AppCompatActivityMenuKeyInterceptor to support v22.1.x and also v22.2.1

Update 29 May

This has been fixed in the v22.2.0 of appcompat-v7 support library. Update to the last version to see this fixed.

Unfortunately AppCompat v22.1.0 intercepts the onKeyDown and onKeyUp events and does not propagate them when the menu key is pressed. The only possible solution involves using Reflection to intercept the onKeyDown and onKeyUp events before the AppCompat does.

Add this class to your project:

public class AppCompatActivityMenuKeyInterceptor {

    private static final String FIELD_NAME_DELEGATE = "mDelegate";
    private static final String FIELD_NAME_WINDOW = "mWindow";

    public static void intercept(AppCompatActivity appCompatActivity) {
        new AppCompatActivityMenuKeyInterceptor(appCompatActivity);

    private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) {
        try {
            Field mDelegateField = AppCompatActivity.class.getDeclaredField(FIELD_NAME_DELEGATE);
            Object mDelegate = mDelegateField.get(activity);

            Class mDelegateClass = mDelegate.getClass().getSuperclass();
            Field mWindowField = null;

            while (mDelegateClass != null) {
                try {
                    mWindowField = mDelegateClass.getDeclaredField(FIELD_NAME_WINDOW);
                } catch (NoSuchFieldException ignored) {

                mDelegateClass = mDelegateClass.getSuperclass();

            if (mWindowField == null)
                throw new NoSuchFieldException(FIELD_NAME_WINDOW);

            Window mWindow = (Window) mWindowField.get(mDelegate);

            Window.Callback mOriginalWindowCallback = mWindow.getCallback();
            mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity));
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {

    private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper {

        private WeakReference<AppCompatActivity> mActivityWeak;

        public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) {

            mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity);

        public boolean dispatchKeyEvent(KeyEvent event) {
            final int keyCode = event.getKeyCode();

            AppCompatActivity appCompatActivity = mActivityWeak.get();

            if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) {
                if (appCompatActivity.dispatchKeyEvent(event))
                    return true;

            return super.dispatchKeyEvent(event);

Call AppCompatActivityMenuKeyInterceptor.intercept(this) in the onCreate of your activity:

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        //Initialize the interceptor

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Now onKeyDown is called also for KEYCODE_MENU
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            //do your stuff

            //return false if you want to propagate the
            //KeyEvent to AppCompat, return true otherwise
            return false;

        return super.onKeyDown(keyCode, event);

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // Now onKeyUp is called also for KEYCODE_MENU
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            //do your stuff

            //return false if you want to propagate the
            //KeyEvent to AppCompat, return true otherwise
            return false;

        return super.onKeyUp(keyCode, event);

If you use ProGuard or DexGuard add these rules to your configuration:

-keepclassmembers class {
    private mDelegate;

-keepclassmembers class {
    final android.view.Window mWindow;

Now your activity can receive onKeyDown and onKeyUp event also for the menu key.

Tuesday, July 20, 2021
answered 5 Months ago


lastEvent = void 0;

heldKeys = {};

window.onkeydown = function(event) {
    event = event || window.event; //IE does not pass the event object
    if (lastEvent && lastEvent.keyCode === event.keyCode) {
    lastEvent = event;
    heldKeys[event.keyCode] = true;

    var keyCode = event.which || event.keyCode; //key property also different

    switch (keyCode) {
        case 80:
            return myamp.userInput("positiv");
        case 81:
            return myamp.userInput("negativ");

window.onkeyup = function(event) {
    event = event || window.event;
    lastEvent = null;
    return delete heldKeys[event.keyCode];
Wednesday, November 17, 2021
answered 3 Weeks ago

jQuery .change() is an alias for native change event.

The change event is fired for <input>, <select>, and <textarea> elements when a change to the element's value is committed by the user. Unlike the input event, the change event is not necessarily fired for each change to an element's value.

You can use it fairly simple:

// Non-obtrusive JavaScript example(preffered).    
element.addEventListener('change', callback, false);

// Somewhat obtrusive (not recommended).
element.onchange = function () { ... };

// Obtrusive JavaScript in HTML (not recommended).
<input type="text" onchange="function() { ... };">
Sunday, December 5, 2021
Craig Bovis
answered 4 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :