Keyboard not being read

Hi again.

I’m having issues with keyboard not being read. At first, I though my issues were Draw() not looping (or not ending before next loop), but it seems Draw() is running at good fps rate.

My project has a complicate structure, and Draw() is divided this way:

void draw(){
switch (status){
  case INTRO:
      drawIntro();
      break
  case FIRST:
    drawFirstLevel();
/* and so on */ 
}
}

Also, keyPressed() is also divided in a similar way, so I have just one goUp() routine for both keyCode “UP” and Key ‘W’ (and so on).

The case is status should go from INTRO to FIRST when ANY key is pressed, but it never changes.

The issue arosed randomly on previous stages of code, but now it is persistent. The main change was adding a big list of loadImages on a sideloading thread().

Any idea on how to debug?

Is keyPressed() triggered when loading a function from Draw, or should I keep all code inside of Draw to make sure keyPressed() is checked?

Please show keyPressed function

That could also be the case when you accidentally wrote status=0; somewhere

you could print the status every time draw is called to track the variable and also every time a key is pressed

Hi!

This is the complete Draw():

void draw() {
  float ti;
  ti=millis();
  //if (drawing) return;
  drawing=true;
  pushStyle();
  imageMode(CENTER);
  NokiaScreen.beginDraw();
  if (spritesLoaded==0) {
    outString("Sprites Loading",32,0);  
    status=STATUSINTRO;
  } else {
    //status=STATUSFLOOR;
  }
  
  println("Status: "+status);
  switch (status) {
  case STATUSINTRO:
    maxX=1;
    maxY=1;
    waitKeyAny=true;
    drawIntro();
    println("Drawing Intro");
    break;
  case STATUSFLOOR:
    waitKeyAny=false;
    maxX=32*8;
    maxY=8;
    NokiaScreen.background(0);
    println("Drawing floor");
    drawFloor();
    break;
  case STATUSDIALOG:
    case (STATUSDIALOG | STATUSFLOOR):
    waitKeyAny=false;
    maxX=2;
    maxY=1;
    dialog();
    break;
 default:
   outString("STATUS is:"+status,0,32);
}

And this is the complete keypressed and related functions.
Please notice there are many “while” instructions inside. Some hours after my last message I thought the problem could be related to these “while” instructions, but after doing some sanitizing inside, the problem persists.

void keyPressed() {

  if (waitKeyAny|status==STATUSINTRO) {
    println("Any key Pressed"+status);
    if (status==STATUSINTRO) status=STATUSFLOOR;
    return;
  }
  println("Keypressed");
  if (key!=CODED) {
    /* CODED (ASCII) KEY USED */
    switch(key) {
    case 'W': 
    case 'w': 
    case '8':
      doUp();
      break;
    case 'S': 
    case 's': 
    case '2':
      doDown();
      break;
    case 'A': 
    case 'a': 
    case '4':
      myX--;
/* If maxX==0 this causes infinite loop */
      if (maxX>0) {
        while (myX<0) myX+=maxX;
      } else {
          myX=0;
      }
      break;
    case 'D': 
    case 'd': 
    case '6':
      // 84/16=5.25
      if (maxX>0) myX=(myX+1)%maxX;
      
      break;
    case ' ':
    case RETURN:
    case 5:
      /* SELECTION -- Decide what to do */
      if ( (status & STATUSDIALOG)!=0) dialogReturn();
      break;
      case (char) 27:
      /* ESCAPE */
      doEscape();
    }
  } else {
    /* KEYCODE USED */
    switch(keyCode) {
    case UP: 
      doUp();
    case DOWN:
      // 48/16=3
      doDown();
      break;
    case LEFT:
      myX--;
      /* If maxX==0 this causes infinite loop */
      if (maxX>0) {
        while (myX<0) myX+=maxX;
      } else {
          myX=0;
      }
      break;
    case RIGHT:
      // 84/16=5.25
      if (maxX>0) myX=(myX+1)%maxX;
      break;
    case  4:
      /* BACK ANDRODID BUTTON: Exit form mode */
      doEscape();
      break;
    }
  }
}

/* Processes "UP" key:*/
void doUp() {
  if (status==STATUSFLOOR) {
    int selDoor=int(1+myX/32) % 8;
    if (piso[myFloor][selDoor].rtype==HSTAIRS) {
      if (myFloor<8) {
        myFloor++;
      }
    }
  } else {  
    myY--;
      /* If maxY<0 this causes infinite loop */
    if (maxY>0){
      while (myY<0) myY+=maxY;
    }
  }
}

void doDown() {
  if (status==STATUSFLOOR) {
    int selDoor=int(1+myX/32) % 8;
    if (piso[myFloor][selDoor].rtype==HSTAIRS) {
      if (myFloor>0) {
        myFloor--;
      } else {
        status=STATUSDIALOG | STATUSFLOOR;
        dialognum=CONFIRM_EXIT;
        storeFloorX=myX;
        myX=0;
        maxX=1; 
        maxY=1;
      }
    }
  } else {
    if (maxY>0) myY=(myY+1)%maxY;
  }
}
/* Process Escape. I.E. Exit from a Dialog */
void doEscape() {
  if ((status & STATUSDIALOG)!=0) {
    switch (status ^STATUSDIALOG) {
    case STATUSFLOOR:
      // XOR StatusDIALOG bit
      status^=STATUSDIALOG;
      if (status==STATUSFLOOR) {
        myX=storeFloorX;
      }
      break;
    }
  }
}

void dialogReturn() {
  switch (dialognum) {
  case CONFIRM_EXIT:
    if (myX==0) {
      status=STATUSEXITGAME;
      return;
    } else {
      // XOR StatusDIALOG bit
      status^=STATUSDIALOG;
      if (status==STATUSFLOOR) {
        myX=storeFloorX;
      }
    }
    break;
  }
}

REMOVING println() lines inside draw() showed keypress was working (but their printlns were hidden by the many printlns inside keyPress).

Suspicious

Do you set spritesLoaded to 1 ?

Yes, I did. In fact, I was checking that as primary suspect.
I finally found the typo here:

  case 5:
      /* SELECTION -- Decide what to do */
      if ( (status & STATUSDIALOG)!=0) dialogReturn();
      break;

I used 5 (char 0x05) instead of ‘5’ (char 0x35) and it was triggering doReturn() and thus resetting status to 0.

The problem about having to click the sketch window to make keys work is still there.

Please excuse my awful english. I edited the previous version of this comment but I think my grammar is still confusing.

1 Like

then this is solved…