processing 手柄监听事件(xbox)
手柄监听
用到了键盘 空格 键以及S键,使用时请切换 英文输入法。另外 手柄是北通的。
setup()中有打印可以使用的设备,请根据自己的设备名称进行更改。复制粘贴就行。
手柄监听代码
/**
Controlling Servos with Firmata and Game Control Plus
Be able to control servos using any compatible joystick
by Davi Colares
*/
//link to http://www.lagers.org.uk/gamecontrol/api.html
import org.gamecontrolplus.gui.*;
import org.gamecontrolplus.*;
ControlIO control;
ControlDevice stick;
public void setup()
{
size(500,300);
control = ControlIO.getInstance(this);
println(control.getDevices());//打印可以使用的设备
stick = control.getDevice("Controller (XBOX 360 For Windows)");
println(stick.toText(""));//打印所有控制按键/摇杆
if (stick == null){
println("Nenhum dispositivo configurado");
System.exit(-1);
}
println(stick.getButton(0).pressed());
}
//stick slider 左摇杆 0:(up-1>0>1); 1:left 专家摇杆 2: up; 3:left 4: (RT:0>>-1 LT:0>>1) //建议(-0.001,0.001)范围内为按键松开
//stick button 0:A 1:B; 2:C 3:Y 4:LB 5:RB 6:BACK 7:start 8:左摇杆按下 9:右摇杆按下 10:方向键value:up:2 down:6 right:4 left:8 未按下:0
int i=0,s=0;
public void draw(){
//println(stick.getSlider(0).getValue());
background(0);
//fill(0);
noStroke();
textFont(createFont("Arial",16,true));
textSize(15);
text(stick.getSlider(s).getValue()+",",80,40);//显示第i个按键是否按下
text(stick.getButton(i).pressed()+",",40,40);//显示第i个按键是否按下
text(stick.getButton(10).getValue()+",",10,40);//显示方向按钮
text(i+",",10,20);
text(s+",",40,20);
}
void keyPressed(){
if(keyCode==32){//空格 更改第i个按键
i++;
if(i==11){i=0;}//限制i范围(0-10)
}
if(keyCode==83){//当键盘S键按下 【英文输入法下
s++;
if(s==5){s=0;}//限制s范围(0-10)
}
}
需要导入的包
首先打开【工具】中的【添加工具】
点击弹出窗口的 Libraries选项 并在搜索框中输入 joysticks 安装以下插件