Android Keyboard output add key code
No need to increase or decrease the key code value during the development process, in the android2.3 System Event Processing Section,
Mainly to the upper level to provide a unified key code (KEYCODE), this key code is an integer, in the upper Java program in the main through this value to determine the implementation of the system.
This is mainly about the process of the Android event, which requires two conversion steps:
Android Keyboard output add key code
1. Key Scan code scancode is an integer type defined by the input driver framework of Linux, can refer to Input.h header file
Converts its value to a string representing a key in step one
The keyboard layout file (*.KL) will complete the first step of the conversion, placed under/system/usr/keylayout/
Examples are as follows:
Input.h the letter key definition:
#define KEY_Q 16
#define KEY_W 17
#define KEY_E 18
#define KEY_R 19
#define KEY_T 20
#define KEY_Y 21
#define KEY_U 22
#define KEY_I 23
#define KEY_O 24
#define KEY_P 25
The QWERT.KL is defined as follows:
Scancode + String Value
Key Q
Key + W
Key E
Key R
Key T
Key Y
Key U
Key I
Key O
Key P
2. By looking up the keycodes array, the literal string is converted to the integer value of value, which translates to the KeyCode value.
The fragment below, the value on the right is the KeyCode value used in the Android system: (file path: frameworks/base/include/ui/keycodelabels.h)
static const Keycodelabel keycodes[] = {
...
{"A", 29},
{"B", 30},
{"C", 31},
{"D", 32},
{"E", 33},
{"F", 34},
{"G", 35},
{"H", 36},
{"I", 37},
{"J", 38},
{"K", 39},
{"L", 40},
{"M", 41},
{"N", 42},
{"O", 43},
{"P", 44},
{"Q", 45},
{"R", 46},
{"S", 47},
{"T", 48},
{"U", 49},
{"V", 50},
{"W", 51},
{"X", 52},
{"Y", 53},
{"Z", 54},
...
};
The value on the right is defined in Android as follows: (file path: frameworks/base/include/android/keycodes.h)
/*
* Key codes.
*/
enum {
akeycode_a = 29,
Akeycode_b = 30,
Akeycode_c = 31,
Akeycode_d = 32,
Akeycode_e = 33,
Akeycode_f = 34,
Akeycode_g = 35,
Akeycode_h = 36,
Akeycode_i = 37,
Akeycode_j = 38,
Akeycode_k = 39,
akeycode_l = 40,
Akeycode_m = 41,
Akeycode_n = 42,
Akeycode_o = 43,
Akeycode_p = 44,
Akeycode_q = 45,
Akeycode_r = 46,
akeycode_s = 47,
akeycode_t = 48,
Akeycode_u = 49,
Akeycode_v = 50,
Akeycode_w = 51,
Akeycode_x = 52,
Akeycode_y = 53,
Akeycode_z = 54,
..
};
The key value here corresponds to the value in Keyevent.java: (File path: Frameworks/base/core/java/android/view/keyevent.java)
/** Key code constant: ' A ' key. */
public static final int keycode_a = 29;
/** Key code constant: ' B ' key. */
public static final int keycode_b = 30;
/** Key code constant: ' C ' key. */
public static final int keycode_c = 31;
/** Key code constant: ' D ' key. */
public static final int keycode_d = 32;
/** Key code constant: ' E ' key. */
public static final int keycode_e = 33;
/** Key code constant: ' F ' key. */
public static final int keycode_f = 34;
/** Key code constant: ' G ' key. */
public static final int keycode_g = 35;
/** Key code constant: ' H ' key. */
public static final int keycode_h = 36;
/** Key code constant: ' I ' key. */
public static final int keycode_i = 37;
/** Key code constant: ' J ' key. */
public static final int keycode_j = 38;
/** Key code constant: ' K ' key. */
public static final int keycode_k = 39;
/** Key code constant: ' L ' key. */
public static final int keycode_l = 40;
/** Key code constant: ' M ' key. */
public static final int keycode_m = 41;
/** Key code constant: ' N ' key. */
public static final int keycode_n = 42;
/** Key code constant: ' O ' key. */
public static final int keycode_o = 43;
/** Key code constant: ' P ' key. */
public static final int keycode_p = 44;
/** Key code constant: ' Q ' key. */
public static final int keycode_q = 45;
/** Key code constant: ' R ' key. */
public static final int keycode_r = 46;
/** Key code constant: ' S ' key. */
public static final int keycode_s = 47;
/** Key code constant: ' T ' key. */
public static final int keycode_t = 48;
/** Key code constant: ' U ' key. */
public static final int keycode_u = 49;
/** Key code constant: ' V ' key. */
public static final int keycode_v = 50;
/** Key code constant: ' W ' key. */
public static final int keycode_w = 51;
/** Key code constant: ' X ' key. */
public static final int keycode_x = 52;
/** Key code constant: ' Y ' key. */
public static final int keycode_y = 53;
/** Key code constant: ' Z ' key. */
public static final int keycode_z = 54;
...
If you change this, you will need to call make UPDATE-API to affect the API.
OK, figure out the above conversion relationship, here is how to increase the key to increase the Gamekey as an example:
1, the keyboard layout file to increase the key, is generally QWERY.KL (must be *.KL end of the file):
Key 304 Button_a
Key 305 Button_b
Key 306 Button_c
Key 307 button_x
Key 308 button_y
Key 309 Button_z
Key 315 Button_start
Key Button_mode
2. Add the code array of the Keycodelabel type in Frameworks/base/include/ui/keycodelabels.h
{"Button_a", 96},
{"Button_b", 97},
{"Button_c", 98},
{"Button_x", 99},
{"Button_y", 100},
{"Button_z", 101},
{"Button_start", 108},
{"Button_mode", 110},
The current 2.3 system has been added
3. Increase the enumeration value of KeyCode in Frameworks/base/include/android/keycodes.h
Akeycode_button_a = 96,
Akeycode_button_b = 97,
Akeycode_button_c = 98,
akeycode_button_x = 99,
akeycode_button_y = 100,
Akeycode_button_z = 101,
Akeycode_button_start = 108,
Akeycode_button_mode = 110,
The current 2.3 system has been added
4. The Java layer Keyevent.java is added for use in Java applications
The current 2.3 system has been added
5. Add the resource file representing the attribute in the frameworks\base\core\res\res\values\attrs.xml, adding the corresponding attr with name= "KeyCode"
OK, complete the above steps should be ready to use.
Find out if key has not been filtered out the key confirmation:
1, the GetEvent function in the EventHub.cpp file
if (Iev.type = = Ev_key) {
status_t err = device-> Layoutmap->map (Iev.code,
& Outevent->keycode, & Outevent->flags);
LOGV ("iev.code=%d keycode=%d flags=0x%08x err=%d\n",
Iev.code, Outevent->keycode, outevent->flags, err);
if (err! = 0) {
Outevent->keycode = Akeycode_unknown;
outevent->flags = 0;
}
}
Confirm that the conversion is OK here, if not, it is possible that the *.kl file is not added to this key value
2, the InputReader.cpp file in the Keyboardinputmapper processing function process
void Keyboardinputmapper::p rocess (const rawevent* rawevent) {
Switch (rawevent->type) {
Case Ev_key: {
int32_t Scancode = rawevent->scancode;
if (Iskeyboardorgamepadkey (Scancode)) {
Processkey (rawevent->when, Rawevent->value! = 0, rawEvent-> KeyCode, Scancode,
Rawevent->flags);
}
break;
}
}
}
BOOL Keyboardinputmapper::iskeyboardorgamepadkey (int32_t scancode) {
Return Scancode < Btn_mouse
|| Scancode >= KEY_OK
|| (Scancode >= btn_gamepad && scancode < Btn_digi);
}
You can print in Processkey to confirm that the key value has been reported to the Android system through Notifykey.
Attach, how to compile the build Qwerty.kcm.bin file:
Android in order to reduce the loading time, and did not use the original key table file, but to convert it into a binary file
The conversion tool source code in the Android source code BUILD/TOOLS/KCM directory, Android in the compilation process will
First compile the conversion tool and then convert the qwerty.kcm file to Qwerty.kcm.bin using the conversion tool
The converted binaries are copied to out/target/product//system/usr/keychars
directory, which is the/system/usr/keychars directory of the target platform.
Usage:
static int usage ()
{
fprintf (stderr,
"USAGE:KCM INPUT output\n"
"\ n"
"INPUT keycharmap file\n"
"OUTPUT compiled Keycharmap file\n"
);
return 1;
}
http://blog.csdn.net/nihb1/article/details/12995731
MTK Android Keyboard output add key code