Android NDK : How to handle Back key press in Cocos2dx? - android

The question is all I have to say about it. I need to know how to handle the special keypresses like back, menu etc. in android NDK. I am using Cocos2dX so if you could give me a Cocos2dX specif answer that would be great.

In cocos2dx, each CCLayer gets the following methods that can be overriden to add functionality to them:
class CC_DLL CCKeypadDelegate
{
public:
// The back key clicked
virtual void keyBackClicked() {}
// The menu key clicked. only avialble on wophone & android
virtual void keyMenuClicked() {};
};
CCLayer inherits from CCKeypadDelegate. And each screen can give implementation to these functions.

In Cocos2d-x
You have to do that
implement
virtual void keyBackClicked();
and also this
this->setKeyPadEnable(true);
in .cpp class

Enable keypad : this->setKeypadEnabled(true);
Override onKeyReleased method :
virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
Check for keyCode:
void GameScene::onKeyReleased(EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
{
if (keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE) {
CCLOG("You pressed back button");
}
else if(keyCode == EventKeyboard::KeyCode::KEY_MENU)
{
CCLOG("You pressed menu button");
}
}

If your class is not inherit with CClayer, then you can handle this through JNI calls.
In Cocos2dxGLSurfaceView.java you handle back and menu key from onKeyDown function
public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent)

Related

Add missing button mapping

I am creating a qml app for android and run into the following problem when trying to catch button presses:
When I press the volume up, volume down or the home button. The following snipped prints: 0 which means the key event is unknown (Based on the QKeyEvent docu)
Keys.onPressed: {
console.log(event.key);
}
I also see this warning in logcat when pressing the home button:
Unhandled key code 3 !
or this warning when pressing the volume up button:
Unhandled key code 252 !
My Question: How can I add a handler/mapper for this key codes ?
Example I want to map the key code 3 to Qt.Key_Home in order to receive the correct key event in Keys.onPressed
I was able to 'remap' the key codes my devices uses to the key codes Qt expects by:
Copy $QT/src/android/java/src/org/qtproject/qt5/android/binding/ into your proejct folder.
Change ANDROID_PACKAGE_SOURCE_DIR in your .pro to point to the this folder.
rename the package name of all the classes in your new android/binding/ folder to fit the package name of your qml app.
add your custom mapping code in onKeyDown():
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//do the remapping for the home button
if(keyCode == 3) {
keyCode = 122;
}
if (QtApplication.m_delegateObject != null && QtApplication.onKeyDown != null)
return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyDown, keyCode, event);
else
return super.onKeyDown(keyCode, event);
}
Not near my QML machine but of top of my head do something like this.
Create a global object with required key codes like..
QtObject {
id: key_code
property int volume_up: 252
property int volume_down: <key_code_val>
property int key_home: 3
}
Then use it like..
Keys.onPressed {
if (event.key === key_code.volume_up) {
//Do volume up handling..
event.accepted = true
}
}
Also take a look at this: http://doc.qt.io/qt-5/qt.html#Key-enum

Can an Android modifier key fallback on an action?

I'm working on an input device that has a button that I'd like to act both as a home button when pressed by itself, but also work as a modifier for the other buttons on the device.
I know you can do this in the kcm file with other buttons, and have something like:
key ESCAPE {
base: fallback BACK
alt, meta: fallback HOME
ctrl: fallback MENU
}
Is it possible to have something like:
key CTRL_LEFT {
base: fallback HOME
{SOMETHING HERE}: CTRL_LEFT
}
so that I can press that button with another button and have it not fallback on HOME?
Thanks in advance! :D
This functionality is not possible on a system level: see
http://source.android.com/tech/input/key-character-map-files.html for a list of all options possible.
However, if you would like to have it on an application level, you could program it in as follows:
import android.view.KeyEvent;
private boolean home = false;
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (event.getScanCode() == 29) //29 == CTRL_LEFT
home = true; //if we release this button, HOME needs to be invoked
else home = false; //so CTRL_LEFT was used as a modifier: no need to invoke HOME
//allow the system to pass key handling to the next listener
return super.onKeyUp(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (event.getScanCode() == 29 && home == true)
{
super.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME));
return true; //so we absorb the event
}
return super.onKeyUp(keyCode, event);
}
Then, as long as this application is set up to receive the keystrokes (usually a privacy issue unless you're coding it in for yourself), it can process the keystrokes and dispatch the HOME button when need be.
Should you decide to proceed thus, you'd have to remove the
base: fallback HOME
from your code.

Actionscript 3: Controlling the physical back button in Android

I'm developing a simple app for Android using ActionScript 3.0 in Flash CS5 and I'd like to know if there's a way to map the physical back button of the Android's phone to tell my animation to go to the first frame.
I've red that post: Disabling the phone's back button (AIR for Android /ActionScript 3)
so maybe it is possible ? If yes HOW.
Thank you !
//first register key down listener
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);
//then listen to the back key
private function handleKeys(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK ) {
logDebug("=>handleKeys.");
NativeApplication.nativeApplication.exit();
}
}
Have you tried overriding onKeyUp method?
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do your stuff
}
return false;
}

Block Back Button in android

I want to block hardware back button in android ,in order to prevent from going back to other activity..
Thanks in advance...
Here is code that allows you to handle the back key in an activity correctly on all versions of the platform:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( Integer.valueOf(android.os.Build.VERSION.SDK) < 7 //Instead use android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
// Take care of calling this method on earlier versions of
// the platform where it doesn't exist.
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the
// platform.
return;
}
sources:http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
If the 'other activity' is yours, you can set it to not appear in the history list.
Otherwise, remember that the phone belongs to the user and not to you, and stop trying to tell them what they can and can't do with THEIR device.

Confused about Android key event handling. Any good explanations?

I'm a relative beginner with Android. Does anybody have a sane explanation for how to listen for keys and soft keys in an EditText/TextView?
I'd love to see a comprehensive tutorial or set of examples.
As I understand it, I can add a KeyListener to my Activity, e.g. onKeyDown(), onKeyUp() but when I try this I can't trigger the events for normal keys only HOME and BACK for example.
I have seen mention of using a TextWatcher but that isn't the same as handling raw key events.
There seem to be a number of half-solutions here on SO. Hoping you can help clear the mists of confusion...
You have to assign a key listener not to activity but rather to EditText itself.
This is what I have to listen to BACK or MENU key events. Simply add this method, without implementing any Interface. I do this in my BaseActivity, from which every Activity inherits.
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(NAME, "Key pressed");
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Log.d(NAME, "Back pressed");
// IGNORE back key!!
return true;
/* Muestra el MenĂº de Opciones */
case KeyEvent.KEYCODE_MENU:
Intent menu = new Intent(this, Menu.class);
// start activity
startActivity(menu);
return true;
}
return super.onKeyDown(keyCode, event);
}
PS: I highly discourage ignoring the back key.
For example:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
//your code here
}
return false;
}
});
I recently found another way that be stuck using Activity onKeyDown, or event setting a key listener on view (which is not really working with key events from ADB in my case) with view.setOnKeyListener.
Since android P method addOnUnhandledKeyEventListener has been introduced. It allows you to do whatever you need to do when your view is able to catch unhandled key events.
Here is an example of how I used it :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
when (event.keyCode) {
KeyEvent.KEYCODE_UNKNOWN -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
KeyEvent.KEYCODE_SOFT_RIGHT -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
// etc...
else -> false // Specify you didn't handle the event
}
}

Categories

Resources