I am recently working with cocos2dx 3.0rc2 and I want to respond the back key event in android platform. I learned that I could override the onKeyReleased method of the layer and setKeypadEnabled(true) to capture the back key.
However I got a problem, I can capture the event, but not accurately. That is, I expect this method gets called when I released my finger from the back key. However it now gets triggered as soon as I put my finger on the back key. That being said, it responds at key-down phase while I wish it could do it at key-up phase.
Can you help me with this? By the way, I tested the code and it seemed that on win32, the backspace key was not responded (But it does not matter for me as I only care about android)
Here are the code blocks:
init:
...
this->setKeypadEnabled(true);
...
onKeyReleased:
...
if(keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE) {
onBack(nullptr);
}
...
I also tried the other way to capture the event, by setting the listener instead of simply putting setKeypadEnabled(true). The result is the same.
I appreciate your help!
In our apps, we did like this:
auto obKeyBackListener = EventListenerKeyboard::create();
obKeyBackListener->onKeyReleased = [=](EventKeyboard::KeyCode keyCode, Event* event)
{
if (keyCode == EventKeyboard::KeyCode::KEY_BACK) // KEY_BACK
{
event->stopPropagation(); // stop propagation for this event
onBack(nullptr);
}
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(obKeyBackListener, this);
You have written the correct code but have used the wrong key code.
It shoud be EventKeyboard::KeyCode::KEY_ESCAPE.
Change this:
this->setKeypadEnabled(true);
with this:
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this);
Related
I've been trying to catch the keyboard's events within a TextInput in react-native.
By reading the component's doc (https://facebook.github.io/react-native/docs/textinput.html) I noticed the onKeyPress function which fits perfectly what I need. But it is labelled as ios only. I haven't found anything about an android workaround except this issue (https://github.com/facebook/react-native/issues/1882) which has been inactive for a couple months now ...
What I'd need to do is calling a specific method when Backspace is pressed and it looks like it can only be done for ios for now ...
Do you guys know any workaround for this ?
Thanks in advance :)
onKeyPress is now supported on Android.
From version v0.55.2 (commit)
Note hat the hardware keyboard inputs are not supported on Android, only the soft keyboard inputs.
Meaning, if you test this on an Android emulator and you type on your computer keyboard, those inputs are not be handled.
So, go ahead and press the soft keyboard on the emulator with the mouse.
I've come across this problem, too. I can tell you, what I did. It's not elegant, but it will do the job, until it's implemented for Android too.
In Android you can handle key events in Activity. Also, you can send events to JavaScript. That's everything you need.
In js file (e.g. componentDidMount) add listener.
DeviceEventEmitter.addListener('onKeyPressed', yourFunction)
In your MainActivity add something like this.
public boolean onKeyUp(int keyCode, KeyEvent event) {
WritableMap params;
// params.put something
// filter only Backspace events if you wish etc.
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("onKeyPressed", params);
}
It will be send every time a key was pressed. Don't forget to remove listener (e.g. componentDidUnmount or whenever you don't need it anymore). Also, if you have multiple TextInput, keep track of what is focused, so you don't mix it up.
Hope this helps.
You can use onChangeText of TextInput and inside the function supplied to onChangeText, you can check if the last entered text has One character less than the earlier text in the TextInput. If it has, then it means user pressed backspace and you can trigger/call your specific method.
class SearchScreenBase extends Component {
constructor(props) {
super(props);
this.state = { lastText: '',
};
// Bind callback methods to make `this` the correct context.
this._onChange = this._onChange.bind(this);
}
_onChange(newText) {
var oldText = this.state.lastText;
if(newText.length === (oldText.length-1)){
//INSERT TRIGGER FUNCTION HERE
}
this.setState({
lastText: newText
});
}
render() {
return (
<TextInput
onChangeText = {this._onChange}
editable = {true}
maxLength = {40}
/>
);
}
}
It worked for me pretty well.
Add this in your MainActivity.java
import com.facebook.react.modules.core.DeviceEventManagerModule;
import android.view.KeyEvent;
...
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
getReactNativeHost().getReactInstanceManager().getCurrentReactContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("onKeyPressed", keyCode);
return super.onKeyUp(keyCode, event);
}
Now this will return the keyCode corresponding to the button pressed
Add this to your component to listen to the emitted keyCode, for e.g. I am adding the listener in componentWillMount
componentWillMount = () => {
this.keyPressedListener = DeviceEventEmitter.addListener('onKeyPressed', yourFunction);
}
Handle yourFunction according to your requirements and don't forget to remove the listener afterward.
ive got another Question for you.
So im trying to get the user input working on an options menu. For this i got:
1. The Stage and
2. An extra Inputadapter
I need the extra Inputadapter to catch the BACK key on Android. So i have used an Inputmultiflexer, which allows me to use both inputprocessors.
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiplexer);
Gdx.input.setCatchBackKey(true);
And my method to check for the BACK button looks like this:
#Override
public boolean keyUp(int keycode){
if(keycode == Input.Keys.ESCAPE || keycode == Input.Keys.BACK){
new MenuScreen(game);
return true;
}
return false;
}
The problem: Its not working at all. It does not go back, when using the Back key on Android or the Escape key on Desktop. The only thing the Console is printing out when pressing the Button is :
Load KCM of non-default device may incur unexpected result
To be honest, i have no idea what its means and Google didnt help me either with that.
So how do i get this to work?
First when debugging, you should use something like :
System.out.println("back was pressed");
If you see that output in the console then you know input is working. If that works, the only problem I see with your code is that you may have created a new screen class, you never made it switch screens. I noticed you passed in the game object, so you should probably have something like this:
game.setScreen(new MenuScreen(game));
Hope this helps.
I have a Xamarin Android application with a GestureDetector. On my device (Sony Xperia S5303) and in emulators it works fine. However, on a Samsung Galaxy tablet, the OnSingleTapUp event is fired twice, even though I touch the screen just once. Since the event is checked by the OnSingleTapConfirmed, and the device behaves as if two taps were made, the following method is never called:
public bool OnSingleTapUp(MotionEvent e)
{
if (PersistentContext.LoggedPlayer.Token)
{
if (simpleOnGestureListener.OnSingleTapConfirmed(e))
{
Move(e);
}
return true;
}
return false;
}
The strange thing is, that when clicking on a button, the button method is called just once. Can anybody tell me what might cause this strange behaviour and how to avoid it?
You need to use onSingleTapConfirmed(MotionEvent event) instead. This way android makes sure it's a single tap and doesn't fire the event twice.
Edit: if you are determined to use onSingleTapUp(MotionEvent event), then you can use event.getEventTime()and if the time difference between the first and second events is less than a THRESHOLD like 200ms or something, consider it the unwanted second-time fired tap and ignore it.
Edit 2: Since I don't use Xamarin I might be totally wrong but I think you need to change your code like this: (I mean forget onSingleTapUp completely and move wyour whole code into onSingleTapConfirmed)
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
if (PersistentContext.LoggedPlayer.Token)
{
Move(e);
return true;
}
return false;
}
Hope it helps and Good Luck!
i'm working on a project wich require the use of a custom soft keyboard developed by some one else. The problem is that the setOnEditorActionListener does not work in a specific windows where a fragment is used. Does not work means that the onEditorAction is not fired at all. The problem appens only with the custom keyboard, with the default one every thing is working well. The problem is that the soft keyboard project is very complex because i don't know soft keyboard logics and I need to solve the problem before tomoroow morning. Does anyone have an idea of this behavior? Please help
this is the part where i set the listener, this code is working all around the project but here, even the first listener's line is not reached
((EditText) getView().findViewById(R.seatDetailCommonHeader.txtName)).setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) {
return true;
}
// KeyboardHelper.setKeyboardVisibilty(v, false);
executeCheck();
return true;
}
});
i went into further investigations, i put a breakpoint on every method's first line in the keyboard code (which is the one taken from the sdk samples with just some layout modification) and the same EditText in two different activities fires different methods:
in one case (the working one) this methods are fired when action button is clicked:
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
SoftKeyboard.onKey
SoftKeyboard.isWordSeparator
SoftKeyboard.sendKey
SoftKeyboard.keyDownUp
SoftKeyboard.keyDownUp
SoftKeyboard.updateShiftKeyState
in the other case (the one that is not working) the same methods are fired, plus these:
SoftKeyboard.onFinishInput
SoftKeyboard.onStartInput
SoftKeyboard.updateShiftKeyState
LatinKeyboard.setImeOptions
SoftKeyboard.onStartInputView
hope someone has some idea of this behavior because i'm really in trouble
I'm developing a simple app on PhoneGap for an Android set top box.
I have an image that is usable as a link. When I connect a mouse to the set top box and click the image, the link works. But when I use the remote control and select the image (I see the border around the image so I know it is selected) and I click OK button, the link does not work.
How can I use the remote buttons in the code?
This is very tricky because Google didn't feel like mapping the keys on a remote to an actual key output.
To use the setTopBox, you're going to have to figure out what key codes your Android Set Top Box is using and modify the Activity's onKeyUp event to handle it. We currently have an example of a work-around in this bug however we don't have an agreed API for exposing these buttons to Javascript yet, which is why this bug is still open.
But in short, you'd do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
{
sendJavascript("javascript:myJsMethod('UP');");
return true;
}
return super.onKeyDown(keyCode, event);
}