Keymaps and Keyboard Input
来源:互联网 发布:宿迁网络问政12345下载 编辑:程序博客网 时间:2024/06/05 16:17
Keymaps and Keyboard Input
This document describes how keyboard input gets translated into Android actions and how you can customize key layout and key character maps to match the needs of your own device.
Android uses the standard Linux input event device (/dev/event0
) and driver as described in thelinux/input.h
kernel header file. For more information regarding standard Linux input drivers, please seeLinux Input drivers at http://kernel.org.
Functionality
Android's input event device is structured around an interrupt or polling routine that captures the device-specific scancode and converts it to a standard form acceptable to Linux (as defined in input.h
) before passing it to the kernel with input_event()
.
The keymap driver's other primary function is to establish a probe function that sets up the interrupt or polling function, handles hardware initialization, and attaches the driver to the input subsystem with input_register_device()
.
The table below describes the steps required to translate from keyboard input to application action:
KEYCODE_DPAD_CENTER
is the center button on the five-way navigation control. Even though ALT + G generates a "?" character, KEYCODE_G
is the keycode.3.Window manager sends both the scancode and the keycode to the application.Both the scancode and keycode are handled by the view with focus. How the application interprets both depend on the application.Key Layout Map
Selection of a Key Layout Map
Key layout maps are installed in /system/usr/keylayout
and /data/usr/keylayout
.
For each keyboard device xxx, set the android.keylayout.xxx
system property (see Building New Device for help setting system properties). If you don't specify a keylayout file, Android will default to /system/usr/keylayout/qwerty.kl
.
File Format
Key layout maps are stored on the device as UTF-8 text files and have the following characteristics:
- Comments: The pound symbol (#) denotes a comment and everything after the pound symbol on a line is ignored.
- Whitespace: All empty lines are ignored.
- Key definitions: Key definitions follow the syntax
key SCANCODE KEYCODE [FLAGS...]
, whereSCANCODE
is a number,KEYCODE
is defined in your specific keylayout file (android.keylayout.xxx
), and potentialFLAGS
are defined as follows:- SHIFT: While pressed, the shift key modifier is set
- ALT: While pressed, the alt key modifier is set
- CAPS: While pressed, the caps lock key modifier is set
- WAKE: When this key is pressed while the device is asleep, the device will wake up and the key event gets sent to the app.
- WAKE_DROPPED: When this key is pressed while the device is asleep, the device will wake up and the key event does not get sent to the app.
Example of a Key Layout Map File
The following code comes from android/src/device/product/generic/tuttle2.kl
and is an example of a complete key layout file:
# Copyright 2007 Google Inc.key 2 1key 3 2key 4 3key 5 4key 6 5key 7 6key 8 7key 9 8key 10 9key 11 0key 158 BACK WAKE_DROPPEDkey 230 SOFT_RIGHT WAKEkey 60 SOFT_RIGHT WAKEkey 107 ENDCALL WAKE_DROPPEDkey 62 ENDCALL WAKE_DROPPEDkey 229 MENU WAKE_DROPPEDkey 59 MENU WAKE_DROPPEDkey 228 POUNDkey 227 STARkey 231 CALL WAKE_DROPPEDkey 61 CALL WAKE_DROPPEDkey 232 DPAD_CENTER WAKE_DROPPEDkey 108 DPAD_DOWN WAKE_DROPPEDkey 103 DPAD_UP WAKE_DROPPEDkey 102 HOME WAKEkey 105 DPAD_LEFT WAKE_DROPPEDkey 106 DPAD_RIGHT WAKE_DROPPEDkey 115 VOLUME_UPkey 114 VOLUME_DOWNkey 116 POWER WAKEkey 212 SLASHkey 16 Qkey 17 Wkey 18 Ekey 19 Rkey 20 Tkey 21 Ykey 22 Ukey 23 Ikey 24 Okey 25 Pkey 30 Akey 31 Skey 32 Dkey 33 Fkey 34 Gkey 35 Hkey 36 Jkey 37 Kkey 38 Lkey 14 DEL key 44 Zkey 45 Xkey 46 Ckey 47 Vkey 48 Bkey 49 Nkey 50 Mkey 51 COMMAkey 52 PERIODkey 28 NEWLINE key 56 ALT_LEFTkey 42 SHIFT_LEFTkey 215 ATkey 57 SPACEkey 53 SLASHkey 127 SYMkey 100 ALT_LEFTkey 399 GRAVE
Key Character Map
Selection of a Key Character Map
Key character maps are installed in /system/usr/keychars
and /data/usr/keychars
.
For each keyboard device xxx, set the android.keychar.xxx
system property to the full path of the desired keychar file. If you don't specify a keychar file, Android will default to /system/usr/keychar/qwerty.kl
.
File Format
Key character maps are stored on the device as binary resources in order to reduce loading time. Key character maps have the following characteristics:
- Comments: The pound symbol (#) denotes a comment and everything after the pound symbol on a line is ignored.
- Whitespace: All empty lines are ignored.
- Column definitions: Column definitions follow the syntax
columns MODIFIERS [...]
, whereMODIFIERS
are defined as follows:Character in MODIFIERS Corresponding bit in the modifiers Ono modifiersSMODIFIER_SHIFTCMODIFIER_CONTROLLMODIFIER_CAPS_LOCKAMODIFIER_ALT - Key definitions: Key definitions have the syntax
key SCANCODE CHARACTER [...]
whereSCANCODE
is a number andCHARACTER
values are either UTF-8 characters in quotation marks (for example, "a") or a numeric value thatstrtol
can parse.
Example of a Key Character Map File
The following code comes from android/src/device/product/generic/tuttle2.kcm
and represents a complete key character file:
The type line indicates what kind of keyboard your device implements. Possible types include:
- NUMERIC: A numeric (12-key) keyboard.
- Q14: A keyboard that includes all letters but multiple letters per key.
- QWERTY: A keyboard with all letters and possibly numbers. This option applies to all full keyboard configurations, such as AZERTY.
# Copyright 2007 Google Inc.[type=QWERTY]# keycode base caps fn caps_fn number display_labelA 'a' 'A' '%' 0x00 '%' 'A'B 'b' 'B' '=' 0x00 '=' 'B'C 'c' 'C' '8' 0x00E7 '8' 'C'D 'd' 'D' '5' 0x00 '5' 'D'E 'e' 'E' '2' 0x0301 '2' 'E'F 'f' 'F' '6' 0x00A5 '6' 'F'G 'g' 'G' '-' '_' '-' 'G'H 'h' 'H' '[' '{' '[' 'H'I 'i' 'I' '$' 0x0302 '$' 'I'J 'j' 'J' ']' '}' ']' 'J'K 'k' 'K' '"' '~' '"' 'K'L 'l' 'L' ''' '`' ''' 'L'M 'm' 'M' '>' 0x00 '>' 'M'N 'n' 'N' '<' 0x0303 '<' 'N'O 'o' 'O' '(' 0x00 '(' 'O'P 'p' 'P' ')' 0x00 ')' 'P'Q 'q' 'Q' '*' 0x0300 '*' 'Q'R 'r' 'R' '3' 0x20AC '3' 'R'S 's' 'S' '4' 0x00DF '4' 'S'T 't' 'T' '+' 0x00A3 '+' 'T'U 'u' 'U' '&' 0x0308 '&' 'U'V 'v' 'V' '9' '^' '9' 'V'W 'w' 'W' '1' 0x00 '1' 'W'X 'x' 'X' '7' 0xEF00 '7' 'X'Y 'y' 'Y' '!' 0x00A1 '!' 'Y'Z 'z' 'Z' '#' 0x00 '#' 'Z'COMMA ',' ';' ';' '|' ',' ','PERIOD '.' ':' ':' 0x2026 '.' '.'AT '@' '0' '0' 0x2022 '0' '@'SLASH '/' '?' '?' '\' '/' '/'SPACE 0x20 0x20 0x9 0x9 0x20 0x20NEWLINE 0xa 0xa 0xa 0xa 0xa 0xa# on pc keyboardsTAB 0x9 0x9 0x9 0x9 0x9 0x90 '0' ')' ')' ')' '0' '0'1 '1' '!' '!' '!' '1' '1'2 '2' '@' '@' '@' '2' '2'3 '3' '#' '#' '#' '3' '3'4 '4' '$' '$' '$' '4' '4'5 '5' '%' '%' '%' '5' '5'6 '6' '^' '^' '^' '6' '6'7 '7' '&' '&' '&' '7' '7'8 '8' '*' '*' '*' '8' '8'9 '9' '(' '(' '(' '9' '9'GRAVE '`' '~' '`' '~' '`' '`'MINUS '-' '_' '-' '_' '-' '-'EQUALS '=' '+' '=' '+' '=' '='LEFT_BRACKET '[' '{' '[' '{' '[' '['RIGHT_BRACKET ']' '}' ']' '}' ']' ']'BACKSLASH '\' '|' '\' '|' '\' '\'SEMICOLON ';' ':' ';' ':' ';' ';'APOSTROPHE ''' '"' ''' '"' ''' '''STAR '*' '*' '*' '*' '*' '*'POUND '#' '#' '#' '#' '#' '#'PLUS '+' '+' '+' '+' '+' '+'
Resource Binary File Format
The file snippet above gets converted to the following by the makekcharmap
tool as part of the build process. You can mmap
this file in and share the approximately 4k of memory that it uses between processes to minimize load time.
Implementing Your Own Driver (Driver Template)
The following file, pguide_events.c
, illustrates how to implement an Android keymap driver.
/* * pguide_events.c * * ANDROID PORTING GUIDE: INPUT EVENTS DRIVER TEMPLATE * * This template is designed to an example of the functionality * necessary for Android to recieve input events. The PGUIDE_EVENT * macros are meant as pointers indicating where to implement the * hardware specific code necessary for the new device. The existence * of the macros is not meant to trivialize the work required, just as * an indication of where the work needs to be done. * * Copyright 2007, Google Inc. * Based on goldfish-events.c * */#include #include #include #include #include #include #include #include #include #define PGUIDE_EVENTS_INTERRUPT do{} while(0)#define PGUIDE_EVENTS_PROBE do{} while(0)struct event_dev { struct input_dev *input; int irq;};static irqreturn_t pguide_events_interrupt(int irq, void *dev_id){ struct event_dev *edev = dev_id; unsigned type=0, code=0, value=0; /* Set up type, code, and value per input.h */ PGUIDE_EVENTS_INTERRUPT; input_event(edev->input, type, code, value); return IRQ_HANDLED;}static int pguide_events_probe(struct platform_device *pdev){ struct input_dev *input_dev; struct event_dev *edev; printk("*** pguide events probe ***\n"); edev = kzalloc(sizeof(struct event_dev), GFP_KERNEL); input_dev = input_allocate_device(); /* Setup edev->irq and do any hardware init */ PGUIDE_EVENTS_PROBE; if(request_irq(edev->irq, pguide_events_interrupt, 0, "pguide_events", edev) < 0) { goto fail; } /* indicate that we generate key events */ set_bit(EV_KEY, input_dev->evbit); set_bit(EV_REL, input_dev->evbit); set_bit(EV_ABS, input_dev->evbit); /* indicate that we generate *any* key event */ bitmap_fill(input_dev->keybit, KEY_MAX); bitmap_fill(input_dev->relbit, REL_MAX); bitmap_fill(input_dev->absbit, ABS_MAX); platform_set_drvdata(pdev, edev); input_dev->name = "pguide_events"; input_dev->private = edev; input_dev->cdev.dev = &pdev->dev; input_register_device(input_dev); return 0;fail: kfree(edev); input_free_device(input_dev); return -EINVAL;}static struct platform_driver pguide_events_driver = { .probe = pguide_events_probe, .driver = { .name = "pguide_events", },};static int __devinit pguide_events_init(void){ return platform_driver_register(&pguide_events_driver);}static void __exit pguide_events_exit(void){}module_init(pguide_events_init);module_exit(pguide_events_exit);MODULE_DESCRIPTION("Pguide Event Device");MODULE_LICENSE("GPL");
Sample Implementation
Assume the following for the setup of a new keypad device:
android.keylayout.partnerxx_keypad = /system/usr/keylayout/partnerxx_keypad.klandroid.keychar.partnerxx_keypad = /system/usr/keychars/partnerxx.kcm
The following example log file indicates that you have correctly registered the new keypad:
I/EventHub( 1548): New device: path=/dev/input/event0 name=partnerxx_keypad id=0x10000 (of 0x1) index=1 fd=30I/EventHub( 1548): new keyboard input device added, name = partnerxx_keypadD/WindowManager( 1548): Starting input thread.D/WindowManager( 1548): Startup complete!I/EventHub( 1548): New keyboard: name=partnerxx_keypad keymap=partnerxx_keypad.kl keymapPath=/system/usr/keychars/partnerxx_keypad.kcm.binI/ServiceManager( 1535): ServiceManager: addService(window, 0x13610)I/EventHub( 1548): Reporting device opened: id=0x10000, name=/dev/input/event0I/KeyInputQueue( 1548): Device added: id=0x10000, name=partnerxx_keypad, classes=1I/KeyInputQueue( 1548): Keymap: partnerxx_keypad.kl
The snippet above contains artificial line breaks to maintain a print-friendly document.
From: http://www.kandroid.org/online-pdk/guide/keymaps_keyboard_input.html
- Keymaps and Keyboard Input
- Keymaps and Keyboard Input
- Android Keymaps and Keyboard Input
- Android Keymaps and Keyboard Input
- ANDROID Porting系列八、Keymaps and Keyboard Input
- Keyboard Input
- Keyboard Input Overview
- Using Keyboard Input
- VC++ Keyboard Input
- Handling Keyboard Input
- Getting Input from the Keyboard
- keyboard-input-platform2:input输入子系统(未完)
- focus and keyboard hide
- UIPicker and keyboard
- Android keymaps
- Can not input to minicom via keyboard
- minicom can't input with keyboard
- android touch screen keyboard input移植记录
- 八大排序算法
- 一个简单实用的log类
- 欧拉回路
- HDU 1334 Perfect Cubes【穷举法】
- 几种c/c++中字符串转整形的方法
- Keymaps and Keyboard Input
- Android webview的基本使用方法
- android中fragment与activity之间通信
- C++智能指针
- sk_buff整理笔记(四、克隆与复制)
- 黑马程序员之----------面对对象概述
- (驱动)(中断)(定时器)中断结合定时器完成按键消抖的驱动程序
- Sqrt(x)
- C++&&Mysql配置及例子