Restart sketch for keyboard event

My code has a function that opens and closes the keyboard. I want it to be so that the sketch restarts or at least the dimensions change. I’m happy to provide more info if anybody needs.

A lot more info, please :smiley:

EnhancedLoop7

1 Like

@KTibow ===
not sure to understand: the user enters something with the keyboard then he closes it and at this moment you want that the app restarts.??? - Is it what you want? - If yes that is possible using packageManager, creating some pending intent adding it to an alarm service with a delay, and finally calling finish or system.exit().

When the keyboard opens, I want it so that the sketch’s height reflects the area not covered by the keyboard.

Hi KTibow,

You can maybe find an answer here: https://forum.processing.org/one/topic/is-there-a-way-to-resize-the-display-window-during-the-program-execution.html

1 Like

@KTibow I believe that what @jb4x metioned might be what you’re looking for!

EnhancedLoop7

@jb4x and @EnhancedLoop7 , how would I know the height of the keyboard?

@KTibow===

you cannot directly know it; yet you can a) get the height from the editext b) get the size of the rect which is NOT occupated by the keyboard= that is (roughly!) done using android method getWindowVisibleDisplayFrame® where r is this rect, to which you can apply r.height(); then, knowing display dimensions (according to the orientation) you can get the keyboard height : kbH= (displayHeight -(editheight+ rectH); i can put code for that, yet the problem is that in my code i create the input edit text by code (posted in the previous forum) and i dont know if it is your case.

I don’t really understand that. Could you make an example sketch where a rectangle is centered and the keyboard opens and closes every 3 seconds?

@KTibow===

can you put the code you have written till now? Have you tried with the code i have posted in the “old” forum in order to create an editText?

Code:

import android.os.Bundle;
import android.app.Activity;
import java.io.FileNotFoundException;
int textSize = 20;//should recompile
int textSize2 = 20;//should recompile
int textSize3 = 20;//should recompile
int textSize4 = 20;//should recompile
int textSize5 = 20;//should recompile
int textSize6 = 20;
int textSize7 = 20;
int page = 1;//should save
int leftBox = 0;
int rightBox = 0;
boolean leftBoxLoaded = false;
boolean rightBoxLoaded = false;
float speed = 0.1;//should recompile
float x = -20;//should save
boolean sucessful = false;//should save
boolean internet = true;
int numPings = 0;//should save
boolean timedOut = false;//should save
class textBox {
  
}
class sizeNText {
  String text;
  int size;
  sizeNText(String text, int size) {
    this.text = text;
    this.size = size;
  }
}
class ipBox {
  int contents;
  int x;
  int y;
  boolean open = false;
  ipBox(int x, int y) {
    this.x = x;
    this.y = y;
    this.contents = 1000;
  }
  void handlePress(int x, int y) {
    textSize(height/16);
    if (this.x < x && this.y < y) {
      if (x < this.x+textWidth("100") && y < this.y+height/16) {
        openKeyboard();
        open = true;
      } else {
        closeKeyboard();
        open = false;
      }
    } else {
      closeKeyboard();
      open = false;
    }
  }
  void handleKey() {
    if (open) {
      println("Key is num "+str(int(key)));
      if (key == '0' || key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9') {
        if (contents < 26) {
          if (contents == 25) {
            if (int(str(key)) < 6) {
              contents = int(str(contents)+key);
              numPings = 0;
            }
          } else {
            contents = int(str(contents)+key);
            numPings = 0;
          }
        } else if (contents == 1000) {
          contents = int(""+key);
        }
      } else if (key == char(65535)) {
        if (str(contents).length() == 1 || contents == 1000) {
          contents = 1000;
        } else {
          contents = int(str(contents).substring(0, str(contents).length()-1));
          numPings = 0;
        }
      }
    }
  }
  void render() {
    stroke(0);
    fill(255);
    textSize(height/16);
    if (open) {
      strokeWeight(10);
    } else {
      strokeWeight(5);
    }
    rect(this.x, this.y, textWidth("100"), height/16);
    fill(0);
    if (contents != 1000) {
      text(str(contents), this.x, this.y+height/16);
    }
  }
}
class changingText {
  String Text = "";
  int x = 0;
  int y = 0;
  int textSize = 0;
  changingText() {
  }
  void setText(int textSize, int x, int y, String text) {
    this.textSize = textSize;
    this.x = x;
    this.y = y;
    this.Text = text;
  }
  void render() {
    //println("Render was called");
    textSize(this.textSize);
    fill(255, 255, 255);
    text(this.Text, this.x, this.y);
  }
}
ipBox text = new ipBox(0, 0);
ipBox text2 = new ipBox(0, 0);
changingText pings = new changingText();
void setup() {
  fullScreen();
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth("SecurePythonPi") > width) {
      i = i / 2;
      textSize = i;
      break;
    }
  }
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth("Search") > width) {
      i = i / 2;
      textSize2 = i;
      break;
    }
  }
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth("Searching...") > width) {
      i = i / 2;
      textSize3 = i;
      break;
    }
  }
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth("Found the device!") > width) {
      i = i / 2;
      textSize4 = i;
      break;
    }
  }
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth("There's no internet connection.") > width) {
      i = i / 2;
      textSize6 = i;
      break;
    }
  }
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth("There's an internet connection!") > width) {
      i = i / 2;
      textSize7 = i;
      break;
    }
  }
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth("No devices found on that port, sorry. :~(") > width) {
      i = i - 2;
      textSize5 = i;
      break;
    }
  }
  speed = width/300;
  textSize(height/16);
  text = new ipBox(int((width/4)-(textWidth("100")/2)), height/2);
  text2 = new ipBox(int((width/4*3)-(textWidth("100")/2)), height/2);
  if (rightBoxLoaded) {
    text2.contents = rightBox;
  }
  if (leftBoxLoaded) {
    text.contents = leftBox;
  }
}
void draw() {
  strokeWeight(5);
  if (page == 1) {
    stroke(0);
    background(100);
    textSize(textSize);
    fill(255);
    text("SecurePythonPi", (width/2)-textWidth("SecurePythonPi")/2, textSize);
    textSize(textSize2);
    fill(100);
    strokeWeight(5);
    rect(width/2-(textWidth("Search")/2), ((textSize2)*0.5)+textSize, textWidth("Search"), textSize2);
    fill(255);
    text("Search", (width/2)-textWidth("Search")/2, ((textSize2)*1.375)+textSize);
    fill(255);
    if (loadStrings("https://google.com") != null) {
      textSize(textSize7);
      
      text("There's an internet connection!", (width/2)-textWidth("There's an internet connection!")/2, (textSize2*1.5)+textSize+textSize7);
    } else {
      textSize(textSize6);
      text("There's no internet connection.", (width/2)-textWidth("There's no internet connection.")/2, (textSize2*1.5)+textSize+textSize6);
    }
  } else if (page == 2) {
    background(100);
    stroke(0);
    line(0, height/10, width, height/10);
    line(width/2-(((height/10)-(height/32))/2), height/64, width/2+(((height/10)-(height/32))/2), height/10-height/64);
    line(width/2+(((height/10)-(height/32))/2), height/64, width/2-(((height/10)-(height/32))/2), height/10-height/64);
    textSize(textSize3);
    fill(255);
    text("Searching...", (width/2)-textWidth("Searching...")/2, textSize3+height/10);
    noStroke();
    ellipse(x, textSize3+height/8, 20, 20);
    x += speed;
    if (x > width+20 || x < -20) {
      speed *= -1;
    }
    if (sucessful) {
      background(100);
      stroke(0);
      line(0, height/10, width, height/10);
      line(width/2-(((height/10)-(height/32))/2), height/64, width/2+(((height/10)-(height/32))/2), height/10-height/64);
      line(width/2+(((height/10)-(height/32))/2), height/64, width/2-(((height/10)-(height/32))/2), height/10-height/64);
      textSize(textSize4);
      fill(255);
      text("Found the device!", (width/2)-textWidth("Found the device!")/2, textSize4+height/10);
    } else if (timedOut) {
      background(100);
      stroke(0);
      line(0, height/10, width, height/10);
      line(width/2-(((height/10)-(height/32))/2), height/64, width/2+(((height/10)-(height/32))/2), height/10-height/64);
      line(width/2+(((height/10)-(height/32))/2), height/64, width/2-(((height/10)-(height/32))/2), height/10-height/64);
      textSize(textSize5);
      fill(255);
      text("No devices found on that port, sorry. :~(", (width/2)-textWidth("No devices found on that port, sorry. :~(")/2, textSize5+height/10);
    } else if (!internet) {
      background(100);
      stroke(0);
      line(0, height/10, width, height/10);
      line(width/2-(((height/10)-(height/32))/2), height/64, width/2+(((height/10)-(height/32))/2), height/10-height/64);
      line(width/2+(((height/10)-(height/32))/2), height/64, width/2-(((height/10)-(height/32))/2), height/10-height/64);
      textSize(textSize6);
      fill(255);
      text("There's no internet connection.", (width/2)-textWidth("There's no internet connection.")/2, textSize6+height/10);
    } else {
      sizeNText theData = formatPings();
      textSize(theData.size);
      pings.setText(theData.size, int((width/2)-(textWidth(theData.text)/2)), height-theData.size, theData.text);
      text.render();
      text2.render();
      pings.render();
    }
  }
}
void checkIP() {
  String[] answer = null;
  int startTime = millis();
  internet = loadStrings("https://google.com") != null;
  if (internet) {
    while (answer == null && millis()-startTime<30000 && numPings <= 10) {
      println("http://192.168."+text.contents+"."+text2.contents+":5000/");
      answer = loadStrings("http://192.168."+text.contents+"."+text2.contents+":5000/");
      internet = loadStrings("https://google.com") != null;
      if (answer != null) {
        sucessful = true;
        println("Sucess!");
      } else {
        println("No sucess.");
        numPings += 1;
        int lastMillis = millis();
        while (millis() - 2000 < lastMillis) {
          int abc = 5;
          abc = abc - 1;
        }
      }
    }
    timedOut = answer == null;
  }
}
sizeNText formatPings() {
  String returnVal;
  boolean isOne = numPings == 1;
  int theTextWidth = 20;
  if (isOne) {
    returnVal = "1 ping";
  } else {
    returnVal = str(numPings)+" pings";
  }
  for (int i = 1; i < 1000; i++) {
    textSize(i);
    if (textWidth(returnVal) > width) {
      i = i / 2;
      if (height-(i*2) < (height/2+height/16)) {
        i = (height-(height/2+height/16))/2;
      }
      theTextWidth = i;
      break;
    }
  }
  return new sizeNText(returnVal, theTextWidth);
}
void mousePressed() {
  closeKeyboard();
  if (page == 1) {
    textSize(textSize2);
    if (mouseX > width/2-(textWidth("Search")/2) &&  mouseX < width/2+(textWidth("Search")/2)) {
      if (mouseY > ((textSize2)*0.5)+textSize && mouseY < ((textSize2)*1.5)+textSize) {
        page = 2;
        sucessful = false;
        numPings = 0;
        text.contents = 1000;
        text2.contents = 1000;
        timedOut = false;
        internet = true;
        thread("checkIP");
        println("Tap ocurred");
      }
    }
  } else if (page == 2) {
    text.handlePress(mouseX, mouseY);
    text2.handlePress(mouseX, mouseY);
    if (mouseY < height/10) {
      page = 1;
      x = -20;
    }
  }
}
void keyPressed() {
  text.handleKey();
  text2.handleKey();
}
//public void onSaveInstanceState(Bundle savedInstanceState){
//    //savedInstanceState.putInt("TheInt", page);
//    print("OnSaveInstanceState");
//    //declare values before saving the state
//    //super.onSaveInstanceState(savedInstanceState);
//    //super.onSaveInstanceState(savedInstanceState);
//}
@Override
  public void onPause() {
  print("OnPause");
  String[] s = new String[7];
  s[0] = str(page);
  s[1] = str(x);
  s[2] = str(sucessful);
  s[3] = str(timedOut);
  s[4] = str(numPings);
  println(text.contents);
  println(text2.contents);
  s[5] = str(text.contents);
  s[6] = str(text2.contents);
  saveStrings("data.txt", s);
  super.onPause();
}
@Override
  public void onStart() {
  print("OnStart");
  String[] s = new String[7];
  File f = new File(sketchPath("data.txt"));
  boolean loaded = f.exists();
  if (loaded) {
    s = loadStrings("data.txt");
    if (s.length == 7) {
      page = int(s[0]);
      x = int(s[1]);
      numPings = int(s[4]);
      leftBox = int(s[5]);
      rightBox = int(s[6]);
      leftBoxLoaded = true;
      rightBoxLoaded = true;
      timedOut = boolean(s[3]);
      sucessful = boolean(s[2]);
      if (page == 2 && !sucessful && !timedOut) {
        thread("checkIP");
      }
    }
  }
  super.onStart();
}

@KTibow===
where is the code for the “openKeyboard()” call? - This code tests the internet conn. and (i suppose) try to loadStrings() from a socket; is it what is supposed to be entered by the user using the keyboard?

Class ipBox's function handlePress.

@KTibow===
yes, i have seen: but where is the code for this call?

https://android.processing.org/reference/keyboard/virtual.html

Hope that that helps.

@KTibow====

ok; now i understand: this method is supposed to be working with P5 and if it works that’s good! - yet, in this case, as you dont create (by code) the keyboard, you cannot do nothing (i think); what i said (/your question) is that if you want to customize the keyboard you have to create it by yourself, without this method and using only native android code, which is not a problem - Doing so all of the params (and you can know its height and so on) can be changed: that s all!

:smiley: got it. Thanks! Wondering, do you have an example?

@KTibow===
i have posted code for an editext on the previous forum.

I believe akenaton is referring to:

Using class EditText with processing? - Processing 2.x and 3.x Forum

How to manually create Android TextField in Processing ? - Processing 2.x and 3.x Forum

Kf