我有同樣的問題,我已經解決了它使用'Character'事件,我分開處理InputEvent。
問題是這樣的:AKeyEvent_getKeyCode
對於某些軟鍵事件沒有返回KeyCode,特別是當您按住某個鍵時展開的「unicode/latin」字符。這可以防止方法@Shammi和@eozgonul工作,因爲在Java端重建的KeyEvent
沒有足夠的信息來獲取unicode字符。
另一個問題是在觸發dispatchKeyEvent
事件之前,在C++/Native端耗盡InputQueue
。這意味着KEYDOWN/KEYUP事件在Java代碼處理事件之前全部觸發。 (它們不交錯)。
我的解決辦法是通過重寫dispatchKeyEvent
和發送字符的Queue<Integer> queueLastInputCharacter = new ConcurrentLinkedQueue<Integer>();
// [JAVA]
@Override
public boolean dispatchKeyEvent (KeyEvent event)
{
int metaState = event.getMetaState();
int unichar = event.getUnicodeChar(metaState);
// We are queuing the Unicode version of the characters for
// sending to the app during processEvents() call.
// We Queue the KeyDown and ActionMultiple Event UnicodeCharacters
if(event.getAction()==KeyEvent.ACTION_DOWN){
if(unichar != 0){
queueLastInputCharacter.offer(Integer.valueOf(unichar));
}
else{
unichar = event.getUnicodeChar();
if(unichar != 0){
queueLastInputCharacter.offer(Integer.valueOf(unichar));
}
else if (event.getDisplayLabel() != 0){
String aText = new String();
aText = "";
aText += event.getDisplayLabel();
queueLastInputCharacter.offer(Integer.valueOf(Character.codePointAt(aText, 0)));
}
else
queueLastInputCharacter.offer(Integer.valueOf(0));
}
}
else if(event.getAction()==KeyEvent.ACTION_MULTIPLE){
unichar = (Character.codePointAt(event.getCharacters(), 0));
queueLastInputCharacter.offer(Integer.valueOf(unichar));
}
return super.dispatchKeyEvent(event);
}
併發隊列打算讓線程發揮好一起捕捉在Java端的Unicode字符。
我有一個返回的最後一個輸入字符一個Java端的方法:
// [JAVA]
public int getLastUnicodeChar(){
if(!queueLastInputCharacter.isEmpty())
return queueLastInputCharacter.poll().intValue();
return 0;
}
在我的尺蠖代碼的最後,我上漲了額外的檢查,看是否隊列中保留任何Unicode字符:
// [C++]
int ident;
int events;
struct android_poll_source* source;
// If not rendering, we will block 250ms waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((ident = ALooper_pollAll(((nv_app_status_focused(_lpApp)) ? 1 : 250),
NULL,
&events,
(void**)&source)) >= 0)
{
// Process this event.
if (source != NULL)
source->process(_lpApp, source);
// Check if we are exiting. If so, dump out
if (!nv_app_status_running(_lpApp))
return;
}
static int modtime = 10; // let's not run on every call
if(--modtime == 0) {
long uniChar = androidUnicodeCharFromKeyEvent();
while (uniChar != 0) {
KEvent kCharEvent; // Game engine event
kCharEvent.ptkKey = K_VK_ERROR;
kCharEvent.unicodeChar = uniChar;
kCharEvent.character = uniChar;
/* Send unicode char */
kCharEvent.type = K_EVENT_UNICHAR;
_lpPortableHandler(&kCharEvent);
if (kCharEvent.character < 127) {
/* Send ascii char for source compatibility as well */
kCharEvent.type = K_EVENT_CHAR;
_lpPortableHandler(&kCharEvent);
}
uniChar = androidUnicodeCharFromKeyEvent();
}
modtime = 10;
}
的androidUnicodeCharFromKeyEvent
功能是非常相似的@Shammi的GetStringFromAInputEvent
方法,只能使用CallIntMethod
返回jint
。
備註 這確實需要修改引擎以處理與按鍵事件分開的字符事件。 Android仍然有像AKEYCODE_BACK
或AKEYCODE_ENTER
這樣的關鍵代碼,它們不是字符事件,仍然需要處理(並且可以在主輸入活套上處理)。
編輯框,控制檯等...期望用戶輸入的內容可以修改爲接收一個單獨的字符事件來構建字符串。如果您正在多個平臺上工作,那麼除了正常的鍵輸入事件之外,還需要生成這些新的字符事件。
謝謝你問,但我不能跟着你。你指的是什麼變量,它與它有什麼關係? – Pierre