android系統下怎麼廣播鍵盤的輸入消息

有時我們希望程序在後臺運行時能接收到按鍵輸入消息,我們在底層修改添加廣播消息就可以實現這樣的功能,具體過程如下所示:

1.在lichee/linux-3.0/include/linux/input.h文件可以查看底層驅動的按鍵編碼
#define KEY_F1 59
#define KEY_F2 60
#define KEY_F3 61
#define KEY_F4 62
#define KEY_F5 63
#define KEY_F6 64
#define KEY_F7 65
#define KEY_F8 66
#define KEY_F9 67
#define KEY_F10 68
2.在android4.0/frameworks/base/core/java/android/view/KeyEvent.java文件可以查看上層驅動的按鍵編碼
/** Key code constant: F1 key. */
public static final int KEYCODE_F1              = 131;
/** Key code constant: F2 key. */
public static final int KEYCODE_F2              = 132;
/** Key code constant: F3 key. */
public static final int KEYCODE_F3              = 133;
/** Key code constant: F4 key. */
public static final int KEYCODE_F4              = 134;
/** Key code constant: F5 key. */
public static final int KEYCODE_F5              = 135;
/** Key code constant: F6 key. */
public static final int KEYCODE_F6              = 136;
/** Key code constant: F7 key. */
public static final int KEYCODE_F7              = 137;
/** Key code constant: F8 key. */
public static final int KEYCODE_F8              = 138;
/** Key code constant: F9 key. */
public static final int KEYCODE_F9              = 139;
/** Key code constant: F10 key. */
public static final int KEYCODE_F10             = 140;
3.在/android4.0/frameworks/base/data/keyboards/Generic.kl文件裏面有底層到上層的映射,將這個文件定製到自己的系統android4.0/out/target/product/crane-m1003h6/system/usr/keylayout目錄下就可以了在上層收到USB鍵盤F1-F10的消息了。
key 59    F1
key 60    F2
key 61    F3
key 62    F4
key 63    F5
key 64    F6
key 65    F7
key 66    F8
key 67    F9
key 68    F10
4.在/android4.0/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindow.java文件的onKeyDown,onKeyUp函數截獲F1-F10的消息,用自己定義的廣播消息廣播出去應用就可以在後臺或者前臺都可以接收到按鍵按下或者釋放的廣播消息了。
     case KeyEvent.KEYCODE_F1: {
                //Log.e("zkliu","---------------------------test F1---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F1_KEYDOWN"));
                return true;
            }

            case KeyEvent.KEYCODE_F2: {
                //Log.e("zkliu","---------------------------test F2---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F2_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F3: {
                //Log.e("zkliu","---------------------------test F3---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F3_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F4: {
                //Log.e("zkliu","---------------------------test F4---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F4_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F5: {
                //Log.e("zkliu","---------------------------test F5---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F5_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F6: {
                //Log.e("zkliu","---------------------------test F6---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F6_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F7: {
                //Log.e("zkliu","---------------------------test F7---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F7_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F8: {
                //Log.e("zkliu","---------------------------test F8---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F8_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F9: {
                //Log.e("zkliu","---------------------------test F9---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F9_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F10: {
                //Log.e("zkliu","---------------------------test F10---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F10_KEYDOWN"));
                return true;
            }


            case KeyEvent.KEYCODE_F1: {
                //Log.e("zkliu","---------------------------test F1---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F1_KEYUP"));
                return true;
            }


   case KeyEvent.KEYCODE_F2: {
                //Log.e("zkliu","---------------------------test F2---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F2_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F3: {
                //Log.e("zkliu","---------------------------test F3---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F3_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F4: {
                //Log.e("zkliu","---------------------------test F4---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F4_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F5: {
                //Log.e("zkliu","---------------------------test F5---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F5_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F6: {
                //Log.e("zkliu","---------------------------test F6---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F6_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F7: {
                //Log.e("zkliu","---------------------------test F7---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F7_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F8: {
                //Log.e("zkliu","---------------------------test F8---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F8_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F9: {
                //Log.e("zkliu","---------------------------test F9---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F9_KEYUP"));
                return true;
            }


            case KeyEvent.KEYCODE_F10: {
                //Log.e("zkliu","---------------------------test F10---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F10_KEYUP"));
                return true;
            }

應用程序註冊接收相應的廣播消息即可接收到鍵盤輸入消息。

        完整的底層系統源代碼以及測試程序可以發郵件給:[email protected]索取。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章