# Virtual keyboard problem

**URL:** <https://discourse.processing.org/t/virtual-keyboard-problem/17149>\
**Category:** Processing for Android\
**Created:** [January 17, 2020, 7:24pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149 "2020-01-17T19:24:24Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperos](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@camperos](https://discourse.processing.org/u/camperos)\
**Post date:** [January 17, 2020, 7:24pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/1 "2020-01-17T19:24:24Z")

</div>

google translate…  
hi, I have a strange problem with entering characters with the virtual keyboard,  
when I use uppercase, before the desired character an unknown character is inserted, square with an X inside  
I tried everything, I changed tablet, updated sdk, updated android, updated processing but the problem remained.  
Thanks for your help

```auto
String msg="";
boolean keyboard = false;
void setup() {
  fullScreen();
  textFont(createFont("SansSerif", 40 * displayDensity));
  fill(0);
}    
void draw() {
  background(255);
  text(msg,20, height/2);
}
void keyPressed() {
  background(200, 50, 30);  
msg+=key;
}
void mousePressed() {
  if (!keyboard) {
    openKeyboard();
    keyboard = true;
  } else {
    closeKeyboard();
    keyboard = false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 18, 2020, 2:31am UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/2 "2020-01-18T02:31:18Z")

</div>

only play on PC / win 10 / processing 3.5.4.

```auto
String msg="";

void setup() {
  size(200, 200);
  fill(0);
}    

void draw() {
  background(255);
  text(msg, 20, height/2);
}

void keyPressed() {
  if ( key == CODED ) {
    //println(key, keyCode);
  } else {
    msg+=key;
  }
}

```

 ![key_shift](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/aec3f70efba5ade992731756ebeb172b2d0ea9a4.png)

---

<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:** [January 18, 2020, 2:47am UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/3 "2020-01-18T02:47:14Z")

</div>

Hi @camperos, welcome to the forum.  
It’s obviously a bug.  
My workaround is

```auto
If (key != '') msg += key;

```

What are you planning. If it is an edit box you are better of with an android java edit box. For instance If you want to use a back space you can do so by keeping track of the textWidth of each character and blank it out with a rect. There are other problems making it difficult. If you want a code for an android java edit box I can post it. Just let me know.

Edit: In code above the “notdef” character is not printed.

---

<div class="post-metadata">

**Author:** ![camperos](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@camperos](https://discourse.processing.org/u/camperos)\
**Post date:** [January 18, 2020, 4:27pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/4 "2020-01-18T16:27:29Z")

</div>

![Screenshot_2020-01-18-17-04-42](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4454ddf426d3bf58542f12892beb5178c38f7118.png)

thanks to all but it didn’t work, there are always “notdef” characters, i don’t want them

I typed:  
asdQWERTY

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [January 18, 2020, 4:34pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/5 "2020-01-18T16:34:53Z")

</div>

Might be you need to use getchar() instead I remember getting a similar problem when coding a basic textbox.

---

<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:** [January 18, 2020, 5:39pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/6 "2020-01-18T17:39:17Z")

</div>

Are you sure you put this “notdef” character between the single quotes in my code above? It solves the problem on all my devices. What android version are you using, and on what device.?

---

<div class="post-metadata">

**Author:** ![camperos](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@camperos](https://discourse.processing.org/u/camperos)\
**Post date:** [January 18, 2020, 6:53pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/7 "2020-01-18T18:53:18Z")

</div>

![Image 1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b49a3d1431f23c822eeba7c078ed8a86a60343e4.jpeg)

the “notdef” (unwanted) is this how can I insert it?  
`ï¿¿`

android 6.0.1

but with a notepad with different android version same problem

---

<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:** [January 18, 2020, 7:04pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/8 "2020-01-18T19:04:57Z")

</div>

Edited  
A “notdef” character is a square with a x in it.  
Now I see that you have other characters.  
What language is your device(keyboard)?  
Try:

```auto
 if(key != "ï¿¿") msg += 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:** [January 18, 2020, 7:58pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/9 "2020-01-18T19:58:32Z")

</div>

> [@paulgoux](#):
>
> Might be you need to use getchar() instead

Could you elaborate?  
Something like

```auto
String str = getChar(keyCode);

```

is undefined.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [January 18, 2020, 10:15pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/10 "2020-01-18T22:15:59Z")

</div>

my bad, just use` str(key)`

---

<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:** [January 18, 2020, 10:23pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/11 "2020-01-18T22:23:37Z")

</div>

It’s also in processing when you don’t check against CODED

It’s the shift key

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [January 19, 2020, 6:11pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/12 "2020-01-19T18:11:13Z")

</div>

@Chrisir, @camperos , Yes, it s the shitft key, but with android it is not coded as ==SHIFT; it is coded with= 59 and i think , like noel, that you have to create an edit text (android snippet) and add some keyEyEventListener(); i would try that ASAP, and tell you.

---

<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:** [January 19, 2020, 6:21pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/13 "2020-01-19T18:21:38Z")

</div>

Or check against CODED ? Or against keyCode==59 ?

---

<div class="post-metadata">

**Author:** ![camperos](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@camperos](https://discourse.processing.org/u/camperos)\
**Post date:** [January 20, 2020, 1:23am UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/14 "2020-01-20T01:23:25Z")

</div>

with your suggestions I think I have solved it, now I have to try sending the text to arduino

```auto
//String notDef="ï¿¿";
String msg="";
String msg1="";
boolean keyboard = false;
void setup() {
  fullScreen();
  textFont(createFont("SansSerif", 40 * displayDensity));
  fill(0);
}    
void draw() {
  background(255);
  text(msg, 20, height/2);
}
void keyPressed() {
  background(200, 50, 30);  
// msg+=str(key);
if (keyCode==SHIFT)
  {
    msg1+=key;
  } else {
    msg+=key;
  }
    println(msg);
}
void mousePressed() {
  if (!keyboard) {
    openKeyboard();
    keyboard = true;
  } else {
    closeKeyboard();
    keyboard = false;
  }
}

```

I did it … but it is not clear to me why if in the IF I put msg1, it becomes correct msg.  
thank you all 😁

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [January 20, 2020, 9:30am UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/15 "2020-01-20T09:30:50Z")

</div>

@camperos ===  
you dont need your mess1, which stores the shift modifier event, which is useless;  
As for your question that is easy to understand: Shift is a key event that you can capture but it s not a char and if you ask to draw it you get what you have said…I would add that the problem (and the result) is the same for keyEvents of the same kind, like “delete”

So in a more simple way:

```auto
void keyPressed() {
  background(200, 50, 30); 
  if(keyCode!=SHIFT){
msg+=key;
  }
}

```

---

<div class="post-metadata">

**Author:** ![camperos](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@camperos](https://discourse.processing.org/u/camperos)\
**Post date:** [January 20, 2020, 7:21pm UTC](https://discourse.processing.org/t/virtual-keyboard-problem/17149/16 "2020-01-20T19:21:21Z")

</div>

perfect thanks.  
to complete, even if I don’t currently need it, the “backspace” button also has problems,  
I forgot to mention that my sketch is based on an official example.  
[https://android.processing.org/reference/keyboard/virtual.html](https://android.processing.org/reference/keyboard/virtual.html)
