Last active
December 27, 2024 11:09
-
-
Save leonkasovan/803bbc9408251ea75694feef01d583b9 to your computer and use it in GitHub Desktop.
Check device path is a valid keyboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> | |
#include <linux/input.h> | |
#include <string.h> | |
// Function to check if the input device is a keyboard | |
int is_keyboard(const char *device_path) { | |
int fd = open(device_path, O_RDONLY); | |
if (fd == -1) { | |
perror("Failed to open device"); | |
return 0; // Failed to open device, return false | |
} | |
// Get the device name | |
char name[256]; | |
if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) { | |
perror("Failed to get device name"); | |
close(fd); | |
return 0; // Error reading device name, return false | |
} | |
// Output the device name for debugging (optional) | |
printf("Device name: %s\n", name); | |
// Check if the device supports key events (EV_KEY) | |
unsigned long evbit[2]; // 2 bits to store capability info | |
if (ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) { | |
perror("Failed to get device capabilities"); | |
close(fd); | |
return 0; // Error getting capabilities, return false | |
} | |
// Check if EV_KEY is set in the capabilities (indicates a keyboard) | |
if (evbit[0] & (1 << EV_KEY)) { | |
close(fd); | |
return 1; // It's a keyboard | |
} | |
close(fd); | |
return 0; // Not a keyboard | |
} | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> | |
#include <linux/input.h> | |
void process_keyboard_events(const char *device_path) { | |
int fd = open(device_path, O_RDONLY); | |
if (fd == -1) { | |
perror("Failed to open device"); | |
return; | |
} | |
struct input_event ev; | |
while (1) { | |
// Read an input event from the device | |
ssize_t n = read(fd, &ev, sizeof(struct input_event)); | |
if (n == -1) { | |
perror("Error reading input event"); | |
break; | |
} | |
// Filter for key events (EV_KEY) | |
if (ev.type == EV_KEY) { | |
printf("Scan Code: %d\n", ev.code); // Print scan code | |
if (ev.value == 0) { | |
printf("Key Released: Scan Code %d\n", ev.code); | |
} else if (ev.value == 1) { | |
printf("Key Pressed: Scan Code %d\n", ev.code); | |
} else if (ev.value == 2) { | |
printf("Key Held: Scan Code %d\n", ev.code); | |
} | |
} | |
} | |
close(fd); | |
} | |
char scanCodeToKey(int scanCode) { | |
switch (scanCode) { | |
case 0x1E: return 'A'; | |
case 0x30: return 'B'; | |
case 0x2E: return 'C'; | |
case 0x20: return 'D'; | |
case 0x12: return 'E'; | |
case 0x21: return 'F'; | |
case 0x22: return 'G'; | |
case 0x23: return 'H'; | |
case 0x17: return 'I'; | |
case 0x24: return 'J'; | |
case 0x25: return 'K'; | |
case 0x26: return 'L'; | |
case 0x32: return 'M'; | |
case 0x31: return 'N'; | |
case 0x18: return 'O'; | |
case 0x19: return 'P'; | |
case 0x10: return 'Q'; | |
case 0x13: return 'R'; | |
case 0x1F: return 'S'; | |
case 0x14: return 'T'; | |
case 0x16: return 'U'; | |
case 0x2F: return 'V'; | |
case 0x11: return 'W'; | |
case 0x2D: return 'X'; | |
case 0x15: return 'Y'; | |
case 0x2C: return 'Z'; | |
case 0x39: return ' '; // Space | |
case 0x0E: return '\b'; // Backspace | |
case 0x1C: return '\n'; // Enter | |
default: return '\0'; // Undefined scan code | |
} | |
} | |
int main() { | |
const char *device_path = "/dev/input/event0"; // Change this to your device path | |
if (is_keyboard(device_path)) { | |
printf("The device is a keyboard.\n"); | |
} else { | |
printf("The device is not a keyboard.\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment