Obviously you would need a boolean variable to store the state.
In Processing select File | Examples in the menu and in the window that opens locate the GCP library and open the joystick example.