android键盘映射之一

按键事件

对于按键事件,调用mDevices->layoutMap->map进行映射。映射实际是由 KeyLayoutMap::map完成的,KeyLayoutMap类里读取配置文件qwerty.kl,由配置 文件 qwerty.kl 决定键值的映射关系。你可以通过修 改./development/emulator/keymaps/qwerty.kl来改变键值的映射关系。

JNI 函数
在frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp文 件中,向 JAVA提供了函数android_server_KeyInputQueue_readEvent,用于读 取输入设备事件。
C代码:

static jboolean android_server_KeyInputQueue_readEvent(JNIEnv* env, jobject clazz,
                                            jobject event)
{
     gLock.lock();
     sp hub = gHub;
    if (hub == NULL) {
         hub = new EventHub;
         gHub = hub;
     }
     gLock.unlock();
     int32_t deviceId;
     int32_t type;
     int32_t scancode, keycode;
     uint32_t flags;
     int32_t value;
     nsecs_t when;
    bool res = hub->getEvent(&deviceId, &type, &scancode, &keycode,
            &flags, &value, &when);
     env->SetIntField(event, gInputOffsets.mDeviceId, (jint)deviceId);
     env->SetIntField(event, gInputOffsets.mType, (jint)type);
     env->SetIntField(event, gInputOffsets.mScancode, (jint)scancode);
     env->SetIntField(event, gInputOffsets.mKeycode, (jint)keycode);
     env->SetIntField(event, gInputOffsets.mFlags, (jint)flags);
     env->SetIntField(event, gInputOffsets.mValue, value);
     env->SetLongField(event, gInputOffsets.mWhen,
                        (jlong)(nanoseconds_to_milliseconds(when)));
    return res;
}

readEvent调用hub->getEvent读了取事件,然后转换成JAVA的结构。

事件中转线程
在frameworks/base/services/java/com/android/server/KeyInputQueue.java 里创建了一个线程,它循环的读取事件,然后把事件放入事件队列里。
Java代码:

Thread mThread = new Thread("InputDeviceReader") {
        public void run() {
            android.os.Process.setThreadPriority(
                    android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
            
            try {
                RawInputEvent ev = new RawInputEvent();
                while (true) {
                    InputDevice di;

                    // block, doesn't release the monitor
                    readEvent(ev);

                    boolean send = false;
                    boolean configChanged = false;
                    
                    if (false) {
                        Log.i(TAG, "Input event: dev=0x"
                                + Integer.toHexString(ev.deviceId)
                                + " type=0x" + Integer.toHexString(ev.type)
                                + " scancode=" + ev.scancode
                                + " keycode=" + ev.keycode
                                + " value=" + ev.value);
                    }
                    
                    if (ev.type == RawInputEvent.EV_DEVICE_ADDED) {
                        synchronized (mFirst) {
                            di = newInputDevice(ev.deviceId);
                            mDevices.put(ev.deviceId, di);
                            configChanged = true;
                        }
                    }

            ......
          }
        }
      }
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Android 数字键盘的实现步骤: 1. 创建一个新的 XML 布局文件,命名为 "keyboard.xml",并添加如下代码: ``` <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="*"> <TableRow> <Button android:id="@+id/button1" android:text="1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button2" android:text="2" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button3" android:text="3" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </TableRow> <TableRow> <Button android:id="@+id/button4" android:text="4" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button5" android:text="5" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button6" android:text="6" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </TableRow> <TableRow> <Button android:id="@+id/button7" android:text="7" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button8" android:text="8" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button9" android:text="9" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </TableRow> <TableRow> <Button android:id="@+id/buttonClear" android:text="Clear" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/button0" android:text="0" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonDone" android:text="Done" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </TableRow> </TableLayout> ``` 2. 在你的 Activity 中,创建一个键盘弹出的方法: ``` private void showKeyboard() { // 加载布局文件并创建视图对象 LayoutInflater inflater = LayoutInflater.from(this); View keyboardView = inflater.inflate(R.layout.keyboard, null); // 创建一个 PopupWindow 对象 PopupWindow keyboardPopup = new PopupWindow(keyboardView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); // 显示 PopupWindow keyboardPopup.showAtLocation(findViewById(R.id.editText1), Gravity.BOTTOM, 0, 0); // 设置按钮点击事件 Button button1 = (Button) keyboardView.findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理按钮点击事件 editText.setText(editText.getText() + "1"); } }); // 处理其他按钮的点击事件... } ``` 3. 在你的 Activity 中,创建一个点击事件监听器,当用户点击 EditText 控件时,弹出数字键盘: ``` editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showKeyboard(); } }); ``` 4. 为“Clear”、“Done”按钮添加点击事件监听器,实现清空和隐藏数字键盘的功能: ``` Button buttonClear = (Button) keyboardView.findViewById(R.id.buttonClear); buttonClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.setText(""); } }); Button buttonDone = (Button) keyboardView.findViewById(R.id.buttonDone); buttonDone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { keyboardPopup.dismiss(); } }); ``` 这样就完成了一个简单的 Android 数字键盘的实现。当用户点击 EditText 控件时,弹出自定义的数字键盘,用户可以输入数字并进行清空和隐藏操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值