PHP 7.2.0 Release Candidate 2 Released


Please answer this simple SPAM challenge: min(eight, seven)?
(Example: nine)

The Note You're Voting On

james_s2010 at NOSPAM dot hotmail dot com
9 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
function inKey($vals) {
$inKey = "";
in_array($inKey,$vals)) {
$inKey = trim(`read -s -n1 valu;echo \$valu`);
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