# How does one detect ENTER/BACKSPACE keys in Android?

**URL:** <https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504>\
**Category:** Processing for Android\
**Created:** [February 1, 2020, 10:04pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504 "2020-02-01T22:04:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TechMaster04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/techmaster04/32/14929_2.png) [@TechMaster04](https://discourse.processing.org/u/TechMaster04)\
**Post date:** [February 1, 2020, 10:04pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/1 "2020-02-01T22:04:00Z")

</div>

Before I begin, here is a link to my current code: [My Code](https://1drv.ms/u/s!AnXXk5PX0LXn6kSAjQMKYwyNzaMp?e=1e47So)

Okay so: I am porting my Processing Java project to my Android phone (S7 Edge). Normally, I’d use if (key == ENTER) in void keyPressed to detect the Enter key being pressed (on a wireless keyboard: Logitech K380), however when I ran it on my phone it did not work as planned. Same goes for the BACKSPACE key. The code is a queue manager (This is what I like to call a school project taken too far), so the Enter and Backspace keys are crucial. I tried many ways to figure this out, like printing the key to the console, and LOTS of Google searching, with no luck sadly.

Anyone know how to detect these key inputs?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [February 2, 2020, 4:08am UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/2 "2020-02-02T04:08:07Z")

</div>

I understand you already have tested this, but what is your output when you run this code?  
(I’m using APDE)

```auto
void draw(){} // mandatory

void mousePressed(){
  openKeyboard();
}

void keyPressed(){
  println("keyCode = "+keyCode); 
  if(keyCode == 67) println("Backspace"); 
  if(keyCode == 66) println("Enter"); 
}

```

---

<div class="post-metadata">

**Author:** ![TechMaster04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/techmaster04/32/14929_2.png) [@TechMaster04](https://discourse.processing.org/u/TechMaster04)\
**Post date:** [February 2, 2020, 6:06pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/3 "2020-02-02T18:06:13Z")

</div>

Here is my output:

```auto
keyCode = 66
Enter
keyCode = 67
Backspace

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [February 2, 2020, 8:22pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/4 "2020-02-02T20:22:50Z")

</div>

So, what do you actually need more?

---

<div class="post-metadata">

**Author:** ![TechMaster04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/techmaster04/32/14929_2.png) [@TechMaster04](https://discourse.processing.org/u/TechMaster04)\
**Post date:** [February 11, 2020, 11:08pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/5 "2020-02-11T23:08:17Z")

</div>

Oh sorry I figured it out but forgot to mark it as soloution

---

<div class="post-metadata">

**Author:** ![dinoelectro](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@dinoelectro](https://discourse.processing.org/u/dinoelectro)\
**Post date:** [April 13, 2020, 2:04pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/6 "2020-04-13T14:04:38Z")

</div>

hi, noel

I was experimenting with the code, but found a problem

for example if I type 5 letters, I need to press the backspace key six times to just delete a character

for example if I type 10 letters, I need to press the backspace key eleven times to just delete a character

and so on etc

```auto
void keyPressed(){
  if(keyCode==67){//backspace
    if(t.length()>0)t=t.substring(0,max(0,t.length()-1));
  }else{
    if (key>31 && key<127 && t.length()<10){  
      t+=key;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 13, 2020, 7:04pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/7 "2020-04-13T19:04:08Z")

</div>

Hi @dinoelectro  
What device are you using and with what Android mode? Code below is running well on my Samsung S4 Lollipop.

```auto
String t = "abcde";

void setup() {
  textSize(60);
  textAlign (CENTER);
  fill (0);
}

void draw () {
  background (255);
  text (t, width/2, height/2);
}

void keyPressed() {
  if (keyCode == 67) { //backspace
    if (t.length() > 0) t = t.substring(0, max(0, t.length()-1));
  } else {
    if (key > 31 && key < 127 && t.length() < 10) {  
      t += key;
    }
  }
}

void mousePressed () {
  openKeyboard ();
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 13, 2020, 9:52pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/8 "2020-04-13T21:52:10Z")

</div>

I tested the same code on my Samsung tablet E 9.6 Kitkat 4.4 and it gave the error you mentioned.  
I installed the Hacker’s keyboard (Play Store) and the code did run correctly again.  
I was not able to find a workaround.

---

<div class="post-metadata">

**Author:** ![dinoelectro](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@dinoelectro](https://discourse.processing.org/u/dinoelectro)\
**Post date:** [April 15, 2021, 9:16pm UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/9 "2021-04-15T21:16:44Z")

</div>

> [@noel](#):
>
> ```auto
> String t = "abcde";
> 
> void setup() {
> textSize(60);
> textAlign (CENTER);
> fill (0);
> }
> 
> void draw () {
> background (255);
> text (t, width/2, height/2);
> }
> 
> void keyPressed() {
> if (keyCode == 67) { //backspace
> if (t.length() > 0) t = t.substring(0, max(0, t.length()-1));
> } else {
> if (key > 31 && key < 127 && t.length() < 10) {  
> t += key;
> }
> }
> }
> 
> void mousePressed () {
> openKeyboard ();
> }
> 
> ```

I my friend, have you found a solution to this problem?

---

<div class="post-metadata">

**Author:** ![MPROCD](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@MPROCD](https://discourse.processing.org/u/MPROCD)\
**Post date:** [April 20, 2021, 9:31am UTC](https://discourse.processing.org/t/how-does-one-detect-enter-backspace-keys-in-android/17504/10 "2021-04-20T09:31:14Z")

</div>

Be aware that keyCodes for Java and Android are different. So if you need your sketch to run correctly in Android and in Java (for testing purposes) you can use both codes. For example if you want to use the ENTER key in Java and Android you can change the line  
if (keyCode == 66) { /ENTER  
to be:  
if (keyCode == 66 || keyCode==10) { //ENTER for Android or Java
