KeyPressed issue
              in 
             Programming Questions 
              •  
              1 year ago    
            
 
           
             Hey everyone! 
            
             
            
            
             
            
            
             
            
            
             
            
             
            
            
             
            
 
            
           
             I'm having an issue with keyPressed. 
            
            
             Basically i want it to be if the spacebar is pressed something happens, if the left arrow key is pressed something happens, if the right arrow key something else happens and if any other key is pressed something else happens. I'm quite a noob so excuse the question. I'm confused because i don't know how to mix if key==CODED (for left and right arrow keys) with all the other keys at once.
            
            
             This is what i have so far: (for space and else)
            
            - void keyPressed() {
 - if (key == ' ') {
 - background(255);
 - }
 - else {
 - background(0);
 - }
 - }
 
- if (keyPressed) {
 - if (key == CODED) {
 - if (keyCode == LEFT) {
 - if (position>300) {
 - position=position-5;
 - }
 - }
 - else if (key == CODED) {
 - if (keyCode == RIGHT) {
 - if (position<300) {
 - Position=Position+5;
 - }
 - }
 - }
 - }
 - }
 
             i need to somehow combine these two but can't do it :(
            
            
             please help! :)
            
 
            
              
              1  
            
 
            