How does one detect ENTER/BACKSPACE keys in Android?

Before I begin, here is a link to my current code: My Code

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

1 Like

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

void draw(){} // mandatory

void mousePressed(){

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

Here is my output:

keyCode = 66
keyCode = 67

So, what do you actually need more?

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

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

void keyPressed(){
    if (key>31 && key<127 && t.length()<10){  

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

String t = "abcde";

void setup() {
  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 ();

1 Like

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.


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

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