Voting

: five minus zero?
(Example: nine)

The Note You're Voting On

james_s2010 at NOSPAM dot hotmail dot com
16 years ago
I was looking for a way to interactively get a single character response from user. Using STDIN with fread, fgets and such will only work after pressing enter. So I came up with this instead:

#!/usr/bin/php -q
<?php
function inKey($vals) {
$inKey = "";
While(!
in_array($inKey,$vals)) {
$inKey = trim(`read -s -n1 valu;echo \$valu`);
}
return
$inKey;
}
function
echoAT($Row,$Col,$prompt="") {
// Display prompt at specific screen coords
echo "\033[".$Row.";".$Col."H".$prompt;
}
// Display prompt at position 10,10
echoAT(10,10,"Opt : ");

// Define acceptable responses
$options = array("1","2","3","4","X");

// Get user response
$key = inKey($options);

// Display user response & exit
echoAT(12,10,"Pressed : $key\n");
?>

Hope this helps someone.

<< Back to user notes page

To Top