Public Boolean on Keydown (int key code, KeyEvent event)
Developers can judge specific events according to the parameters, keyCode and KeyEvent in the callback method. However, because the event callback mechanism runs in its sandbox, it is impossible to get the current application event callback in other applications.
Then let's look at the top-down transmission mechanism of events. As shown in the figure below, after the user clicks, the input driver of the soft keyboard or physical key will generate an interrupt, and the corresponding semaphore will be written into /dev/input/event*. The Android operating system will read events circularly and distribute them to WindowManagerServer. WindowManagerServer distributes the events to different ViewGroup and Views according to their sources, thus generating different events such as OnClick, OnKeyDown and OnTouch.
At this time, it is natural to think that hackers want to do keyboard monitoring, and they will definitely add custom events to the bottom of Linux. Here we use getevent in Linux to get the events reported by /dev/input/eventX devices. This command will also output the basic information of all event devices. Include touch screen, keys, earphone insertion, etc. Its basic usage is as follows:
Usage: getevent [-t] [-n] [-sswitchmask] [-s] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-ccount] [-r] [device]
? -t: display timestamp
? -n: Do not print line breaks.
? -s: Switch status printed for positioning.
? -S: print all switch states.
? -v: detailed mask (errs= 1, dev=2, name=4, info=8, vers= 16, pos. Event =32, Prop =64)
? -d: display the HID descriptor (if any)
? -p: Displays possible events (error, development, name, location. Event)
? -i: Display all device information and possible events.
? -l: Mark the event type and name in plain text.
? -q: quiet (clear detailed mask)
? -c: Print a given number of events, and then exit.
? -r: print rate event received.
After entering getevent, we can see that some columns in the device have input hardware driver information, and there will be many input command signals below. Usually these semaphores are screened, as shown in the following figure:
We can't directly understand the representation of these semaphores. Enter get event–l to add tags, and we can see some added tags, as shown in the following figure:
In fact, these tags have been defined in their input.h header files, where the definition of type is as follows:
/*
* Event type
*/
? # define EV_SYN? 0x00
? # define EV_KEY? 0x0 1
? # define EV_REL? 0x02
? # define EV_ABS? 0x03
? # define EV_MSC? 0x04
? # define EV_SW? 0x05
? # define EV_LED? 0x 1 1
? # define EV_SND? 0x 12
? # define EV_REP? 0x 14
? # define EV_FF? 0x 15
? # define EV_PWR? 0x 16
? # define EV_FF_STATUS? 0x 17
? # define EV_MAX? 0x 1f
? # define EV_CNT (EV_MAX+ 1)
Generally speaking, EV_KEY, EV_REL, EV_ABS and EV_SYN are commonly used, which correspond to keyboard keys, relative coordinates, absolute coordinates and synchronous events respectively. EV_SYN indicates that a complete set of events has been completed and needs to be processed. The code of EV_SYN defines the type of event distribution.