• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps on key press

sniper8752

Well-Known Member
I am trying to complete an action when a key is pressed. I have the following code:

[HIGH]@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_E:
/* Sample for handling the Menu button globally */
Toast.makeText(getApplicationContext(), "E BUTTON WAS PRESSED!",
Toast.LENGTH_LONG).show();
return true;
}
return false;
} [/HIGH]

When I press E though, it does not do anything.
 
Hi sniper8752

It maybe to do with your final return statement try :

return super.onKeyDown(keyCode, keyEvent);

Instead of :

return false;

Cheers

Timcs
 
It does not work.


Okay that was just a suggestion from a post I found on the net. Looking on the net again, it does look like you need to include the "super" method within the onKeyDown so I have found this link :

Android Codes: Example code for OnKeyDown listener in Android

and it looks like from this code that the super.onKeyDown( keyCode,keyEvent); is the first line within the onKeyDown method and then the final return is as you have put :

return false.

See how you get on with that.

Thanks

TimCS
 
It still doesn't work.

Update: just noticed something. I have swiftkey installed, and I modified my code to look like so:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Toast.makeText(getApplicationContext(), "Good",
Toast.LENGTH_LONG).show();

"Good" shows up when i press keys like backspace, menu and enter, but not the letters.
 
It still doesn't work.

Update: just noticed something. I have swiftkey installed, and I modified my code to look like so:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Toast.makeText(getApplicationContext(), "Good",
Toast.LENGTH_LONG).show();

"Good" shows up when i press keys like backspace, menu and enter, but not the letters.

Is this onKeyDown event connected to any object in your app , such as an editText ? if so you need to "bind" the onKeyDown to this object e.g. :


[HIGH]myEditText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// put your switch and case statements here etc....

return true;
}
return false;
}
});
[/HIGH]
Thanks

TimCS
 
Still no luck. Here is what I have now:

EditText editText1;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText edittext = (EditText) findViewById(R.id.editText1);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_E)) {
Toast.makeText(getApplicationContext(), "Good",
Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(getApplicationContext(), "bad",
Toast.LENGTH_LONG).show();
return false;
}
});
}
 
Still no luck. Here is what I have now:

EditText editText1;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText edittext = (EditText) findViewById(R.id.editText1);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_E)) {
Toast.makeText(getApplicationContext(), "Good",
Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(getApplicationContext(), "bad",
Toast.LENGTH_LONG).show();
return false;
}
});
}

You need to add the @override above the onKey method

Thanks

TimCS
 
The override annotation shouldnt matter (but it is good practice), but onKey events for EditTexts tend to only be command keys. "Done" "Go" "Enter" "Next" etc.

I think the OP needs a TextWatcher



listener - onKeyListener not working with soft keyboard (Android) - Stack Overflow

Yes you are probably right and stupid me because I am using a textwatcher in my current project but its purpose is to check for the length of the entered text to make sure it is valid for the database field it will be written to.

Thanks

TimCS
 
what I am looking to do is when a certain phrase is typed, it runs a method after that phrase is typed. how would I do this?
 
what I am looking to do is when a certain phrase is typed, it runs a method after that phrase is typed. how would I do this?

First you need the import statements as this :

[HIGH]import android.text.TextWatcher;
import android.text.Editable;
[/HIGH]

then the way I have done it (this makes it work for different edit text objects) is to implement the textWatcher to your activity :

[HIGH]public class youractivity extends Activity implements TextWatcher [/HIGH]

This will then create an option to create unimplemented method (your activity name will have that wiggly line under it) and these are :

[HIGH]@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}[/HIGH]

I suspect from what you are wanting to do , I would use the :

[HIGH]@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}[/HIGH]

as a test lets say your editText is calls enterhere and you want to show a message when hello is entered , the following should do the trick :


in the onCreate method you would first to this :

[HIGH] enterhere.addTextChangedListener(this);[/HIGH]

Then in the afterTextChanged method try this :

[HIGH] @Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if (s == enterehere.getEditableText()) {

if (enterhere.getText().length() > 0 ) {
if (enterhere.getText().toString()=="hello") {
// Put your message here such as a Toast message
}
}
}

}[/HIGH]



Thanks

TimCS
 
It seems to keep on crashing when I attempt to open it. Here is what I have:

[HIGH]import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements TextWatcher {

EditText edittext = (EditText) findViewById(R.id.editText1);

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edittext.addTextChangedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void afterTextChanged(Editable s) {
if (s == edittext.getEditableText()) {
if (edittext.getText().length() > 0 ) {
if (edittext.getText().toString()=="hello") {
Toast.makeText(getApplicationContext(), "afterTextChanged",
Toast.LENGTH_SHORT).show();
}
}
}

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}
}[/HIGH]
 
I think you need to move your line where you are defining your editext within the onCreate method . So currently you have this as follows :

[HIGH]public class MainActivity extends Activity implements TextWatcher {

EditText edittext = (EditText) findViewById(R.id.editText1);

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edittext.addTextChangedListener(this);
}[/HIGH]

Try Defining the edittext this way instead :

[HIGH]public class MainActivity extends Activity implements TextWatcher {

EditText edittext;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext = (EditText) findViewById(R.id.editText1);
edittext.addTextChangedListener(this);
}[/HIGH]


Thanks

TimCS
 
Back
Top Bottom