# Keyboard not being read

**URL:** <https://discourse.processing.org/t/keyboard-not-being-read/35014>\
**Category:** Coding Questions\
**Created:** [February 6, 2022, 10:21pm UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014 "2022-02-06T22:21:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [February 6, 2022, 10:21pm UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/1 "2022-02-06T22:21:20Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 6, 2022, 11:02pm UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/2 "2022-02-06T23:02:40Z")

</div>

Please show keyPressed function

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 6, 2022, 11:30pm UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/3 "2022-02-06T23:30:51Z")

</div>

> [@JoseMY](#):
>
> when ANY key is pressed, but it never changes.

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

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 7, 2022, 2:02am UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/4 "2022-02-07T02:02:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [February 7, 2022, 7:31am UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/5 "2022-02-07T07:31:37Z")

</div>

Hi!

This is the complete Draw():

```auto
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.

```auto
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).

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 7, 2022, 7:39am UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/6 "2022-02-07T07:39:31Z")

</div>

> [@JoseMY](#):
>
> ```auto
> if (spritesLoaded==0) {
> outString("Sprites Loading",32,0);  
> status=STATUSINTRO;
> 
> ```

Suspicious

Do you set spritesLoaded to 1 ?

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [February 7, 2022, 8:51am UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/7 "2022-02-07T08:51:06Z")

</div>

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

```auto
  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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 7, 2022, 4:59pm UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/8 "2022-02-07T16:59:49Z")

</div>

then this is solved…

---

<div class="post-metadata">

**Author:** ![Lapov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lapov/32/10630_2.png) [@Lapov](https://discourse.processing.org/u/Lapov)\
**Post date:** [July 2, 2024, 8:35am UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/9 "2024-07-02T08:35:23Z")

</div>

> [@JoseMY](#):
>
> ` NokiaScreen.beginDraw();`

@JoseMY  
Hey,  
I am curious if you’ve used in that project some library/class to drive Nokia (5110?) display from Processing by any chance? I’m trying to google something without luck so far…

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [July 2, 2024, 12:43pm UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/10 "2024-07-02T12:43:47Z")

</div>

Sorry, I did not put a 5110 screen driver in processing. I merely made a filter to turn a black-and-white screen into colors that resemble those of Nokia 5110.  
I was entering a Nokia jam in [itch.io](http://itch.io). That is a yearly jam. If you are interested, look for nokia jam in [itch.io](http://itch.io).

---

<div class="post-metadata">

**Author:** ![Lapov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lapov/32/10630_2.png) [@Lapov](https://discourse.processing.org/u/Lapov)\
**Post date:** [July 2, 2024, 6:36pm UTC](https://discourse.processing.org/t/keyboard-not-being-read/35014/11 "2024-07-02T18:36:03Z")

</div>

@JoseMY  
Ah, I see… I’ll try to dig further then  
Thanks for pointing at Nokia jam – looks interesting. 😊

btw at the moment the only code I found related to processing driving 5110 is this

> <https://github.com/noxo/SnakePI4J/blob/master/src/org/noxo/devices/driver/JPCD8544.java>
