How to Stop the keyPress

Hello.

I have this and Im using a button to get a number (0-1) in the port. I want that when the read is 1, in the keyboard use the key RIGHT. And when the read is 0 it stops, the problem is that it doesn't stops.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import processing.serial.*;
Serial port;  
int val = 0;   
Robot robot;



void setup() {
  size(200, 200);
  try {
    robot = new Robot();
  } 
  catch (AWTException e) {
    e.printStackTrace();
  }

  println(Serial.list());
  port = new Serial(this, Serial.list()[0], 9600);
}


void draw()
{
  if (0<port.available())
  {
    val = port.read();
    if ( val ==1);
    println(val);
    {
      //robot.keyPress(KeyEvent.VK_RIGHT);
      robot.keyPress(key=RIGHT);
    }
  } else {
    if (val == 0);
    {
      //robot.keyPress(KeyEvent.VK_RIGHT);
      robot.keyRelease(key=RIGHT);

    }
  }
}

Thank you.

Answers

  • edited November 2014

    I guess the problem lies at your else block being bound to the outer if block rather the inner if!
    Also variable val is used exclusively inside draw()'s if block. So why is it declared globally instead of locally?
    That would help you out to discover the bug since val wouldn't exist at the outer else block! [-(
    Anyways, here's my attempt. Not good about Serial & Robot stuff though: :|

    void draw() {
      if (port.available() > 0) {
        int val = port.read();
        if      (val == 1)  robot.keyPress(RIGHT);
        else if (val == 0)  robot.keyRelease(RIGHT);
      }
    }
    
  • WOOOOOOOW THANK YOU SO MUCH IT WORKS :D Finally after maaaaaany attempts.

    Greetings from Colombia: D

Sign In or Register to comment.