Using keyCode when Processing imported as p

I’m curious about why keyCode doesn’t seem to recognise keypresses when you import processing using an alias. I am using trinket, so that may be an issue.

With this simple code


from processing import *

def keyPressed():
  print(keyCode)
 
run()

I can derive the keyCode value for any key I press. But if I change the import style:


import processing as p

def keyPressed():
  print(p.keyCode)
  
p.run()

the code runs but it prints the word unknown surrounded by less than and greater than signs
whatever key I press.

Grateful for enlightenment.

1 Like

Cheers @simonc8 !

The Trinket implementation of Processing + Python is based on Skulpt + ProcessingJS
And ProcessingJS is kind of a zombie, unfortunately (GitHub - processing-js/processing-js: A port of the Processing visualization language to JavaScript.) so I couldn’t find any documentation about keyCode.

Maybe you could try pyp5js: drawing with Python 3 | pyp5js which is a work in progress, but at least you can open an issue if it doesn’t work…

And there are other options to explore, like http://p5py.com (Brython based)
More options at Python + Processing table

1 Like

Hi thanks for the information.

Do you know if there is a way to use the processing library with Python Idle? If I import processing it says ‘No module named processing’ so I suppose it needs installing, but using pip install processing doesn’t work. Do I need a different install command?

1 Like

Processing is a Java library; hence it’s not available via pip. You could use py5 instead (pip install py5). Note that you’ll need to have Java installed on your computer.

1 Like

Full disclosure: @tabreturn and me, we are fans of py5.ixora.io :smiley:

PyScript alpha version was recently released and I’ve converted an online sketch for it: :snake: