2012-05-30 54 views
0

我正在開發一個android應用程序(鍵盤驅動程序),其中首先我將藍牙鍵盤與android設備連接,在Inputmethod服務中成功連接後,我正在接收按鍵並將其發送到當前打開編輯。問題是,我無法弄清楚如何在android中打開/關閉大寫鎖定和切換鍵功能。在android中以編程方式關閉shift鍵

private BroadcastReceiver key3Receiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     int key = Integer.parseInt(intent 
       .getStringExtra(BluezService.COLONY_KEYPRESS_KEY)); 
     int action = intent.getIntExtra(BluezService.EVENT_KEYPRESS_ACTION, 
       KeyEvent.ACTION_DOWN); 
     Log.d("++++ key recieved ", Integer.toString(key)); 

     InputConnection ic = getCurrentInputConnection(); 
     long eventTime = SystemClock.uptimeMillis(); 

     if(key==2) 
     { 
      if(checkEnable==true){ 
       ic.clearMetaKeyStates(KeyEvent.META_SHIFT_LEFT_ON); 
       Log.d("metakey", "turn off"); 
       checkEnable=false; 
       return; 
      }else{ 
       Log.d("metakey", "turn on"); 
       checkEnable=true; 
      } 
     } 
     // ********* Hacks 
     if(key==8) 
     { 
      key=127; 
     } 



     if (key < 130) { 

      ic.sendKeyEvent(new KeyEvent(eventTime, eventTime, action,COLONYKEYCODE[key], 
        0, KeyEvent.META_SHIFT_ON, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD)); 
     } 

    } 
}; 
+0

請不要重複您已發佈的問題。 http://stackoverflow.com/questions/10794848/turn-on-off-caps-lock-and-shift-key-programatically-on-android –

回答

2

我發現它可能有助於某人的解決方案。我做錯了什麼,我調用KeyEvent.ACTION_DOWN,並忘記調用KeyEvent.ACTION_UP來處理ic.sendKeyEvent(...)中的操作,這就是爲什麼它沒有釋放shift鍵。