1

Please anyone tell me if there is any listener on finding which key is pressed currently in an EditText from softkeyboard programmatically.

onKeyListener is for hardkeyboard only and i cant write 100 if else conditions for each and every keycode's and using textwatcher you can get the whole string but not the current pressed key. I have looked into all of these solutions.

Note : What i require is that when the user presses "A" key, it should show toast "A" (or any other alphabet)

Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66

2 Answers2

1

With using this this listener you will get the current pressed key from keyboard..

@Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        char ch=  s.charAt(start + count - 1); 

    }

here it will print the last entered character..

Community
  • 1
  • 1
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • What i require is that when the user presses "A" key, it should show toast "A" (or any other alphabet – Rahul Gupta Nov 05 '13 at 09:59
  • This is called when the enter key is pressed, or when an action supplied to the IME is selected by the user. – Rahul Gupta Nov 05 '13 at 10:00
  • @kalyanpvs your code is wrong, you should log s.subsequence(start, start+count) – pskink Nov 05 '13 at 10:14
  • ok, what. is onTextChanged? is it a part of TextWatcher? if so count is almost always equal to 1 and you have to use suqsequence(start, start+count) – pskink Nov 05 '13 at 10:25
  • did you try to Log.d three params: s, start and count? i see you did not, then do it – pskink Nov 05 '13 at 10:39
  • are you using a TextWatcher added to an EditText with addTextChangedListener? – pskink Nov 05 '13 at 10:46
  • tell me what is a purpose of start == 0 (always) and count == s.length() (always), i dont know where do you have those numbers from, since my numbers are taken directly from a running emulator – pskink Nov 05 '13 at 10:52
  • ok..i agree with you..start and count are changed based on the cursor position..but your code also gives you wrong result what he expect.. – kalyan pvs Nov 05 '13 at 11:02
  • @kalyanpvs let me say example, if user has entered text like "Android". now last character is 'd' because user has pressed 'd', but when user has deleted last two character by using delete key in keyboard, then text becomes "Andro" but the above method will give last key event character as 'o' where as user pressed delete key. – Gopal Gopi Nov 05 '13 at 11:17
  • no i am tested..your code is printing whole string because if we dont chage the cursor position in edittext start is zero so whole string is prints..that too he want the last entered character whrever the cursor is count-1 returns the last char..if he changed the cursor position you code will work for first time and if he enter another char that two characters will be print.. – kalyan pvs Nov 05 '13 at 11:19
  • ok add Log.d(TAG, "[" + s.subSequence(start, start + count) + "]") and run it and see results... – pskink Nov 05 '13 at 11:25
  • http://pastebin.com/4zfNPjhZ i dont know what build you are using but onTextChanged params are completly off with a TextWatcher documentation – pskink Nov 05 '13 at 12:14
  • btw what API build are you using? i tested on 2.2 – pskink Nov 05 '13 at 15:44
0
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    // Do Code here
}
return super.onKeyDown(keyCode, event);

}

Check this link. It will help you

Community
  • 1
  • 1
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • i already have mentioned that i cannot write if statements for each and every keycodes to check which key has been pressed. That is the point – Rahul Gupta Nov 05 '13 at 09:53
  • In text watcher, you don't you take the last character of the String, that will give you the current key na? Or can you pls tell me what to compare?? an example? – Eldhose M Babu Nov 05 '13 at 09:59
  • What i require is for example :- When the user presses "A" key, it should show toast "A" (or any other alphabet) – Rahul Gupta Nov 05 '13 at 10:01
  • why dont you want to use a TextWatcher? – pskink Nov 05 '13 at 10:06
  • Then in the textwatcher, check whether a new letter is added at end and if added, take the last letter and that will give you the input letter na? then toast it as you need. – Eldhose M Babu Nov 05 '13 at 10:08