Example usage for android.view KeyEvent getAction

List of usage examples for android.view KeyEvent getAction

Introduction

In this page you can find the example usage for android.view KeyEvent getAction.

Prototype

public final int getAction() 

Source Link

Document

Retrieve the action of this key event.

Usage

From source file:com.appunite.list.AbsHorizontalListView.java

/**
 * Sends a key to the text filter window
 *
 * @param keyCode The keycode for the event
 * @param event The actual key event/* w  w w  .  ja  va 2s.c o m*/
 *
 * @return True if the text filter handled the event, false otherwise.
 */
boolean sendToTextFilter(int keyCode, int count, KeyEvent event) {
    if (!acceptFilter()) {
        return false;
    }

    boolean handled = false;
    boolean okToSend = true;
    switch (keyCode) {
    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:
        okToSend = false;
        break;
    case KeyEvent.KEYCODE_BACK:
        if (mFiltered && mPopup != null && mPopup.isShowing()) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                KeyEvent.DispatcherState state = getKeyDispatcherState();
                if (state != null) {
                    state.startTracking(event, this);
                }
                handled = true;
            } else if (event.getAction() == KeyEvent.ACTION_UP && event.isTracking() && !event.isCanceled()) {
                handled = true;
                mTextFilter.setText("");
            }
        }
        okToSend = false;
        break;
    case KeyEvent.KEYCODE_SPACE:
        // Only send spaces once we are filtered
        okToSend = mFiltered;
        break;
    }

    if (okToSend) {
        createTextFilter(true);

        KeyEvent forwardEvent = event;
        if (forwardEvent.getRepeatCount() > 0) {
            forwardEvent = KeyEvent.changeTimeRepeat(event, event.getEventTime(), 0);
        }

        int action = event.getAction();
        switch (action) {
        case KeyEvent.ACTION_DOWN:
            handled = mTextFilter.onKeyDown(keyCode, forwardEvent);
            break;

        case KeyEvent.ACTION_UP:
            handled = mTextFilter.onKeyUp(keyCode, forwardEvent);
            break;

        case KeyEvent.ACTION_MULTIPLE:
            handled = mTextFilter.onKeyMultiple(keyCode, count, event);
            break;
        }
    }
    return handled;
}

From source file:org.botlibre.sdk.activity.ChatActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    // Remove flag button if a single bot app.
    if (MainActivity.launchType == LaunchType.Bot) {
        //findViewById(R.id.flagButton).setVisibility(View.GONE);
    }/*from   w w  w  .  j  a v a2  s  .  c  o  m*/

    //permission required.
    ActivityCompat.requestPermissions(ChatActivity.this, new String[] { Manifest.permission.RECORD_AUDIO }, 1);

    //set/Save the current volume from the device.
    setStreamVolume();
    //Music Volume is Enabled.
    muteMicBeep(false);

    //For "scream" issue
    micLastStat = MainActivity.listenInBackground;

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    this.instance = (InstanceConfig) MainActivity.instance;
    if (this.instance == null) {
        return;
    }
    /*if (MainActivity.showAds) {
         AdView mAdView = (AdView) findViewById(R.id.adView);
         AdRequest adRequest = new AdRequest.Builder().build();
         mAdView.loadAd(adRequest);
    } else {
       AdView mAdView = (AdView) findViewById(R.id.adView);
       mAdView.setVisibility(View.GONE);
    }*/

    setTitle(this.instance.name);
    ((TextView) findViewById(R.id.title)).setText(this.instance.name);
    HttpGetImageAction.fetchImage(this, this.instance.avatar, findViewById(R.id.icon));
    ttsInit = false;
    tts = new TextToSpeech(this, this);

    if (!MainActivity.handsFreeSpeech) {
        setMicIcon(false, false);
    } else if (!MainActivity.listenInBackground) {
        setMicIcon(false, false);
    }

    //Last time will be saved for the MIC.
    if (MainActivity.listenInBackground && MainActivity.handsFreeSpeech) {
        microphoneThread(thread);
    }

    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);
    //scrollVie added and stuff
    scrollView = findViewById(R.id.chatList);
    menuMLayout = (LinearLayout) findViewById(R.id.menuMLayout);
    chatCLayout = (LinearLayout) findViewById(R.id.chatCLayout);
    responseLayout = (LinearLayout) findViewById(R.id.responseLayout);
    chatToolBar = (LinearLayout) findViewById(R.id.chatToolBar);

    videoView = (VideoView) findViewById(R.id.videoView);
    resetVideoErrorListener();
    videoError = false;

    imageView = (ImageView) findViewById(R.id.imageView);
    videoLayout = findViewById(R.id.videoLayout);

    textView = (EditText) findViewById(R.id.messageText);
    textView.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            submitChat();
            return false;
        }
    });

    if (MainActivity.translate) {
        findViewById(R.id.yandex).setVisibility(View.VISIBLE);
    } else {
        findViewById(R.id.yandex).setVisibility(View.GONE);
    }

    Spinner emoteSpin = (Spinner) findViewById(R.id.emoteSpin);
    emoteSpin.setAdapter(
            new EmoteSpinAdapter(this, R.layout.emote_list, Arrays.asList(EmotionalState.values())));

    ListView list = (ListView) findViewById(R.id.chatList);
    list.setAdapter(new ChatListAdapter(this, R.layout.chat_list, this.messages));
    list.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

    ImageButton button = (ImageButton) findViewById(R.id.speakButton);
    button.setOnClickListener(new View.OnClickListener() {
        @TargetApi(23)
        @Override
        public void onClick(View v) {
            if (MainActivity.handsFreeSpeech) {
                //set the current volume to the setting.
                setStreamVolume();
                //if its ON Or OFF - Switching back and forth 
                MainActivity.listenInBackground = !MainActivity.listenInBackground;

                //saving the boolean data of MainActivity.listeningInBackground
                SharedPreferences.Editor cookies = MainActivity.current.getPreferences(Context.MODE_PRIVATE)
                        .edit();
                cookies.putBoolean("listenInBackground", MainActivity.listenInBackground);
                cookies.commit();
                if (MainActivity.listenInBackground) {
                    micLastStat = true;
                    try {
                        microphoneThread(thread);
                    } catch (Exception ignore) {
                    }
                    beginListening();
                } else {
                    micLastStat = false;
                    microphoneThread(thread);
                    stopListening();
                }
            } else {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, MainActivity.voice.language);
                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    textView.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "Your device doesn't support Speech to Text", Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        }
    });

    //adding functionality on clicking the image  
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (stateLayouts == 4) {
                stateLayouts = 0;
            }
            switch (stateLayouts) {
            case 0:
                scrollView.setVisibility(View.VISIBLE);
                chatCLayout.setVisibility(View.VISIBLE);
                menuMLayout.setVisibility(View.VISIBLE);
                responseLayout.setVisibility(View.VISIBLE);
                chatToolBar.setVisibility(View.VISIBLE);
                break;
            case 1:
                scrollView.setVisibility(View.GONE);
                break;
            case 2:
                responseLayout.setVisibility(View.GONE);
                chatToolBar.setVisibility(View.GONE);
                break;
            case 3:
                menuMLayout.setVisibility(View.GONE);
                chatCLayout.setVisibility(View.GONE);
                break;
            }
            stateLayouts++;
        }
    });

    //adding functionality on clicking the image 
    videoLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (stateLayouts == 4) {
                stateLayouts = 0;
            }
            switch (stateLayouts) {
            case 0:
                scrollView.setVisibility(View.VISIBLE);
                chatCLayout.setVisibility(View.VISIBLE);
                menuMLayout.setVisibility(View.VISIBLE);
                responseLayout.setVisibility(View.VISIBLE);
                chatToolBar.setVisibility(View.VISIBLE);
                break;
            case 1:
                scrollView.setVisibility(View.GONE);
                break;
            case 2:
                responseLayout.setVisibility(View.GONE);
                chatToolBar.setVisibility(View.GONE);
                break;
            case 3:
                menuMLayout.setVisibility(View.GONE);
                chatCLayout.setVisibility(View.GONE);
                break;
            }
            stateLayouts++;
        }
    });

    GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTapEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                boolean isVideo = !MainActivity.disableVideo && !videoError && response != null
                        && response.isVideo();
                View imageView = findViewById(R.id.imageView);
                View videoLayout = findViewById(R.id.videoLayout);
                if (imageView.getVisibility() == View.VISIBLE) {
                    imageView.setVisibility(View.GONE);
                } else if (!isVideo) {
                    imageView.setVisibility(View.VISIBLE);
                }
                if (videoLayout.getVisibility() == View.VISIBLE) {
                    videoLayout.setVisibility(View.GONE);
                } else if (isVideo) {
                    videoLayout.setVisibility(View.VISIBLE);
                }
                return true;
            }
            return false;
        }
    };
    final GestureDetector detector = new GestureDetector(this, listener);
    findViewById(R.id.chatList).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return detector.onTouchEvent(event);
        }
    });

    listener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTapEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                View avatarLayout = findViewById(R.id.avatarLayout);
                if (avatarLayout.getVisibility() == View.VISIBLE) {
                    avatarLayout.setVisibility(View.GONE);
                } else {
                    avatarLayout.setVisibility(View.VISIBLE);
                }
                return true;
            }
            return false;
        }
    };
    final GestureDetector detector2 = new GestureDetector(this, listener);
    /*findViewById(R.id.responseText).setOnTouchListener(new View.OnTouchListener() {         
       @Override
       public boolean onTouch(View v, MotionEvent event) {
    return detector2.onTouchEvent(event);
       }
    });*/
    WebView responseView = (WebView) findViewById(R.id.responseText);
    responseView.getSettings().setJavaScriptEnabled(true);
    responseView.getSettings().setDomStorageEnabled(true);
    responseView.addJavascriptInterface(new WebAppInterface(this), "Android");

    findViewById(R.id.responseImageView).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View avatarLayout = findViewById(R.id.avatarLayout);
            if (avatarLayout.getVisibility() == View.VISIBLE) {
                avatarLayout.setVisibility(View.GONE);
            } else {
                avatarLayout.setVisibility(View.VISIBLE);
            }
        }
    });

    HttpGetImageAction.fetchImage(this, instance.avatar, this.imageView);
    HttpGetImageAction.fetchImage(this, instance.avatar, (ImageView) findViewById(R.id.responseImageView));

    final ChatConfig config = new ChatConfig();
    config.instance = instance.id;
    config.avatar = this.avatarId;
    if (MainActivity.translate && MainActivity.voice != null) {
        config.language = MainActivity.voice.language;
    }
    if (MainActivity.disableVideo) {
        config.avatarFormat = "image";
    } else {
        config.avatarFormat = MainActivity.webm ? "webm" : "mp4";
    }
    config.avatarHD = MainActivity.hd;
    config.speak = !MainActivity.deviceVoice;
    // This is required because of a bug in TextToSpeech that prevents onInit being called if an AsynchTask is called...
    Thread thread1 = new Thread() {
        public void run() {
            for (int count = 0; count < 5; count++) {
                if (ttsInit) {
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception exception) {
                }
            }
            HttpAction action = new HttpChatAction(ChatActivity.this, config);
            action.execute();
        }
    };
    thread1.start();
}

From source file:com.processing.core.PApplet.java

protected void handleKeyEvent(KeyEvent event) {
    //    keyEvent = event;
    key = event.getKey();//from   w w  w .  j  av a 2  s . co m
    keyCode = event.getKeyCode();

    switch (event.getAction()) {
    case KeyEvent.PRESS:
        keyPressed = true;
        keyPressed(event);
        break;
    case KeyEvent.RELEASE:
        keyPressed = false;
        keyReleased(event);
        break;
    }

    handleMethods("keyEvent", new Object[] { event });
}

From source file:processing.core.PApplet.java

protected void handleKeyEvent(KeyEvent event) {
    // event.isPrintingKey() returns false for whitespace and others,
    // which is a problem if the space bar or tab key are used.
    key = (char) event.getUnicodeChar();
    // if not mappable to a unicode character, instead mark as coded key
    if (key == 0 || key == 0xFFFF) {
        // System.out.println("  key is coded");
        key = CODED;//w  w w  . j a  v a2s  . co  m
        // } else {
        // System.out.println("  key is unicode");
    }

    keyCode = event.getKeyCode();
    // println(key + " " + keyCode);

    int action = event.getAction();
    if (action == KeyEvent.ACTION_DOWN) {
        keyPressed = true;
        keyPressed();
    } else if (action == KeyEvent.ACTION_UP) {
        keyPressed = false;
        keyReleased();
    }

    // if someone else wants to intercept the key, they should
    // set key to zero (or something besides the "ESC").
    // println(event);
    /*
    if (action == KeyEvent.ACTION_DOWN) {        
       if (keyCode == KeyEvent.KEYCODE_BACK) {
    // println("KEYCODE_BACK, calling exit()");
    Preferences.saveRestart(getActivity(), true);
    exit();
    System.exit(0);
       }
    }
    */
    // When running tethered to the Processing application, respond to
    // Ctrl-W (or Cmd-W) events by closing the sketch. Disable this behavior
    // when running independently, because this sketch may be one component
    // embedded inside an application that has its own close behavior.
    // if (external &&
    // event.getModifiers() == MENU_SHORTCUT &&
    // event.getKeyCode() == 'W') {
    // exit();
    // }
    // }
}

From source file:com.processing.core.PApplet.java

protected void nativeKeyEvent(android.view.KeyEvent event) {
    // event.isPrintingKey() returns false for whitespace and others,
    // which is a problem if the space bar or tab key are used.
    char key = (char) event.getUnicodeChar();
    // if not mappable to a unicode character, instead mark as coded key
    if (key == 0 || key == 0xFFFF) {
        key = CODED;//  ww w.  j a  v a2s.com
    }

    int keyCode = event.getKeyCode();

    int keAction = 0;
    int action = event.getAction();
    if (action == android.view.KeyEvent.ACTION_DOWN) {
        keAction = KeyEvent.PRESS;
    } else if (action == android.view.KeyEvent.ACTION_UP) {
        keAction = KeyEvent.RELEASE;
    }

    // TODO set up proper key modifier handling
    int keModifiers = 0;

    KeyEvent ke = new KeyEvent(event, event.getEventTime(), keAction, keModifiers, key, keyCode);

    postEvent(ke);
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public PeerComponent createBrowserComponent(final Object parent) {
    if (getActivity() == null) {
        return null;
    }//w  ww  .  ja va 2  s. c o  m
    final AndroidImplementation.AndroidBrowserComponent[] bc = new AndroidImplementation.AndroidBrowserComponent[1];
    final Throwable[] error = new Throwable[1];
    final Object lock = new Object();

    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {

            synchronized (lock) {
                try {
                    WebView wv = new WebView(getActivity()) {

                        public boolean onKeyDown(int keyCode, KeyEvent event) {
                            switch (keyCode) {
                            case KeyEvent.KEYCODE_BACK:
                                Display.getInstance().keyPressed(AndroidImplementation.DROID_IMPL_KEY_BACK);
                                return true;
                            case KeyEvent.KEYCODE_MENU:
                                //if the native commands are used don't handle the keycode
                                if (Display.getInstance()
                                        .getCommandBehavior() != Display.COMMAND_BEHAVIOR_NATIVE) {
                                    Display.getInstance().keyPressed(AndroidImplementation.DROID_IMPL_KEY_MENU);
                                    return true;
                                }
                            }
                            return super.onKeyDown(keyCode, event);
                        }

                        public boolean onKeyUp(int keyCode, KeyEvent event) {
                            switch (keyCode) {
                            case KeyEvent.KEYCODE_BACK:
                                Display.getInstance().keyReleased(AndroidImplementation.DROID_IMPL_KEY_BACK);
                                return true;
                            case KeyEvent.KEYCODE_MENU:
                                //if the native commands are used don't handle the keycode
                                if (Display.getInstance()
                                        .getCommandBehavior() != Display.COMMAND_BEHAVIOR_NATIVE) {
                                    Display.getInstance().keyPressed(AndroidImplementation.DROID_IMPL_KEY_MENU);
                                    return true;
                                }
                            }
                            return super.onKeyUp(keyCode, event);
                        }
                    };
                    wv.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                            case MotionEvent.ACTION_UP:
                                if (!v.hasFocus()) {
                                    v.requestFocus();
                                }
                                break;
                            }
                            return false;
                        }
                    });
                    wv.getSettings().setDomStorageEnabled(true);
                    wv.requestFocus(View.FOCUS_DOWN);
                    wv.setFocusableInTouchMode(true);
                    bc[0] = new AndroidImplementation.AndroidBrowserComponent(wv, getActivity(), parent);
                    lock.notify();
                } catch (Throwable t) {
                    error[0] = t;
                    lock.notify();
                }
            }
        }
    });
    while (bc[0] == null && error[0] == null) {
        Display.getInstance().invokeAndBlock(new Runnable() {
            public void run() {
                synchronized (lock) {
                    if (bc[0] == null && error[0] == null) {
                        try {
                            lock.wait(20);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }

        });
    }
    if (error[0] != null) {
        throw new RuntimeException(error[0]);
    }
    return bc[0];
}