Input
You can detect if user pressed keyboard or mouse.
Keyboard
Property
keyboard.key
: the actual key, if you pressede
, this will bee
keyboard.keyCode
: the key's code, if you pressede
, this will be69
keyboard.keyPressed
:True
if any key is pressed andFalse
if no keys are pressed
Constants
BACKSPACE
: backspace key codeTAB
: tab key codeENTER
: enter key codeRETURN
: return key codeESC
: esc key codeDELETE
: delete key codeSHIFT
: shift key codeCONTROL
: control key codeALT
: alt key codeCAPSLK
: caps lock key codePGUP
: page up key codePGDN
: page down key codeEND
: end key codeHOME
: home key codeLEFT
: left arrow key codeUP
: up arrow key codeRIGHT
: right arrow key codeDOWN
: down arrow key codeF1
,F2
...F12
: F1 ~ F12 key codeNUMLK
: number lock key codeINSERT
: insert key code
Events
keyPressed()
: If any key is pressed, this function will be calledkeyReleased()
: If any key is released, this function will be calledkeyTyped()
: If any key is typed, this function will be called
Example
Move a rectangle when left or right arrow key is pressed.
from processing import *
rectX = 0
def setup():
size(500, 400)
def draw():
background(0, 0, 0)
rect(rectX, 100, 50, 10)
def keyPressed():
global rectX
if keyboard.keyCode == LEFT:
rectX -= 5
elif keyboard.keyCode == RIGHT:
rectX += 5
run()
Mouse
Property
mouse.x
: current horizontal coordinate of the mousemouse.y
: current vertical coordinate of the mousemouse.px
: the previous horizontal coordinate of the mousemouse.py
: the previous vertical coordinate of the mousemouse.pressed
: if mouse is pressedmouse.button
: which mouse button is pressed, left mouse button is 37, right mouse button is 39
Functions
mouseClicked()
: will be called when click clickedmouseDragged()
: will be called when drag mousemousePressed()
: will be called when press mousemouseMoved()
: will be called when move mousemouseReleased()
: will be called when release mousemouseOut()
: will be called when mouse pointer leaves canvasmouseOver()
: will be called when the mouse pointer moves over canvas
Example
from processing import *
def setup():
size(500, 400)
def draw():
background(0)
def mousePressed():
print "pressed at:", mouse.x, mouse.y
# left button is 37, right button is 39
print "mouse button:", mouse.button
def mouseMoved():
info = "Mouse moved at (%s, %s)" % (mouse.x, mouse.y)
text(info, mouse.x, mouse.y)
def mouseReleased():
text("Mouse released", 10, 10)
run()