I am trying to capture the key-press for the shift or alt keys and it seems to work just fine with the physical keyboard. Unfortunately when I test the same code with the onScreen keyboard it doesn't respond at all (I've tried a 2.1, 4.03 emulator and 2.3 physical device).
This seems to be a platform issue that probably has no workaround, but as a last ditch effort I wanted to post the question here to see if anyone has found one (I am willing to entertain any ideas).
Thanks!
Edit to clarify my point: I tried putting a breakpoint on the " if (event.getAction() == KeyEvent.ACTION_DOWN) " line, and it will always stop if I press the shift of the physical keyboard, but never when I press the shift of the onScreenKeyboard. So, the problem is that onKey is not being executed for all keypresses of the onScreenKeyboard.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText1 = (EditText) findViewById(R.id.editText1);
mEditText1.setOnKeyListener( new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
Toast.makeText(IMETestActivity.this, "Enter was consumed",
Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.META_SHIFT_LEFT_ON:
case KeyEvent.META_SHIFT_RIGHT_ON:
Toast.makeText(IMETestActivity.this, "Meta Shift was consumed",
Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
Toast.makeText(IMETestActivity.this, "Shift was consumed",
Toast.LENGTH_SHORT).show();
return true;
default:
break;
}
}
return false;
}
});
}
Try logging the keycode whenever a key is pressed to make sure that the event is registering. If it is, just add that code to your switch statement.
For example, something like System.out.println(keyCode) at the beginning of your onKeyListener. You can view the output using LogCat.
Related
I have created android keyboard and I am creating arrows to move the cursor in the user input, I could do the left and right but I couldn't know how to write the code for the up and down .. here is the code
switch (arrow){
case KEY_LEFT:
CharSequence leftText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int leftLen = leftText.length() ;
getCurrentInputConnection().setSelection(leftLen-1, leftLen-1);
break;
case KEY_RIGHT:
CharSequence rightText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int rightLen = rightText.length() ;
getCurrentInputConnection().setSelection(rightLen+1, rightLen+1);
break;
case KEY_UP: case KEY_DOWN:
break;
}
can any one help me implementing the down and up ??
If you just want to move cursors
https://developer.android.com/reference/android/inputmethodservice/InputMethodService.html#sendDownUpKeyEvents(int)
Try use sendDownUpKeyEvents method
sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_RIGHT);
Will simply do the work.
First of all, I recommend doing something like this to check for key presses
myEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v,
int keyCode,
KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
moveCursorLeft();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
moveCursorRight();
return true;
case KeyEvent.KEYCODE_DPAD_UP:
moveCursorUp();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
moveCursorDown();
return true;
}
}
return false;
}
});
I'm going off the assumption that you're programming a text editor of some sort.
The implementation for up and down depends on what you want those buttons to do, and how you've implemented things so far. For example, you could have each line as its own EditText that is kept track of in an ArrayList. Then, each time you add a new line, you can add a new EditText to the ArrayList. And when a user presses up or down, you can just change the index of the ArrayList by 1 in the correct direction.
I imagine that would be a pretty inefficient way of doing it, but it would be easy to define what up and down do. It's difficult to say more about what you should do without seeing more of your code.
All the Android experts,
I'm working on Android TV app.
I face problem on Dpad navigation.
i would like to stop auto Dpad navigate while KeyDown UP and DOWN.
i wrote a listener on a focusable TextView, if TextView on key UP then scroll UP the listview, and key DOWN scroll DOWN.
but the below code seen failed to scroll my listview, my focus move to other focus point while i press DOWN.
Is there any solution that i can override the auto focus navigation? i would like my TextView ignore the auto navigation to next focus while i press key UP and DOWN.
Thank you.
textView.setOnKeyListener(new View.OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_PAGE_UP:
listview_scrollUP();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_PAGE_DOWN:
listview_scrollDOWN();
break;
}
}
return false;
}
});
After some research, i got some idea to solve my problem.
i used setDescendantFocusability() block the rest of my fragments(viewgroup) while my dpad focus enter the targeted fragment, ideally is to space out the key UP and DOWN for my purpose usage (without processing dpad navigate next focus).
textview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
LinearLayout menu = (LinearLayout)getActivity().findViewById(R.id.mainMenuLayout);
menu.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
}
});
my targeted fragment just needed LEFT and RIGHT to let dpad change to next focus, and then my UP and DOWN onKey Down is used for scrolling the listview.
setDescendantFocusability() enable while the dpad navigate focus reached the edge of my fragment focusing point.
textview.setOnKeyListener(new View.OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
LinearLayout menu = (LinearLayout)getActivity().findViewById(R.id.mainMenuLayout);
menu.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
menuItem1.requestFocus();
break;
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_PAGE_UP:
listview_scrollUP();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_PAGE_DOWN:
listview_scrollDOWN();
break;
}
}
return false;
}
});
this is what i came out of to solve my problem.
wish this info help up other.
thank you.
I am using a KeyEvent in my App.Now I have to replace the KeyEvent With the Button in my Layout.I dont't know how to use OnclickListener with KeyEvent.I searched & got a Method using OnTouchListener.https://stackoverflow.com/a/13311997/2781359
I tried it.I don't know how to use it ???
Now I Want to make the keyEvent Action when the button is Clicked..
My Current Method
public boolean onKeyDown(int keyCode, KeyEvent event)
{
int unicode = event.getUnicodeChar();
if (unicode == 0)
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_VOLUME_UP:
unicode = KeyboardAction.UNICODE_ARROW_UP;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
unicode = KeyboardAction.UNICODE_ARROW_DOWN;
break;
}
if (unicode != 0)
{
this.application.sendAction(new KeyboardAction(unicode));
}
return super.onKeyDown(keyCode, event);
}
So Help me in the Right Direction :)
Thanks for your Help ...
EDIT
When I Press the Volume UP Button the following action happens
unicode = KeyboardAction.UNICODE_ARROW_UP;
But i Want to Perform this action With a normal button(using OnClickListener) in my layout .
Do you mean you want a button to act as if one of the buttons has been pressed? I did this in my app to simulate the back button being pressed with the following code:
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
Trigger back-button functionality on button click in Android
I have an onKeyDown event which wont recognise the first key press (wont even enter the event, i have tested by producing a 'toast' output). On the second key press and after, it works perfectly. If I click on another element on the screen and try the key press again, it yet again needs another key press to get it going. Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
scorered.performClick();
return true;
case KeyEvent.KEYCODE_1:
red_m1.performClick();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
scoreblue.performClick();
return true;
case KeyEvent.KEYCODE_2:
blue_m1.performClick();
return true;
case KeyEvent.KEYCODE_BACK:
finish();
return true;
}
return true;
}
I have been stumped for hours so any help is much appreciated!
I'm sure, there are good reasons for such behaviour, but do not think, that removing focus is a good solution. My workaround is to fire a keydown event, that "activate" regular onKeyDown functionality. Here is snippet:
new Thread(new Runnable() {
#Override
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
}
}).start();
To fix this, a work-around is to remove the view focus before pressing any DPAD key. It works in my case. I am having exactly the same issue: When a view of Android Activity is on focus, the very first DPAD key event, that is, the KeyDown event, is ignored: None of these methods are called: onUserInteraction(), dispatchKeyEvent(), onKeyDown(). However, subsequent DPAD key events - KeyUp, KeyDown, KeyUp, ..., can be captured.
Note that this issue does not occur with soft keys (Home, Previous, Recents) nor with hard button keys (BUTTON_A, BUTTON_B, BUTTON_X, BUTTON_Y).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
public boolean onKey() called twice?
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
solveExpression();
return true;
}
return false;
}
});
I'm trying to solve the expression contained within the Display(EditText), by pressing the enter button on the keyboard, yet it always interprets it as though I pressed the button twice. Does anyone know why this happens?
Try...
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// Check for ACTION_DOWN only...
if (KeyEvent.ACTION_DOWN == event.getAction()) {
solveExpression();
return true;
}
}
}
});
The 'action' can be ACTION_DOWN, ACTION_UP or ACTION_MULTIPLE (the last being for when a key is pressed and held). onKey() will be called for any/all of those actions.
As the other answer mentions, it's triggering twice because it's once for down and once for up.
if (event.getAction()!=KeyEvent.ACTION_DOWN) // we catch only key down events
return true;
Thus you stop listening other keyevents as onClick.
If you want nobody else further in chain to get the event for another piece of work, you should set
return false;
not an android guy either but the fact that it registers twice makes me think that OnKey encompasses a onKeyDown and onKeyUp. Would listening to onKeyUp work for you as well?