I have an physical keyboard connected to my android device. I have an application with two buttons and the following two functions handle the KeyEvent:
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(state==State.INI){
char unicodeChar = (char)event.getUnicodeChar();
Log.d("CHAR", "UP: "+Character.toString(unicodeChar)+" - "+Integer.toString(keyCode));
}
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(state==State.INI){
char unicodeChar = (char)event.getUnicodeChar();
Log.d("CHAR", "DOWN: "+Character.toString(unicodeChar)+" - "+Integer.toString(keyCode));
}
return true;
}
but even it handle the ENTER key doesn't prevent it to be handled by the system :s Per example, i changed the return true to false and i can navigate with the arrow keys between the buttons so with return true it is working for almost every key except ENTER :s
Resolved with:
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Log.d("CHAR","YOU CLICKED ENTER KEY");
return false;
}
return super.dispatchKeyEvent(e);
};
Related
I am trying to add shortcuts in my app when external keyboard is connected. SHIFT+KEY, this combination is triggering the following event. But Control+KEY and Control key events are not triggered. Can anyone suggest me on this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT) {
flag_sellreceipt = KeyEvent.isModifierKey(KeyEvent.KEYCODE_CTRL_LEFT);
int i = KeyEvent.META_CTRL_LEFT_ON;
System.out.println("ctrl is pressed");
}
return
super.onKeyDown(keyCode, event);
}
Note: I have tested this in emulator.
I was interested to know how can i catch key/button events from Android TV Box remote controller?
For example, i want a popup menu to show when i click the OK button from remote controller. And i want to catch the next/back key events from remote controller.
Should i use the Key Event class from Android, if yes how should i implement it?
I came across this function but i cannot really make sense of it.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
//your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks in advance.
You should catch key event on dispatchKeyEvent
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Log.e(TAG, "Key down, code " + event.getKeyCode());
} else if (event.getAction() == KeyEvent.ACTION_UP) {
Log.e(TAG, "Key up, code " + event.getKeyCode());
}
return true;
}
Edit:
First, you should know key map of your remote (it is not the same for all kind of Android TV box), the code above will help you know code of key that you press on the remote. For example, i got key code 3 when i press button BACK on remote.
Then, i want when back key pressed, a Toast message will be show:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// You should make a constant instead of hard code number 3.
if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode == 3) {
Toast.makeText(this, "Hello, you just press BACK", Toast.LENG_LONG).show();
}
return true;
}
I'm trying to manage the Next event on the Google Keyboard on a Google Nexus 5. I want my application to check user information when the Next button gets pressed.
The code looks like this:
private TextView.OnEditorActionListener GenerateEditorListeners()
{
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_NULL && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
};
}
On a Samsung Galaxy S4 works perfect, but on a Google Nexus 5 the actionId = 0 and the event = null. So I figure out that the Samsung keyboard works fine with this code, but doesn't happen the same with the Google Keyboard.
Any idea on why it's not wokring for Google's keyboards?
EDIT: I've read in this post that Google keyboard has a bug in some LatinIME keyboards.... I'm using a latin one. If that's the problem, how to solve it?
I could solve it by using the OnKeyListener event:
textUserEmail.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
});
Now it works in all keyboards.
I've noticed that the 'back' request used to be made at the point a user pressed down on the 'back' key but at some point this was changed so that a 'back' request is made instead at the point the 'back' key is released. (Correct me if I am wrong!) Does anyone know from which SDK (or API Level) exactly this change was made effective? I think it was SDK 2.0 (API Level 5) and hence have the code in my Activity as follows but would like to be certain...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR)
handleBackRequest();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR)
handleBackRequest();
return true;
}
return super.onKeyUp(keyCode, event);
}
Use onBackPressed() for Android 2.0 and higher. Use onKeyDown() for Android 1.6.
With the code in my previous post I was getting some unwanted behaviour (when dialogs, the android keyboard etc were showing up) and on CommonsWare's advice, changed my code to the following and it seems to be working alright for the different SDKs...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK
&& android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR)
{
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed()
{
// handle back request here
}
... Let me know please if there's anything here not quite right!
I've attached an OnKeyListener to an EditText. I've overrode the onKey handler to capture a KeyEvent.
When a user hits the enter key (either their computer enter key while using the emulator, or the hardware enter key on their phone with a hardware keyboard), the onKey handler gets executed twice. Both executions have the keyCode 66.
Why is this happening?
I want my screen so when the user hits the enter key, a search is performed. Because of what is happening, the search is needlessly happening twice.
My method looks like this:
mFilter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// perform search
return true;
}
return false;
}
});
Ahhhh
I think this is happening for key up and key down?
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction()==0) {
Try this:
mFilter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// perform search
return true;
}
}
return false;
}
});
you can filter like this :
object.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
// do stuff
return true;
}
return false;
}
});
idem when you push the key with KeyEvent.ACTION_DOWN
I had the same issue and the answers above helped me but I am using Xamarin.Android (c#) so it is slightly different syntax.. Here is what worked for me:
MyStringTextBox.KeyPressed += OnEnterKeyPressed;
protected void OnEnterKeyPressed(object sender, View.KeyEventArgs e)
{
if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Up)
{
DoSomething(this, EventArgs.Empty);
}
else
{
e.Handled = false;
}
}
This way, the DoSomething() would only be called on hitting Enter Key (Up) only and thus would be fired once. Works and tested on Xamarin.Android
This event is fired by KeyEvent.ACTION_DOWN and KeyEvent.ACTION_UP.
I have done debugging and finally I realize that there is an param called KeyEvent event that I never use, then I checked and found the problem.
I debugged and what worked for me was that,
editText.setOnKeyListener(View.OnKeyListener { view, i, keyEvent ->
if (i == KeyEvent.KEYCODE_ENTER && enterAsSend && (keyEvent.action == KeyEvent.ACTION_UP || keyEvent.action == KeyEvent.ACTION_DOWN)) {
//Do stuff
}
return#OnKeyListener true
}
false
})
and checkout your Editext that android:inputType="textNoSuggestions" because the first click of enter key gives us the suggestion from the dictionary.