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 › Keypress thingy...
Page Index Toggle Pages: 1
Keypress thingy... (Read 2220 times)
Keypress thingy...
Mar 27th, 2007, 10:27pm
 
Hi,

Im working on a project that involves keypresses. I found the keypress example and tried playing around with it, but cant figure out how to use each key for a different sprite rather than pressing a key and different sprites come one after another.

Im looking to use each keypress as a different sprite on a different position on the screen so eg.

'A' = sprite 1 (0,10) --grid location--
'B' = sprite 2 (0,20) --grid location--
and so on...

I have this so far:

int max_height = 20;
int min_height = 20;
int letter_height = max_height; // Height of the letters
int letter_width = 20;          // Width of the letter

int x = -letter_width;          // X position of the letters
int y = 0;                      // Y position of the letters

boolean newletter;              

int numChars = 26;      // There are 26 characters in the alphabet
color[] colors = new color[numChars];

void setup()
{
 size(480, 480);
 for(int i=0; i<100; i++) {
 float r = random(50);
 stroke(r*90);
 line(50, i, 50+r, i);
}
 
 stroke(random (1255));
 colorMode(RGB, numChars);
 background(numChars/2);
 // Set a gray value for each key
 for(int i=0; i<numChars; i++) {
   colors[i] = color(i, i, i);    
 }
}

void draw()
{
 if(newletter == true) {
   // Draw the "letter"
   int y_pos;
   if (letter_height == max_height) {
     y_pos = y;
     ellipse( x, y_pos, letter_width, letter_height );
   } else {
     y_pos = y + min_height;
     ellipse( x, y_pos, letter_width, letter_height );
     fill(numChars/2);
     ellipse( x, y_pos-min_height, letter_width, letter_height );
   }
   newletter = false;
 }
}

void keyPressed()
{
 // if the key is between 'A'(65) and 'z'(122)
 if( key >= 'A' && key <= 'z') {
   int keyIndex;
   if(key <= 'Z') {
     keyIndex = key-'A';
     letter_height = max_height;
     fill(colors[key-'A']);
   } else {
     keyIndex = key-'a';
     letter_height = min_height;
     fill(colors[key-'a']);
   }
 } else {
   fill(0);
   letter_height = 10;
 }

 newletter = true;

 // Update the "letter" position
 x = ( x + letter_width );

 // Wrap horizontally
 if (x > width - letter_width) {
   x = 0;
   y+= max_height;
 }

 // Wrap vertically
 if( y > height - letter_height) {
   y = 0;      // reset y to 0
 }
}

Just wondered if anyone can help with the keypress coding.

Thanks in advance,

Jason
Re: Keypress thingy...
Reply #1 - Apr 2nd, 2007, 12:17am
 
Anyone?
Re: Keypress thingy...
Reply #2 - Apr 2nd, 2007, 1:17pm
 
Sound like you're confusing a drawing for a sprite. A sprite should have it's own co-ordinates and label. Like so:

Code:

Sprite one, two;
void setup(){
size(400,400);
one = new Sprite(50, 50);
two = new Sprite(50, 150);
smooth();
}
void draw(){
background(30,50,40);
one.draw();
two.draw();
if(keyPressed){
if(key == 'a'){
one.x +=2;
}
if(key == 'b'){
two.x +=2;
}
}
}
class Sprite{
float x, y;
Sprite(float x, float y){
this.x = x;
this.y = y;
}
void draw(){
rect(x, y, 20, 20);
}
}
Re: Keypress thingy...
Reply #3 - Apr 2nd, 2007, 7:36pm
 
I had a look at the above code... and also tried this:

void setup() {
 size(430, 430);
 background(255);
 smooth();
 noStroke();
}
 
void draw() {
 if(keyPressed) {
   if (key == 'q' || key == 'Q') {
     fill(0);
   }
 } else {
   fill(255);
 }
 rect(25, 25, 50, 50);

 if(keyPressed) {
   if (key == 'w' || key == 'W') {
     fill(0);
   }
 } else {
   fill(245);
 }
 rect(80, 25, 50, 50);

if(keyPressed) {
   if (key == 'e' || key == 'E') {
     fill(0);
   }
 } else {
   fill(235);
 }
 rect(135, 25, 50, 50);

if(keyPressed) {
   if (key == 'r' || key == 'R') {
     fill(0);
   }
 } else {
   fill(225);
 }
 rect(190, 25, 50, 50);
 
if(keyPressed) {
   if (key == 't' || key == 'T') {
     fill(0);
   }
 } else {
   fill(215);
 }
 rect(245, 25, 50, 50);
 
 if(keyPressed) {
   if (key == 'y' || key == 'Y') {
     fill(0);
   }
 } else {
   fill(225);
 }
 rect(300, 25, 50, 50);
 
if(keyPressed) {
   if (key == 'u' || key == 'U') {
     fill(0);
   }
 } else {
   fill(215);
 }
 rect(355, 25, 50, 50);
}


But each sprite/rectangle changes colour when I hit a key, its supposed to just change the colour of the chosen sprite/rectangle... anyone?
Re: Keypress thingy...
Reply #4 - Apr 2nd, 2007, 7:56pm
 
This is happening because your nested if tests have "holes" in them, such that a new fill might not be set for each rectangle.  (so whatever previous fill was set last frame is still in effect)  If you instead reword your if statements like this you can plug those holes:

Quote:

 if (keyPressed && (key == 'q' || key == 'Q')) {
    fill(0);
 } else {
   fill(255);
 }
 rect(25, 25, 50, 50);
Re: Keypress thingy...
Reply #5 - Apr 3rd, 2007, 1:15am
 
Thanks so much... working on your example as we speak!
Re: Keypress thingy...
Reply #6 - Apr 3rd, 2007, 6:23pm
 
Worked a treat! Now I have:

void setup() {
 size(375, 375);
 background(255);
 smooth();
 noStroke();
}
 
//First Line 6 x 6
 
void draw() {
   if (keyPressed && (key == '1' || key == '!')) {  
   //if (key == 'q' || key == 'Q') {
     fill(0);
 } else {
   fill(235);
 }
 rect(25, 25, 50, 50);

if (keyPressed && (key == '2' || key == '@')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(80, 25, 50, 50);

if (keyPressed && (key == '3' || key == '£')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(135, 25, 50, 50);

if (keyPressed && (key == '4' || key == '$')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(190, 25, 50, 50);
 
if (keyPressed && (key == '5' || key == '%')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(245, 25, 50, 50);
 
if (keyPressed && (key == '6' || key == '^')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(300, 25, 50, 50);
 

//Second Line


if (keyPressed && (key == '7' || key == '&')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(25, 80, 50, 50);
 
if (keyPressed && (key == '8' || key == '*')) {  
     fill(0);
 } else {
   fill(235);
 }
rect(80, 80, 50, 50);

if (keyPressed && (key == '9' || key == '(')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(135, 80, 50, 50);
 
if (keyPressed && (key == '0' || key == ')')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(190, 80, 50, 50);
 
if (keyPressed && (key == 'q' || key == 'Q')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(245, 80, 50, 50);
 
if (keyPressed && (key == 'w' || key == 'W')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(300, 80, 50, 50);  
 
 //Third Line
 
if (keyPressed && (key == 'e' || key == 'E')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(25, 135, 50, 50);
 
if (keyPressed && (key == 'r' || key == 'R')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(80, 135, 50, 50);
 
if (keyPressed && (key == 't' || key == 'T')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(135, 135, 50, 50);

if (keyPressed && (key == 'y' || key == 'Y')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(190, 135, 50, 50);
 
if (keyPressed && (key == 'i' || key == 'I')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(245, 135, 50, 50);
 
if (keyPressed && (key == 'o' || key == 'O')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(300, 135, 50, 50);
 

//Fourth Line  
 

if (keyPressed && (key == 'p' || key == 'P')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(25, 190, 50, 50);
 
if (keyPressed && (key == 'a' || key == 'A')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(80, 190, 50, 50);
     
if (keyPressed && (key == 's' || key == 'S')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(135, 190, 50, 50);
 
if (keyPressed && (key == 'd' || key == 'D')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(190, 190, 50, 50);
 
if (keyPressed && (key == 'f' || key == 'F')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(245, 190, 50, 50);
 
if (keyPressed && (key == 'g' || key == 'G')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(300, 190, 50, 50);
 
 
//Fifth Line
 
 
if (keyPressed && (key == 'h' || key == 'H')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(25, 245, 50, 50);
 
if (keyPressed && (key == 'j' || key == 'J')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(80, 245, 50, 50);
 
if (keyPressed && (key == 'k' || key == 'K')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(135, 245, 50, 50);
 
if (keyPressed && (key == 'l' || key == 'L')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(190, 245, 50, 50);
 
if (keyPressed && (key == 'z' || key == 'Z')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(245, 245, 50, 50);
 
if (keyPressed && (key == 'x' || key == 'X')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(300, 245, 50, 50);
 

//Sixth Line

 
if (keyPressed && (key == 'c' || key == 'C')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(25, 300, 50, 50);
 
if (keyPressed && (key == 'v' || key == 'V')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(80, 300, 50, 50);
 
if (keyPressed && (key == 'b' || key == 'B')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(135, 300, 50, 50);

if (keyPressed && (key == 'n' || key == 'N')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(190, 300, 50, 50);
 
if (keyPressed && (key == 'm' || key == 'M')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(245, 300, 50, 50);
 
if (keyPressed && (key == '.' || key == '>')) {  
     fill(0);
 } else {
   fill(235);
 }
 rect(300, 300, 50, 50);  
}

I was wondering how to combine something like:

http://processing.org/learning/examples/simpleparticlesystem.html

Into the code... bearing in mind that corresponding keypress would donate that the particle would appear in its grid reference?
Re: Keypress thingy...
Reply #7 - Apr 3rd, 2007, 9:05pm
 
oh-my-god. you will collect lot's a miles scrolling up and down Processing ...

try this:
Code:

KeyRect[] keyrects;

void setup() {
size(375, 375);
background(255);
smooth();
noStroke();
keyrects = new KeyRect[] {
//First Line 6 x 6
new KeyRect(25, 25, 50, 50,'1','!'),
new KeyRect(80, 25, 50, 50,'2','@'),
new KeyRect(135, 25, 50, 50,'3', '£'),
new KeyRect(190, 25, 50, 50,'4', '$'),
new KeyRect(245, 25, 50, 50,'5', '%'),
new KeyRect(300, 25, 50, 50,'6', '^'),
//Second Line
new KeyRect(25, 80, 50, 50,'7', '&'),
new KeyRect(80, 80, 50, 50,'8', '*'),
new KeyRect(135, 80, 50, 50,'9', '('),
new KeyRect(190, 80, 50, 50,'0', ')'),
new KeyRect(245, 80, 50, 50,'q', 'Q'),
new KeyRect(300, 80, 50, 50,'w', 'W'),
//Third Line
new KeyRect(25, 135, 50, 50,'e', 'E'),
new KeyRect(80, 135, 50, 50,'r', 'R'),
new KeyRect(135, 135, 50, 50,'t', 'T'),
new KeyRect(190, 135, 50, 50,'y', 'Y'),
new KeyRect(245, 135, 50, 50,'i', 'I'),
new KeyRect(300, 135, 50, 50,'o', 'O'),
//Fourth Line
new KeyRect(25, 190, 50, 50,'p', 'P'),
new KeyRect(80, 190, 50, 50,'a', 'A'),
new KeyRect(135, 190, 50, 50,'s', 'S'),
new KeyRect(190, 190, 50, 50,'d', 'D'),
new KeyRect(245, 190, 50, 50,'f', 'F'),
new KeyRect(300, 190, 50, 50,'g', 'G'),
//Fifth Line
new KeyRect(25, 245, 50, 50,'h', 'H'),
new KeyRect(80, 245, 50, 50,'j', 'J'),
new KeyRect(135, 245, 50, 50,'k', 'K'),
new KeyRect(190, 245, 50, 50,'l', 'L'),
new KeyRect(245, 245, 50, 50,'z', 'Z'),
new KeyRect(300, 245, 50, 50,'x', 'X'),
//Sixth Line
new KeyRect(25, 300, 50, 50,'c', 'C'),
new KeyRect(80, 300, 50, 50,'v', 'V'),
new KeyRect(135, 300, 50, 50,'b', 'B'),
new KeyRect(190, 300, 50, 50,'n', 'N'),
new KeyRect(245, 300, 50, 50,'m', 'M'),
new KeyRect(300, 300, 50, 50,'.', '>')
};
}

void draw ()
{
for ( int k = 0; k < keyrects.length; k++ ) keyrects[k].draw();
}

public class KeyRect
{
int x,y,w,h;
char k1,k2;
color fillColor;

KeyRect ( int _x, int _y, int _w, int _h, char _k1, char _k2 )
{
x = _x; y = _y; w = _w; h = _h; k1 = _k1; k2 = _k2;
fillColor = 235;
}

void draw ()
{
if ( keyPressed & (key == k1 || key == k2) ) fillColor = 0;
else fillColor = 235;

fill(fillColor);
rect(x,y,w,h);
}
}


F
Re: Keypress thingy...
Reply #8 - Apr 3rd, 2007, 9:37pm
 
WOW thanks alot... that cleaned up the code considerably... Problem is I don't understand a bit of it at the end. Would you be able to give me a laymans run through? Sorry to be a burden.
Re: Keypress thingy...
Reply #9 - Apr 4th, 2007, 7:47am
 
ok. the basic idea is to have a class represent one rect that listens to keypressed. then have an array to hold all the instances of that class that represents the grid.

Code:

// this is an array of KeyRect, something like a numbered list.
// KeyRect class is defined at the end, maybe jump there first ...
KeyRect[] keyrects;

void setup() {
// that's what you had before
size(375, 375);
background(255);
smooth();
noStroke();

// now fill the array with KeyRects,
// creating a new one for each position and
// passing it the initial set of values it needs to hold

keyrects = new KeyRect[] {
//First Line 6 x 6
new KeyRect(25, 25, 50, 50,'1','!'),
new KeyRect(80, 25, 50, 50,'2','@'),
new KeyRect(135, 25, 50, 50,'3', '£'),
new KeyRect(190, 25, 50, 50,'4', '$'),
new KeyRect(245, 25, 50, 50,'5', '%'),
new KeyRect(300, 25, 50, 50,'6', '^'),
//Second Line
new KeyRect(25, 80, 50, 50,'7', '&'),
new KeyRect(80, 80, 50, 50,'8', '*'),
new KeyRect(135, 80, 50, 50,'9', '('),
new KeyRect(190, 80, 50, 50,'0', ')'),
new KeyRect(245, 80, 50, 50,'q', 'Q'),
new KeyRect(300, 80, 50, 50,'w', 'W'),
//Third Line
new KeyRect(25, 135, 50, 50,'e', 'E'),
new KeyRect(80, 135, 50, 50,'r', 'R'),
new KeyRect(135, 135, 50, 50,'t', 'T'),
new KeyRect(190, 135, 50, 50,'y', 'Y'),
new KeyRect(245, 135, 50, 50,'i', 'I'),
new KeyRect(300, 135, 50, 50,'o', 'O'),
//Fourth Line
new KeyRect(25, 190, 50, 50,'p', 'P'),
new KeyRect(80, 190, 50, 50,'a', 'A'),
new KeyRect(135, 190, 50, 50,'s', 'S'),
new KeyRect(190, 190, 50, 50,'d', 'D'),
new KeyRect(245, 190, 50, 50,'f', 'F'),
new KeyRect(300, 190, 50, 50,'g', 'G'),
//Fifth Line
new KeyRect(25, 245, 50, 50,'h', 'H'),
new KeyRect(80, 245, 50, 50,'j', 'J'),
new KeyRect(135, 245, 50, 50,'k', 'K'),
new KeyRect(190, 245, 50, 50,'l', 'L'),
new KeyRect(245, 245, 50, 50,'z', 'Z'),
new KeyRect(300, 245, 50, 50,'x', 'X'),
//Sixth Line
new KeyRect(25, 300, 50, 50,'c', 'C'),
new KeyRect(80, 300, 50, 50,'v', 'V'),
new KeyRect(135, 300, 50, 50,'b', 'B'),
new KeyRect(190, 300, 50, 50,'n', 'N'),
new KeyRect(245, 300, 50, 50,'m', 'M'),
new KeyRect(300, 300, 50, 50,'.', '>')
};
}

void draw ()
{
// this line is a for-loop. it basically says:
// from 0 ( the lowest index in our array ) to almost
// how big the array is ( keyrects.length ) have
// a variable named "k" hold that value and do ...
for ( int k = 0; k < keyrects.length; k++ ) {
// k is now a number between or equal to 0 and (keyrects.length-1).
// so we can use it as index to the KeyRects stored in our array and
// call their draw method ( which will handle drawing and keyPressed
keyrects[k].draw();
}
}

// classes are like containers that hold different variables and
// functions ... super handy when simplifying things!
// the KeyRect class is there to handle the behavior of one grey rect
// that listens to a pair of keys

class KeyRect
{
// define the internal variables we need
int x,y,w,h;
char k1,k2;
color fillColor;

// this is a contructor, constructors are a way to pass an
// inital set of values to a class upon it's creation
KeyRect ( int _x, int _y, int _w, int _h, char _k1, char _k2 )
{
x = _x; y = _y; w = _w; h = _h; k1 = _k1; k2 = _k2;
fillColor = 235;
}

// KeyRect has it's own draw function,
// pretty much what you had before ..
void draw ()
{
if ( keyPressed & (key == k1 || key == k2) ) fillColor = 0;
else fillColor = 235;

fill(fillColor);
rect(x,y,w,h);
}
}


lemme know if this is clear enough .. or just ask if you have questions.

F
Re: Keypress thingy...
Reply #10 - Apr 4th, 2007, 7:49pm
 
I think I get it. So you've basically created the code that takes out the repetition of each line. So the code is simplified in the 'class' which im guessing is announced at the top of the code.

I was wondering if it would be then difficult to combine the code with something like:

http://processing.org/learning/examples/simpleparticlesystem.html

As this seems like a more interesting graphic. Would it be difficult to then put them together. Or is it just a case of knowing where things would go? I tried it the other night but to no avail. I shall try again and show you what I come up with. (if i can!)

Thanks again!
Re: Keypress thingy...
Reply #11 - Apr 28th, 2007, 12:44pm
 
Right I have been trying to combine the two codes... and I guess it's not as simple as I thought.

It has been tweaked a bit to how I like so far (like the code above). At the moment when you move the the cursor and press a key a new particle class appears. Is there a way to integrate this with the keypress code? So that when you press eg. 'a' the particle class appears in the grid location?

Any help would be great or even a point in the right direction!?

(((Is there anywhere i can post my hacked code as there isnt enough room to type it all!?)))
Re: Keypress thingy...
Reply #12 - May 13th, 2007, 4:37pm
 
Anyone help? I'll invite you to see the final project at my degree show if that can convince anyone? =)
Page Index Toggle Pages: 1