I'm trying to authenticate users in my app by their keystrokes on the soft keyboard, I want to get:
The key release and press.
the pressure on the key.
the finger area on the key.
I used the onKeyUp and onKeyDown but It doesn"t work.
So how can I get these events from the soft keyboard.
Instead of using the onKeyDown from the view, override dispatchKeyEvent at the activity level. This will handle your key event before it gets to the window manager, so make sure to call super on any key events you do not explicitly handle. Example using the ACTION_DOWN (as every event has both ACTION_UP and ACTION_DOWN)
#Override
public boolean dispatchKeyEvent(KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_UP) {
return super.dispatchKeyEvent(event);
}
if (keyCode == KeyEvent.KEYCODE_DEL) {
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
} else {
return super.dispatchKeyEvent(event);
}
}
Related
I am developing a Lock Screen App that will lock the screen as well as apps that the user want to.
Lock can be pin code or pattern lock and so far now when I activate a pin code or pattern lock then a lock screen appears on the screen when user Reboot or off the screen, the problem I am facing right now is that, I can't disable my Home button and Task manager button.
What I have tried soo far is
I have added the
getWindow().addFlags(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
after adding this, Home button and Task Manager button is still enabled and my screen freezes, if I touch something on the screen its not performing anything and when I press Home button the lock screen disappear.
I also have added this too
#Override
public void onBackPressed() {
return;
}
// Handle button clicks
#Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (keyCode == KeyEvent.KEYCODE_POWER)
|| (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
|| (keyCode == KeyEvent.KEYCODE_CAMERA)) {
return true;
}
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
return true;
}
return false;
}
// handle the key press events here itself
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
|| (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
return false;
}
if ((event.getKeyCode() == KeyEvent.KEYCODE_HOME)) {
return true;
}
return false;
}
after adding this, back button is disabled successfully working but Home button is still enabled and funtioning.
I have a custom device with a custom button and I need to handle an hardware Button Events/Intents:
every time I press the button it generates a PTT Press Action and I need to open my custom application, is there a way to do this?
If by custom device, you mean custom AOSP. Then make sure, it's button bound to events, this should be done with low level device driver configuration. And it's complicated work. Next, make sure you receive your click in next events.
Try to either use dispatch key event on Activity work:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
On on key event with Android View which is in focus.
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}
I want to handle the click on the "ok" key of the onscreen keyboard. For that purpose I added a KeyListener to the text field:
textField = (EditText) view.findViewById(R.id.text_field);
textField.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
boolean handled = false;
if (keyCode == KeyEvent.KEYCODE_ENTER) {
okPressed(view);
handled = true;
}
return handled;
}
});
And in the okPressed method I'm checking the content:
private void okPressed(View view) {
String value = textField.getText().toString().trim();
if (value.equals("")) {
Toast.makeText(view.getContext(), "Error", Toast.LENGTH_SHORT).show();
return;
}
}
And now for the case that my text field is not empty, everything works fine. But in the case the field contains no text, my okPressed method is executed twice. But why?
Per KeyEvent's documentation:
Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN.
The last key event is a ACTION_UP for the key up.
You should check the result of getAction() to filter for only the key action you want (i.e., ACTION_UP if you only want to trigger when the user releases or ACTION_DOWN if you want to trigger as soon as they touch the button).
Trying to get functionality for an image preview working, where holding the finger over a thumbnail displays the full image. Releasing the finger will make the full image disappear.
This was easy enough to implement, but if the user moves their finger off the ImageView before release, the ACTION_UP will not fire.
#Override
public boolean onTouch(View v, MotionEvent e)
{
if (v.getId() == R.id.gallery_iv_pose_thumb)
{
// This works fine
if (e.getAction() == MotionEvent.ACTION_DOWN)
{
preview.setImageBitmap(galleryAdapter.getGalleryItemFromChildView(v).getImage());
preview.setVisibility(View.VISIBLE);
return true;
}
// This only fires if a finger is released over the initial view
else if (e.getAction() == MotionEvent.ACTION_UP)
{
preview.setVisibility(View.INVISIBLE);
return true;
}
}
I attempted to simply listen to the ACTION_UP event for the entire layout, but this requires to have handled the ACTION_DOWN for the layout as well... which stops me from listening for the ACTION_DOWN event for the thumbnail.
Any help would be appreciated!
If you move outside of the bounds, ACTION_CANCEL will be called.
So this should work:
else if (e.getAction() == MotionEvent.ACTION_UP || e.getAction() == MotionEvent.ACTION_CANCEL)
I want to stop media sound when the user press hold key or power key
I do this
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENDCALL || keyCode == KeyEvent.KEYCODE_POWER){
pauseSound();
}
return super.onKeyDown(keyCode, event);
}
But this override function didn't invoke when Power or end call is pressed
Any idea?
Thanks it solved, Ovveride EndCall & Power is forbidden for security reasons, I overrided "OnPause" (of activity) and it works good, Thanks again