[PATCH 1/2] input: add input_is_key_pressed() to read single key

Jonas Rebmann
2026-05-07T11:12:26Z

input_key_get_status() while being powerful and able to read all key
states at once, is difficult to use correctly, as the single incorrect
usage in upstream code shows.

Reading multiple key states at once, for any meaningful number of keys
combined (typically up to three) provides no advantage over checking the
keys sequentially.

Provide a simple input_is_key_pressed() function that checks a single
keycode.

Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
drivers/input/input.c | 14 ++++++++++++++
include/input/input.h | 1 +
2 files changed, 15 insertions(+)

diff --git a/drivers/input/input.c b/drivers/input/input.c
index e5509bf90d..ea3f4ef255 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -84,6 +84,20 @@ void input_key_get_status(unsigned long *keys, int bits)
bitmap_or(keys, keys, idev->keys, bits);
}

+bool input_is_key_pressed(int key)
+{
+ struct input_device *idev;
+ bool pressed = false;
+
+ if (key > KEY_MAX)
+ return false;
+
+ list_for_each_entry(idev, &input_devices, list)
+ pressed = pressed || test_bit(key, idev->keys);
+
+ return pressed;
+}
+
struct input_console {
struct console_device console;
struct input_notifier notifier;
diff --git a/include/input/input.h b/include/input/input.h
index 9445d20e56..3e03eb9a65 100644
--- a/include/input/input.h
+++ b/include/input/input.h
@@ -24,6 +24,7 @@ int input_device_register(struct input_device *);
void input_device_unregister(struct input_device *);

void input_key_get_status(unsigned long *keys, int bits);
+bool input_is_key_pressed(int key);

struct input_notifier {
void (*notify)(struct input_notifier *in, struct input_event *event);

--
2.53.0.610.g30c4861dc6