Alex Walker
- Forum posts: 1
Nov 25, 2013, 12:31:32 PM via Website
Nov 25, 2013 12:31:32 PM via Website
I have an "edittext" in my app, and I want to do things when certain characters are pressed on the soft keyboard. I have tried every which way and how stackOverflow suggests, however characters i.e a/A, b/B etc dont get detected. Enter and Del do however. After reading loads, Apparently I have to override the edittext class, according to an example on here, so I have done that. I am overriding the method here:
Really I would like it not to need a textbox at all, and just show the keyboard, but I dont think thats possible?
Thanks
1@Override
2 public boolean sendKeyEvent(KeyEvent event) {
3 if (event.getAction() == KeyEvent.ACTION_DOWN
4 && event.getKeyCode() == KeyEvent.KEYCODE_A) {
5 ZanyEditText.this.setRandomBackgroundColor();
6
7 return false;
8 }
9 return super.sendKeyEvent(event);
10 }
I also have tried overriding the standard edittext onKeyListener:2 public boolean sendKeyEvent(KeyEvent event) {
3 if (event.getAction() == KeyEvent.ACTION_DOWN
4 && event.getKeyCode() == KeyEvent.KEYCODE_A) {
5 ZanyEditText.this.setRandomBackgroundColor();
6
7 return false;
8 }
9 return super.sendKeyEvent(event);
10 }
1txtSMS.setOnKeyListener(new View.OnKeyListener() {
2 @Override
3 public boolean onKey(View v, int keyCode, KeyEvent event) {
4 // You can identify which key pressed buy checking keyCode value
5 // with KeyEvent.KEYCODE_
6 if (keyCode == KeyEvent.KEYCODE_DEL) {
7 // this is for backspace
8 Toast.makeText(getApplicationContext(), "Del was pressed", Toast.LENGTH_SHORT).show();
9 }
10 if (keyCode == KeyEvent.KEYCODE_A) {
11 // this is for backspace
12 Toast.makeText(getApplicationContext(), "A was pressed", Toast.LENGTH_SHORT).show();
13 }
14 return false;
15 }
However in both cases on the Del is ever detected. how come I cant detect characters of the alphabet? They appear in the text box...2 @Override
3 public boolean onKey(View v, int keyCode, KeyEvent event) {
4 // You can identify which key pressed buy checking keyCode value
5 // with KeyEvent.KEYCODE_
6 if (keyCode == KeyEvent.KEYCODE_DEL) {
7 // this is for backspace
8 Toast.makeText(getApplicationContext(), "Del was pressed", Toast.LENGTH_SHORT).show();
9 }
10 if (keyCode == KeyEvent.KEYCODE_A) {
11 // this is for backspace
12 Toast.makeText(getApplicationContext(), "A was pressed", Toast.LENGTH_SHORT).show();
13 }
14 return false;
15 }
Really I would like it not to need a textbox at all, and just show the keyboard, but I dont think thats possible?
Thanks
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.