We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › on screen keyboard for touch screen
Page Index Toggle Pages: 1
on screen keyboard for touch screen (Read 931 times)
on screen keyboard for touch screen
Feb 20th, 2010, 10:49am
 
I am working on building an on screen keyboard to use with a touch interface.

I was wondering if anyone had any suggestions?

Currently I am writing a button class and instancing 26 objects of that class. Each mousePress (simulating the users touch) is checking if it sits inside any of those objects.

Does any one have a better method for doing this?  Or a quick way I can create all my button instances?  I am currently doing something like:

 q = new Button('q', xpos, ypos);
 w = new Button('w', xpos, ypos);

etc.

Haven't quite figured out the best way to generate the xpos/ypos info yet.

Thanks,
TB
Re: on screen keyboard for touch screen
Reply #1 - Feb 20th, 2010, 12:31pm
 
Code:
String[] chars = { "a", "b", ... "z", ",", ".", "!" ... };
Button[] touches = new Button[chars.length];
for (int i = 0; i < chars; i++) {
// If pos can be found algorithmically
int xpos = ...;
int ypos = ...;
touches[i] = new Button(chars[i], xpos, ypos);
}
Re: on screen keyboard for touch screen
Reply #2 - Feb 21st, 2010, 7:20pm
 
Thanks!

I have implemented your code and it works great.

I am now working on a method in the button class that returns the corresponding key of that button.

Code:

char keyCheck(int Xmouse, int Ymouse){
   //check if click was inside key
   if(Xmouse > x && Xmouse < x + keySize
&& Ymouse > y && Ymouse < y + keySize){
    return keyVal;
   }
   return null;
 }


Basically - i only want the key value returned if the click falls within the button area, otherwise I don't want anything returned.  The above code gives the error:

'cannot convert from null to char'

Any advice?

Thanks,
TB
Re: on screen keyboard for touch screen
Reply #3 - Feb 21st, 2010, 7:47pm
 
hey terry, just curious. what are you working on ?
Re: on screen keyboard for touch screen
Reply #4 - Feb 21st, 2010, 10:11pm
 
char is a primitive type, actually represented by the Unicode (or Ascii, if you prefer) value of the character.
So you can't return null in place of a char.
If you used Strings as I did, you could return null...
Or return Character type, the class wrapper of the char type.
Or return a special char value, eg. \uFFFF (-1) which is an invalid Unicode value.
Re: on screen keyboard for touch screen
Reply #5 - Feb 21st, 2010, 11:40pm
 
Hrmm....i think I am getting a little in over my head.

I converted the char to a String using Character.toString - now I cant figure out why this string prints fine from within the button class, but always returns a string with content "null", or infact any string i place in that else{return ... } section.

Code:
char[] chars = new char[26];
Button[] touches = new Button[chars.length];

int charVal = 97;
int xpos = 10;
int ypos = 10;
String lastPress;

void setup(){
 size(930,200);
 for (int i = 0; i < chars.length; i++) {
   chars[i] = char(charVal);
   charVal++;
   touches[i] = new Button(chars[i], xpos, ypos);
   xpos += 35;
 }
}

void draw(){
 for (int i = 0; i < touches.length; i++){
   touches[i].display();
 }
}

void mouseReleased(){
 for (int i = 0; i < touches.length; i++){
   lastPress = touches[i].keyCheck(mouseX, mouseY);
 }
println(lastPress); //prints "null"
}


Code:
class Button {

 String charString;
 int x;
 int y;
 int keySize = 30;

 Button(char keyVal, int x, int y){
   charString = Character.toString(char(keyVal));
   this.x = x;
   this.y = y;
 }

 void display(){
   rect(x,y,keySize,keySize);
 }

 String keyCheck(int Xmouse, int Ymouse){
   if(Xmouse > x && Xmouse < x + keySize
       && Ymouse > y && Ymouse < y + keySize){
     println(charString); //prints relevant char
     return charString;
   }  else {
     return null;
   }
 }
}

Re: on screen keyboard for touch screen
Reply #6 - Feb 21st, 2010, 11:47pm
 
Cedric wrote on Feb 21st, 2010, 7:47pm:
hey terry, just curious. what are you working on


Working on an installation that allows a user to generate their own unique piece of art (in processing).  I then want the user to have the option of emailing a saved frame to themselves.  I have access to a touch screen and thought it'd be a tidier way to give them this option than a wired keyboard.  Will also give me more control over what they can enter.

And I've just started learning OOP and thought it'd be a good project to try and capitalize on that.
Re: on screen keyboard for touch screen
Reply #7 - Feb 22nd, 2010, 1:33am
 
The idea was to have chars as a String array too, so you don't have char type anymore.
It is more annoying the hand-write the full alphabet, but for e-mail, you have to add some additional chars anyway (@ . - _ as a strict minimum).
Page Index Toggle Pages: 1