Overwrite button behaviour

I solved! Here is the code in C and Python. These codes don’t need to change the driver code, you can just use them.

#include <linux/input.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    // Open the input device file
    int fd = open("/dev/input/event1", O_RDONLY);
    if (fd == -1) {
        perror("Failed to open input device");
        exit(EXIT_FAILURE);
    }

    // Grab the input device to prevent other programs from receiving events
    if (ioctl(fd, EVIOCGRAB, 1) == -1) {
        perror("Failed to grab input device");
        close(fd);
        exit(EXIT_FAILURE);
    }

    // Execute your program logic here
    while (1) {
        // ...
    }

    // Release the input device
    if (ioctl(fd, EVIOCGRAB, 0) == -1) {
        perror("Failed to release input device");
        close(fd);
        exit(EXIT_FAILURE);
    }

    close(fd);
    return 0;
}
import fcntl
import os
import struct
import array
import time

# Define constants
EVIOCGRAB = 0x40044590
EV_KEY = 0x01
KEY_POWER = 116  # Replace with the actual key code

# Open the input device file
input_device = "/dev/input/event1"  # Replace with your actual input device
fd = os.open(input_device, os.O_RDONLY)

# Grab the input device to prevent other programs from receiving events
fcntl.ioctl(fd, EVIOCGRAB, 1)

# Execute your program logic here
try:
    print("try")
    while True:
        print("disable key")
        time.sleep(1)

except KeyboardInterrupt:
    pass

finally:
    # Release the input device
    fcntl.ioctl(fd, EVIOCGRAB, 0)
    os.close(fd)
1 Like