[Android] How to lock BACK button & keyboard // HELP ME~

Question 1
Hopefully the application won’t turn off to hit the back button.
Is there a way to keep the app from turning off?

Press the button to turn off.
Is there a way to prevent the application from turning off?

Question 2
Is there a way for void keyPressed () to be recognized?
Korean is not recognized.
When writing with the keyboard

English is available but
Korean is not available.
How can I make Korean available?

Help. Please help me.

@GWAK ====

  • 1 question: No, you cannot : that is absolutely the user right to leave your app: you can only detect wether the back button was pressed and do someting (saving some var, scores and so on) with onPause();
  • 2 question: i dont understand, of course keyPressed is recognized; as for korean that is a big problem; you can define what language is used (settings of the phone) but if the korean package is not installed you can do noting except asking to your app user to download and install it…

You can catch the event and code something. When you leave the function empty nothing will happen and your app will not be closed.

boolean ok;

void setup(){
size(100, 100);
background(100);
}

void draw(){
if(ok) text(“OK”, width/2, height/2);
}

void onBackPressed() {
ok = true;
}

Thinking better I think Akenaton is right. The back button is for returning to last activity. So it’s better to let user make the choise. Code below works on my Kitkat and Lollipop. Did not test it on other.

import android.app.Activity;
boolean ok, choise, blink = true;
int timer;
Activity act;

void setup() {
  size(400, 200);
  background(100);
  act = this.getActivity();
}

void draw() {
  if (blink) {
      if (millis() - timer >= 200) {
      choise = !choise;
      if (choise) background(255, 0, 0); 
      else background(0, 0, 255); 
      timer = millis();
    }
  }
}

void onBackPressed() { 
  drawText();
  ok = true;
  blink = false;
}

void drawText() {
  textSize(30);
  text("Do you really want to quit?", 20, 70);
  text("Yes", 80, 150);
  text("No", 280, 150);
}

void mousePressed() {
  if (ok) {
    if (mouseX < width/2) act.finish();
    else if (mouseX > width/2) blink = true;
  }
}

@Akenaton b.t.w If you have a time to see my last topic I really would appreciate it, because I still didn’t solve it.

1 Like

@GWAK
As for the second question; If I understand it well, the korean keyboard has 10 vowels and 14 consonants which are combined together in a figure representing a syllable.
You say that the the keyPressed() is not recognized, but what do you mean? is it the ‘key’ or the ‘keyCode’ because the last one should work .
The keyCodes you can find here.
You could also remap for non-latin. see here

@akenaton
Good morning. Thank you for your kind reply.

@noel
Thanks.
Question 2 Solve.
But number one doesn’t work.

@noel

Please watch the video screen.

@GWAK Strange! What version of android do you use? The phone seems to be a Samsung, is that right? ? Can you capture a println() in the onBackPressed?
If you leave out any code in the onBackPressed does it ignore the back key then?
What does code below do? (Do not leave out draw())

import android.view.KeyEvent;

void draw() {
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
     println(keyCode);
    }
  }
}

@noel

Thank you for your interest.

import android.view.KeyEvent;

void draw () {
}

void keyPressed () {
if (key == CODED) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
println (keyCode);
}
}
}

When the above source code works
debug:
4

The result is as above.

The hardware specifications are as follows:

Smartphone:
-SAMSUNG NOTE4
-Version: 6.0.1
Processing
-Android mode
SDK (API6.0 (23))

@GWAK
Unfortunately I do not have a Marshmallow to test.
You did not answer the question, “If you leave out any code in the onBackPressed does it ignore the back key then?”.

@noel

Please see pictures and videos. As you can see, the ‘println’ result comes out, but the app is terminated immediately.

@GWAK @noel ===
as for the 1 point: code from @noel works (i have tested it with kitKat but i dont think that this is important; however i ll test ASAP with MM); it works because P5 has added (i dont know when and why) onBackPressed() from its own in the MainActivity. Yet, if you dont call this method in your fragment code the result is the standard one: the activity is killed. “4” is absolutely normal, it means that the backpressed event was detected (“3” is for home, “4” for back)
as for the second point i cannot see another way to solve that except doing what i have explained a) testing wether the package is present b) if not asking the user to download it and install it

1 Like

@GWAK
I do understand that it reads the key Code in keyPress().
You can use this to store some bars in onPause().
But I am talking about the onBackPressed() function.
If you just write the function without any instruction in it. It should block the back key. Maybe you can try this on other devices, because it is possible, that after Lollipop Android “fixed” the trick because it is widely “not” recommended.
I only use it because the virtual back button on my device is very close to the bottom, and I am touching it unintentionally all the time. Very annoying! I would like to know if that is the case.(not working after Lollipop)

@akenaton, @noel

I know about onPause (), onBackPressed ().
I know the above function works when I press the back button.
So far, the only way to stop it is to re-run it.

But what I want is … (to see video)
If you look at the video, pressing the back button does not turn off the application and the diolog function is turned off.
I want to make it this way. Is there a way?

@noel === i tested with MM, your code works the same as i thought
@GWAK ==== can you put the real code you are using? -

2 Likes

Hi guys, I just read the post and I can’t contribute too much, but using an android 6 my tablet works badly using processing, maybe that is relevant (the android version)

@akenaton

What code content do you want?

void onBackPressed () {
   Intent intent = new Intent ();
   intent.setClass (act, act.getClass ());
   act.finish ();
   act.startActivity (intent);
}

Using the above source code, you can prevent it from being rerun. But what I want is not this.

What I want is when a user uses multiple pages,
You don’t want the app to shut down right away, but you want to go back to the previous page.

@GWAK
Sir… You are confusing me. Are you using AS with processing as lib to work with multiple pages?

@Waboqueox
What do you mean by “works badly”?
I am using APDE. A great app, entirely build on the work of the Processing for Android group. It has however the advantage of previewing the sketch, which runs the sketch in seconds. As a hobby programmer, debugging all the time this is important.
I don’t use a desktop anymore.