How to use Onkeydown Event for Buttons? - android

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

Related

Disable option on soft-keyboard android programmatically

I want to know that if possible disable hide option on a soft-keyboard programmatically.
Hide Option
//Disable return button
#Override
public boolean dispatchKeyEvent(KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
return true;
}
}
return super.dispatchKeyEvent(event);
}
This code disable, as you can see, the return button. For your case you should replace KEYCODE_BACK with another constant. I don't know for sure if it is one for option button but you can check the list right here: https://developer.android.com/reference/android/view/KeyEvent.html

Getting the pressed character without using edidtext

I want to get a pressed character in the code without using edittext.
Simply a black screen appear if someone presses "a" or "b" etc.. I want to get this value in my code.
I have done it using editext and textwatcher but the requirement is we don't need to use them now.
Can we make character(alphabet) moving from top to bottom randomly
I have followed some links which have used marquee but it does not work.
In your Activity override the method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_A) {
//Here your black screen if you press A
return true;
}
return super.onKeyDown(keyCode, event);
}
Check this KeyEvent ;

Overriding back button press from Activity's subClass in Android

I'm making a game with OpenGL Es 1.1 and i want to implement back button or menu button functionality to my game.(I mean hardware buttons).I have some subClasses so what i have to do when i want to handle hardware button press from subClasses?
You need to implement an onKeyDown listener and check to see what key is pressed.
Sample:
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
handleMenuButton();
return true;
case KeyEvent.KEYCODE_BACK:
handleBackButton();
return true;
}
return super.onKeyDown(keycode, e);
}
Also note that for the back and menu buttons to fire reliably, you need to set setFocusableInTouchMode to true. See the devguide here (scroll down to "Touch Mode").

Capturing special key presses (shift, alt) of the Soft / OnScreen keyboard

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.

Can Menu and onKeyDown work together?

I want to use Menu and OnkeyDown in same activity but it is not working, when I use OnkeyDown then menu disappear from the activity. I want to make a action using virtual keyboard's enter key. When I press enter key of virtual keyboard one toast should be appear.
Please help.
Thanks in advance
You have to return false outside the switch statement of the onKeyDown but true for every key you handle:
public boolean onKeyDown(int KeyCode, KeyEvent event) {
switch (KeyCode) {
case KeyEvent.KEYCODE_CAMERA:
//take photo
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
//lower volume
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
//raise volume
return true;
}
return false;
}
This works for 2.3.3

Categories

Resources