Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
$inkey in php?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Letharion
Veteran
Veteran


Joined: 13 Jun 2005
Posts: 1344
Location: Sweden

PostPosted: Sat Sep 02, 2006 8:41 am    Post subject: $inkey in php? Reply with quote

In Qbasic (which is evil, I know ^^) there's a var called $inkey, which holds the last pressed key, and the program can catch this at any point it wishes, and I'm wondering if there's something similar in PHP? I can readline from stdin, yes, but that will hold the script until the user inputs something. I'd like the program do run it's loop and check $inkey every other second or so and react to it.

(1) Is this doable in a fairly easy way/possiable at all?
(2) Is it a completly bad idea and I should design the program in a different way? :-)
(3) If I can't/shouldn't do I like this, any pointers to where I should start looking?
Back to top
View user's profile Send private message
wally.hall
n00b
n00b


Joined: 26 Sep 2005
Posts: 55
Location: England

PostPosted: Sat Sep 02, 2006 10:12 am    Post subject: Think event driven code Reply with quote

That kind of coding is generally viewed as bad in modern programming, sat in an idle loop "polling" for something to happen is an ugly and resource wasting way of doing what (should) be doable by some other means.

But this of course rasises the question, how do you do it in a non-ugly polling way in php, the answer to which I'm not sure of.

Talking completely non-php, what you want to do is something like this:

Start a thread for executing any code while waiting for user input (optional, only if you want the program to continue processing something in the background)
Sit in a while loop waiting for user input to occur (but not by polling, rather by being event driven).

Your readln() is effectively event driven. Your script will halt there until something is inputted, the event drives the code, not the code polling for the event.

So if you want something to be happening UNTIL the user inputs something, you want to thread your application, or fork off another completely separate process.

There are ways to do this is most high level languages, and I'm fairly certain PHP can do it in the latest release (PHP5), but before that I have no idea.

If you can't find how to do what you want with PHP, you might want to consider using a different language, Perl is darn powerful and highly portable for example, PHP is very useable for shell like scripts, but imo it's real strength is in CGI.

Just some thoughts, please anyone correct me if you disagree, like I said, this is my opinion, obviously if something does the job and does it well (fulfilling all your specifications), then it's perfect for the job. Just depends on what your specifications are.

Cheers.
_________________
I like Gentoo why?
Because it works how I want it to work.
Back to top
View user's profile Send private message
Letharion
Veteran
Veteran


Joined: 13 Jun 2005
Posts: 1344
Location: Sweden

PostPosted: Sun Sep 03, 2006 10:56 am    Post subject: Reply with quote

Very interesting, thanks for the info. I'll look for alternative ways of solving the problem then, such as some event-driven check. :-)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum