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 › (Beginner)keypressed
Page Index Toggle Pages: 1
(Beginner)keypressed (Read 550 times)
(Beginner)keypressed
Dec 8th, 2007, 2:38pm
 

I am the very beginner at processing, and what I want to do is just when you press a key, some text shows up in the window.

I tried this code below, but the text doesn't show up. It's just flickering...What's wrong with this code? And could you explain about the keyindex? I looked up the examples and so on, but it's hard for me to understand completely...

PFont font;
PImage a;
int numChars = 26;
int keyIndex;

void setup()
{
size(700, 210);
font = loadFont("AmericanTypewriter-14.vlw");
a = loadImage("1.jpg");
fill(0);
}

void draw()
{
 background(255);
 image(a,0,0);
 textFont(font, 14);
 text("woke me up today", 460, 100);
 smooth();
}

void keyPressed()
{
 if( key >= 'A' && key <= 'z') {
   int keyIndex;
   if(key <= 'Z') {
     keyIndex = key-'A';
     text("A", 330, 100);
   } else {
     keyIndex = key-'a';
     text("B", 330, 100);
   }
 }
 }


Re: (Beginner)keypressed
Reply #1 - Dec 8th, 2007, 2:59pm
 
That looks plausible, unfortunately the way key presses are handled is that they are only sent once in a while (at the key repeat rate), so that's why you're getting flicker - it only draws right after it's gotten a signal, and doesn't save the fact that they key is down.

Personally, I like to use a boolean array to store current key state, and use keyPressed() and keyReleased() to set it:

Code:

boolean [] keyDown = new boolean[255];

void keyPressed(){
if (0 <= key && key < 255){
keyDown[key] = true;
}
}

void keyReleased(){
if (0 <= key && key < 255){
keyDown[key] = false;
}
}


Then, within the draw loop you just check the keyDown[] array for whatever keys you're interested in (for instance, if (keyDown['a']) doStuff(); ).  Off the top of my head I don't remember if 'a' is set or 'A' when you hit the 'a' key, or if it depends on whether shift/caps lock is down, you can experiment and find out pretty easily.  It gets a little trickier to handle arrow keys and modifiers, because those don't show up in keyDown[], so you need to add special handling for those - a search around the forum will show you what to do there, I'm pretty sure.
Re: (Beginner)keypressed
Reply #2 - Dec 9th, 2007, 4:58am
 
Thanks a lot
I'm really impressed by the quick answer...and it was really helpful to understand some more languages in processing.

But what I hope to do is just this. Once you tab any key, than one text will show up, and then if you tab other key, the previous text will be gone and then the other new text will show up.  

Actually I tried to do that by this code below, at first.  But I realized that I just can't make this line for each keys on the keyboard!  (it's insane...)

void draw()
{
background(255);
image(a,0,0);
textFont(font, 14);
text("a", 460, 100);
if(key == 'w')
{text("b", 330, 100);
} else if(key == 'a'){
text("c", 330, 100);
}


I'm sorry for my somewhat awkward English and not sufficient pre-knowledge.. cuz I'm not a native speaker and I didn't get the book yet :_:

Anyways thank you again.  Although this is not exactly what I want to do right now, but I think I'm gonna use this code with another concept.
I hope I conveyed my appreciation well to you.
Page Index Toggle Pages: 1