Access buttons from linux

Started by ROM, January 14, 2014, 02:16:16 PM

Previous topic - Next topic

ROM

Hello!

How can I get access to the keys from Linux?

bearclaw

All I got so far is:

All Android buttons are connected to an ADC (LRADC) that uses a divider to determine which button is pressed. ADC being labeled "AB23".


bear

hello on!
try: insmod sun4i-keyboard.ko
(from //lib/modules/3.4.xx+/kernel/drivers/input/keyboard/ )
there xx -you kernel version
evtest (apt-get install evtest) help you test/show any input events (select sun4i-keyboard).

lauhub

Hello,

Do you think it is possible to call a program when an event is fired ?

As an example, I would like to call a shutdown command when the power button is hold for more than 4 seconds.

But I would also like to call program as soon as a button is pressed.

Would you have leads on how to do this ?

MBR

You have to read the correct /dev/input/eventX device (look at dmesg output to see what device it is). When the button is pressed or released, you can read the event data. The structure is defined in linux/input.h, but for one button, you will only look for the keypress and release. The program may, for example, wait in read(), branch by event using switch/case, store the timestamp when button is pressed and compare stored value wtih the timestamp when button is released - if the delta is more than four seconds, it will do the shutdown (using the system() call).

fab

Add "sun4i-keyboard" to /etc/modules

and find hereafter an example to catch events from a python script:


infile_path = "/dev/input/event3"
#long int, long int, unsigned short, unsigned short, unsigned int
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)
#open file in binary mode
in_file = open(infile_path, "rb")
try:
    event = in_file.read(EVENT_SIZE)
    while event:
        (tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event)
        if type != 0 or code != 0 or value != 0:
            if code==217 and value==1:
             # TODO
            if code==114 and value==1:
             # TODO
            if code==139 and value==1:
                print "System halt"
                os.system("halt")
        event = in_file.read(EVENT_SIZE)

finally:
    in_file.close()


Fab