Android - Get keyboard key press - java

I want to catch the press of any key of the softkeyboard. I don't want a EditView or TextView in my Activity, the event must be handled from a extended View inside my Activity.
I just tried this:
1) Override the onKeyUp(int keyCode, KeyEvent event) Activity method. This don't work with softkeyboard, it just catch few hardkeyboard.
2) Create my OnKeyListener and register that in my View that contains a registered and working OnTouchListener. This doesn't work at all with softkeyboard.
3) Override the onKeyUp(int keyCode, KeyEvent event) View method. This not work at all neither if I set my OnKeyListener nor if I don't set it.
4) With the InputMethodManager object Call the method showSoftInput and passing it my View. This don't work neither for raise up the keyboard, indeed i have to call toggleSoftInput; nor to catch the key events.
I tested all only in the emulator but i think it's enough. Why it's so complicate take a simple key event from a keyboard ?

For handling hardware keys and Back key you could use dispatchKeyEvent(KeyEvent event) in your Activity
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
UPD: unfortunately you can't handle soft keyboard events (see Handle single key events), unless you develop your own custom keyboard (follow the link to learn how Creating input method).

With the hint of vasart i can get the KeyPress event. To make the keycode printable i have used the function getUnicodeChar passing it the meta button state then just a char cast solve the problem.
This is the working code:
#Override
public boolean dispatchKeyEvent(KeyEvent KEvent)
{
int keyaction = KEvent.getAction();
if(keyaction == KeyEvent.ACTION_DOWN)
{
int keycode = KEvent.getKeyCode();
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
char character = (char) keyunicode;
System.out.println("DEBUG MESSAGE KEY=" + character + " KEYCODE=" + keycode);
}
return super.dispatchKeyEvent(KEvent);
}
Of course this work only with ASCII character.

There are no option to handling key press events on soft keyboard (an on-screen keyboard) only from a hardware keyboard.
for more details: Handling Keyboard Actions
Note: When handling keyboard events with the KeyEvent class and
related APIs, you should expect that such keyboard events come only
from a hardware keyboard. You should never rely on receiving key
events for any key on a soft input method (an on-screen keyboard).

When Keyboard is opened in activity your activity actually becomes foreground...
All TextArea or TextFields have their own mechanism to get keypressed from onScreen keyboard...
if you want to use onKeyDown() listner for virtual keyboard make sure that you set in AndroidManifest File under your activity android:windowSoftInputMode="stateAlwaysVisible" then onkeyDown() will work it did worked for me ...

Related

Remap physical back button for "select_button" in android tv webvew application with java

I'm a french developer , i'm creating an android tv application about cloud Gaming in a webview with java.
My application start a gaming stream direcly in the webview, on fullscreen, the physical buttons of controlers are working with the game, eccept the "view button" the "back button'.
This "view button" or if your prefer "select button" is for android tv a back button to the homescreen. So i have to overiding this back button, and i want replace it by a "select button" that it can interact with the games for displaying maps and inventory like in rpg games.
I know that the name will be "button_select" for interact with the pc game. So in android tv for now i will always redirected to the home page.
this is a sample of my code.
public class MainActivity extends AppCompatActivity {
#Override
public void onBackPressed() {
return;
}
With this overide, the back button is completly disabled for now, i want replace it or call the "button_select". I readed something about "handler" perhaps this is the solution.
Edit 16/06/2021
I tested much coded, but nothing work.
this one
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
}
return super.onKeyDown(keyCode, event);
}
And its variants
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (action == KeyEvent.ACTION_DOWN ){
//Do something in the back button
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
In game, the back button is disabled but, the" keycode_button_select "not interact with the game. There is nothing. I test with the "keycode button_start" the same things.
I tested the app button mapper for android tv, there is an option for custom keycodes with adb, and nothing is working in game.
https://play.google.com/store/apps/details?id=flar2.homebutton&hl=fr&gl=US
So by the code or by an app, nothing is working for now, i don't want to forcing the "root mode" for this things. Perhaps i'll just have to implementing a gamepad Plugin in java. I don't know...
Edit 18/06/21
I can capture the "back button" and display a dialog alert that appear on middle of the screen. But the actions after has no effect for now. I tested "dispatchEvent" with no success for now. I will testing the functions "robots". Perhaps "robot" and "dispatchEvent" will working together.
Thank you for your help.
I am assuming that you want to call button_select key when user presses back button.
In this case, create a method to call upon pressing back button. And call that method from the onBackPressed method.
For example:
public void callButtonSelectKey() {
//This could be done in either ways.
//1.
val i = Instrumentation()
i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
//2.
val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
dispatchKeyEvent(keyEventDown)
dispatchKeyEvent(keyEventUp)
}
#Override
public void onBackPressed() {
methodToCallUponBackPress();
return;
}
Now when the user presses the back button, it will call callButtonSelectKey method invoke "button_select" key. (i.e. remapped backbutton to button_select)
Let me know if you have any more questions.
EDIT
In an Object Oriented Language, overriding the class's (in this case KeyEvent) field (in this case, keycode) will do nothing. Like the code below:
//Do something in the back button
keyCode = KeyEvent.KEYCODE_BUTTON_SELECT;
To call button_select when you receive the event of KEYCODE_BACK, you should manually invoke the method to call Key Event, in this case the method I gave you earlier dispatchKeyEvent .
So you should make your change into something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
//This could be done in either ways.
//1.
val i = Instrumentation()
i.sendKeyDownUpSync(KeyEvent.KEYCODE_BUTTON_SELECT)
//2.
val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_SELECT)
val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_SELECT)
dispatchKeyEvent(keyEventDown)
dispatchKeyEvent(keyEventUp)
}
return super.onKeyDown(keyCode, event); //Edit this return statement if you want to ignore a keypress.
}

Capture done action on softkeyboard without edittext?

In my app I have a webview that displays some content. One of these screen has a text box to fill in. I want to capture when the user presses the done button on the keyboard but there's no edit text to add a listener too. How can I capture the action regardless of device & keyboard?
I've tried these with little luck.
EditText with textPassword inputType, but without Softkeyboard
android: Softkeyboard perform action when Done key is pressed
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// code here
break;
default:
return super.onKeyUp(keyCode, event);
}
return true;
}
My class overrides KeyEvent.Callback but the above funtion onKeyDown is never called.
Create a custom webview, override onCreateInputConnection to set ime option and input type to keyboard, override dispatchKeyEvent to get the key events filter it out
Example :
class MyWeb#JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0) : WebView(context, attrs, defStyleAttr) {
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
val inputConnection = BaseInputConnection(this, false)
return inputConnection
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
super.dispatchKeyEvent(event)
val dispatchFirst = super.dispatchKeyEvent(event)
if (event.action == KeyEvent.ACTION_UP) {
when (event.keyCode) {
KeyEvent.KEYCODE_ENTER -> {
Toast.makeText(context,"Hii",Toast.LENGTH_LONG).show()
//callback?.onEnter()
}
}
}
return dispatchFirst
}
}
and XML
<com.example.MyWeb
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/web"
/>`
Source : https://medium.com/#elye.project/managing-keyboard-on-webview-d2e89109d106
Key events are almost never sent from a soft keyboard, they use more direct methods.
The way a keyboard on Android works is its bound to a view. That view has to implement getInputConnection() returning an object that will allow functions to be called (via AIDL) by the keyboard app. One of these functions is called for the "action key" (the done button). In the default InputConnection implementation, that will call a listener registered to the bound view.
Since you're dealing with a webview here- I don't think there is a way to capture it directly. What you can try is to subclass WebView to ActionKeyWebView. Add a function to register an action key listener interface.
Override getInputConnection to return your own InputConnectionWrapper subclass, and wrap super.getInputConnection(). THen override performEditorAction to call any listener registered for the webview. Its a fair amount of code but it should work.

How to Completely disable keyboard when using EditText in android?

I have 5 EditTexts and I type in them using 5 buttons, so I don't need the keyboard.
How can I disable it completely, even when I click on the EditText? On EditText click I want only to focus it so that it has the cursor on. (I implemented everything so that when I click one of the 5 buttons, the focus goes on the next EditText).
The problem is that when I click on an EditText the keyboard pops up. I think I have to do it in java. Thank you!!!
MyEditor.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch event
}
});
Try :
editText.setShowSoftInputOnFocus(false);
editText.setInputType(InputType.TYPE_NULL);
editText.setFocusable(false);

TextView editable onLongClick -- But one small issue when BACK button pressed

I have been search SO for days and have finally compiled enough answers to accomplish what I wanted. First off, it seems to be an often asked question but not really answered (at least not the way I was looking for it). I thought I would share my findings but I also have one small issue left that I would like to ask for help with. Here goes:
I have a TextView which displays a score. It starts at 0 and at an onClick event the score increments and updates the TextView (score is tracked as a byte - valScore).
onLongClick: This was the challenge. I want the user to be able to do a LongClick to correct/change the score. I first found a solution that utilized another layout.xml file with just an EditText element and the OK and CANCEL buttons. This was very cumbersome to change the score as it involved the LongClick, then the dialog opens, then you had to click on the EditText element to open the keyboard, then you enter the value, click DONE and then click OK. I shortened it by figuring out how to open the software keyboard automatically when the dialog opened. However, you still had to click DONE and then OK. I didn't like this action so I continued searching.
Days later I came up with a bit of code and then more and with a lot of playing/hacking around I came up with the following solution:
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
tvScoreHome.setInputType( InputType.TYPE_CLASS_NUMBER );
tvScoreHome.setFocusable(true);
tvScoreHome.setFocusableInTouchMode( true );
tvScoreHome.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(tvScoreHome, InputMethodManager.SHOW_FORCED);
tvScoreHome.setText("");
tvScoreHome.setOnEditorActionListener( new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
valScoreHome = Byte.valueOf( tvScoreHome.getText().toString() );
// This part will hide the keyboard after input
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
tvScoreHome.setFocusable( false );
tvScoreHome.setFocusableInTouchMode( false );
tvScoreHome.setText( Byte.toString(valScoreHome) );
return true;
}
return false;
}
});
return true;
}
});
This works EXACTLY how I want. User performs LongClick the keyboard opens, the user enters the new value and clicks DONE. The TextView is updated and it works great!
The problem arises if the user changes their mind and hits the BACK button on the device. The keyboard closes (GOOD), but then the focus remains on the TextView instead of removing the focus like I do if the DONE button is pressed. So if you cancel out of a change every click after that results in the keyboard opening again instead of just incrementing the score -- until you actually type a value into the keyboard and click DONE (then the regular behavior takes over again. I need to setFocusableInTouchMode to FALSE if the BACK button is pressed.
The other issue is that the setText() method is executed even if the BACK button is pressed if a different value has been typed in. Even though valScoreHome isn't updated the TextView changes. On the next increment it goes to the correct number again, but the setText() should not execute if the BACK button is pressed.
Can someone help me figure this out please?
Both issues can be handled by subclassing TextView.
The back button press that closes the keyboard is handled by overriding onKeyPreIme.
To avoid updating the text when the user closes the keyboard, the score value is saved in the variable mScore, but only if the TextView is currently not focusable. That means, the TextView "remembers" the current value of the score, that was not entered by the user. When the user closes the the keyboard, the text is set back to the saved value.
public class ScoreTextView extends TextView {
private CharSequence mScore;
public ScoreTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setText(CharSequence text, BufferType type) {
if (!isFocusable()) {
mScore = text;
}
super.setText(text, type);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
setFocusable(false);
setFocusableInTouchMode(false);
setText(mScore);
}
return super.onKeyPreIme(keyCode, event);
}
}

Using the Android hardware side button

I am working with a Motorola Droid. I can see there is a side button which launches the camera.
In my app, I use a button which when pressed, allows the user to talk - like in a walkie-talkie i.e it is a push-to-talk button.
How do I use the side button to work like this button? i.e use it as a push-to-talk button.
Any help would be appreciated.
Thanks
Your activity can override onKeyDown() and watch for KEYCODE_CAMERA KeyEvents:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_CAMERA) {
// do something
return(true);
}
return(super.onKeyDown(keyCode, event));
}

Categories