Skip to content

Instantly share code, notes, and snippets.

@terremoth
Last active November 4, 2024 05:54
Show Gist options
  • Save terremoth/ddf439f37230d41ec2c047fca58fe1c1 to your computer and use it in GitHub Desktop.
Save terremoth/ddf439f37230d41ec2c047fca58fe1c1 to your computer and use it in GitHub Desktop.
key.php
<?php
//echo "\033[23\033[;H";
require_once 'vendor/autoload.php';
$struct = new \danog\PHP\StructClass();
function is_little_endian() {
$testint = 0x00FF;
$p = pack('S', $testint);
return $testint === current(unpack('v', $p));
}
function to_little_endian(string $input) : string {
if (!is_little_endian()) {
return $input;
}
return implode(array_reverse(str_split($input,2)));
}
$buffer_size = 144;
$buffer_part = 48;
while(true) {
//3a54f36400000000aeca050000000000040004001e000000
$event = @fopen("/dev/input/event0", "rb");
if ($event) {
while (($buffer = fread($event, $buffer_size)) !== false) {
system("clear");
//echo substr($buffer, 0,8).PHP_EOL;
//echo $buffer . PHP_EOL;
$hex = bin2hex($buffer);
$start = 0;
//echo $hex.PHP_EOL;
//$buffer_pluck = str_split($hex, 48);
//unset($buffer_pluck[2]);
//unset($buffer_pluck[5]);
for ($i = 0; $i < 144; $i += 24) {
$buffer_pluck = mb_strcut($buffer, $i, 24);
//echo strlen($buffer).PHP_EOL
list($time, $mcs, $type, $code, $value) = $up = $struct->unpack("llhhi",$buffer_pluck);
//print_r($up);
printf("time %d.%.5s - type: %d - code: %d\t - value: %d\n", $time, $mcs, $type, $code, $value);
}
//while($start < $buffer_size*2) {
//$microtime = microtime(true);
//$buffer_hex = substr($hex, $start, $buffer_part);
//$buffer_pluck = substr($buffer, $start, $buffer_part);
//echo $buffer_pluck . PHP_EOL;
//echo $buffer_hex . PHP_EOL;
//$seconds = substr($buffer_hex,0,8);
//$seconds = hexdec(to_little_endian($seconds));
//$seconds = hexdec(($seconds));
//$microseconds = substr($buffer_hex, 16, 8);
//$microseconds = hexdec(to_little_endian($microseconds));
//$microseconds = hexdec(($microseconds));
// 2 bytes = 4hex chars
//$type = substr($buffer_hex, 16, 6);
//$type = hexdec(to_little_endian($type));
//echo $type.PHP_EOL;
//$code = 1; // 2 bytes = 4hex
//$value = 2; // 4 bytes = 8hex chars
//echo $seconds.' - '.$microseconds.' | time: ' . $microtime . ' ' . implode(' ', hrtime()) . PHP_EOL;
//$start += $buffer_part;
//}
//$timeFull = substr($hex,0, 16);
//$sec = hexdec(substr($timeFull,0,8));
//$microsec = substr($timeFull,8,16);
//echo $sec.' - '.$microsec.PHP_EOL;
//echo strlen($buffer);
//echo "|";
//var_dump($hex).PHP_EOL;
//var_dump($buffer).PHP_EOL;
//echo $hex.PHP_EOL;
}
if (!feof($event)) {
echo "EOF".PHP_EOL;
}
echo "Closing handle";
fclose($event);
}
}
@terremoth
Copy link
Author

To discover which eventX is your keyboard, exec the following command:

grep -E 'Handlers|EV=' /proc/bus/input/devices | grep -B1 'EV=120013' | grep -Eo 'event[0-9]+'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment