官方文档部分解释:
1)The first three methods allow you to listen for keyboard events:以下三个方法对键盘事件提供监听
- keyDown(): Called when a key was pressed down. Reports the key code, as found inKeys.
- keyUp(): Called when a key was lifted. Reports the key code as above.
- keyTyped(): Called when a Unicode character was generated by the keyboard input. This can be used to implement text fields and similar user interface elements.
The next three methods report mouse/touch events:
- touchDown(): Called when a finger went down on the screen or a mouse button was pressed. Reports the coordinates as well as the pointer index and mouse button (alwaysButtons.LEFTfor touch screens).当手指在屏幕中按下去的时候调用
- touchUp(): Called when a finger was lifted from the screen or a mouse button was released. Reports the last known coordinates as well as the pointer index and mouse button (always
Buttons.Left
for touch screens).当手指在屏幕中弹起的时候调用 - touchDragged(): Called when a finger is being dragged over the screen or the mouse is dragged while a button is pressed. Reports the coordinates and pointer index. The button is not reported as multiple buttons could be pressed while the mouse is being dragged. You can use
Gdx.input.isButtonPressed()
to check for a specific button.当手指在屏幕中滑动的时候调用 - touchMoved(): Called when the mouse is moved over the screen without a mouse button being down. This event is only relevant on the desktop and will never occur on touch screen devices where you only get
touchDragged()
events. - scrolled(): Called when the scroll wheel of the mouse was turned. Reports either -1 or 1 depending on the direction of spin. This will never be called for touch screen devices.当你滚动滚轮的时候调用
3)The
InputMultiplexer
will hand any new events to the firstInputProcessor
that was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, theMyUiInputProcessor
can handle any events that fall inside one of its widgets and pass on any other events to theMyGameInputProcessor
. 使用Inputmultiplexer来处理多个inputprocessor的情况。InputMultiplexer会将事件传给第一个添加的inputprocessor的相应的方法来处理,如果该方法返回false,则会调用第二个Inputprocessor中相应的方法来进行处理。
二、应用举例
package com.example.groupactiontest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
public class MyGame implements ApplicationListener {
InputProcessor inputProcessor;
InputProcessor inputProcessor2;
@Override
public void create() {
inputProcessor = new InputProcessor() {
@Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
System.out.println("inputProcessor--------->touchUp");
return true;
}
@Override
public boolean touchDragged(int arg0, int arg1, int arg2) {
System.out.println("inputProcessor--------->touchDragged");
return true;
}
@Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
System.out.println("inputProcessor--------->touchDown");
return true;
}
@Override
public boolean scrolled(int arg0) {
System.out.println("inputProcessor--------->scrolled");
return true;
}
@Override
public boolean mouseMoved(int arg0, int arg1) {
System.out.println("inputProcessor--------->tmouseMoved");
return true;
}
@Override
public boolean keyUp(int arg0) {
System.out.println("inputProcessor--------->keyUp");
return true;
}
@Override
public boolean keyTyped(char arg0) {
System.out.println("inputProcessor--------->keyType");
return true;
}
@Override
public boolean keyDown(int arg0) {
System.out.println("inputProcessor--------->keyDown");
return true;
}
};
inputProcessor2 = new InputProcessor() {
@Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
System.out.println("inputprocessor2---------->touchUp");
return true;
}
@Override
public boolean touchDragged(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
System.out.println("inputprocessor2------------->touchDown");
return true;
}
@Override
public boolean scrolled(int arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyUp(int arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyTyped(char arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyDown(int arg0) {
// TODO Auto-generated method stub
return false;
}
};
InputMultiplexer inputMultiplexer = new InputMultiplexer();//用来处理多个InputProcessor的情况
inputMultiplexer.addProcessor(inputProcessor);
inputMultiplexer.addProcessor(inputProcessor2);
Gdx.input.setInputProcessor(inputMultiplexer);//这一句前往别漏了
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
三、效果图
1)当第一个InputProcessor中的touchUp和touchDown方法的返回值为false的时候,logcat中输出的结果如下:
‘
2)当第一个InputProcessor中的touchUp和touchDown方法的返回值为true的时候,logcat中输出的结果如下:
四、源码下载
http://download.csdn.net/detail/caihongshijie6/7040691