KeyEvents not detected in javascript mode.

edited April 2015 in JavaScript Mode

If KeyEvent.VK_ENTER or for that any garbage like KeyEvent.abcd is used anywhere in the program, it wont give error but none of the key presses are detected. The code does not give error but background doesn't change when enter or a is pressed.

Code:

import java.awt.event.KeyEvent;
void setup() {
  size(500, 500);
  background(0);
}
void draw() {
}
void keyPressed() {  
  if (key==KeyEvent.VK_ENTER)
    background(255);
  if (key=='a')
    background(#EA1F1F);
  if (key==Kefnvjkfjk)
    background(255);
} 

Answers

  • Where have you defined this mysterious Kefnvjkfjk variable??? [-(
    Apart from that & the mal-formed post, your code works! >-)

  • GoToLoop, Sorry bot that! That the point. even though it is not defined it doesn't give any error on saving. "your code works! " the code works in the sense it executes as expected or you are having the same problem as i mentioned?

  • edited April 2015

    Sorry I didn't realize it was about "JS Mode"! It works in "Java Mode" of course! 8-X
    However, you should know better that we can't import Java libraries into JavaScript! [-X

  • edited April 2015

    So i can't use minim and such extra's? How do i include the keyevent definitions then? X_X

  • edited April 2015 Answer ✓

    In order for the "Java Mode" sketch to successfully transpile into "JS Mode", we need to follow PJS' API:
    http://processingjs.org/reference/

    And I question why you'd need KeyEvent in Processing even in "Java Mode".
    Processing already got many PConstants w/ most used key codes like ENTER, LEFT, etc.!

  • Oh i didnt know they covered enter..! The PConstants will work fine! I am used to using the keyevent for backspace and stuff. Thanks for that! That will do for now but i can't import library in anyway? (probably out of scope for this discussion) but can i copy the code in those libraries from github and paste in in my JS? will it work(especially in case of minim(sound) lib)

  • edited April 2015
    • Minim is a wrapper for some other Java library!
    • Rather you should try to find a JS library similar to Minim.
    • Probably you're gonna have a separated ".js" suffixed tab where you write in JS instead of Java to communicate w/ the JS library!
Sign In or Register to comment.