# Getting keyboard input with ruby while forking mplayer

**URL:** <https://www.ruby-forum.com/t/getting-keyboard-input-with-ruby-while-forking-mplayer/92458>\
**Category:** Ruby\
**Created:** [April 19, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/getting-keyboard-input-with-ruby-while-forking-mplayer/92458 "2007-04-19T18:15:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 19, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/getting-keyboard-input-with-ruby-while-forking-mplayer/92458/1 "2007-04-19T18:15:20Z")

</div>

Hi

I’m trying to create a simple ruby script, which forks mplayer and  
afterwards it stays in a while loop which blocks on gets (this is to  
get keyboard input). I start this script from a terminal (linux). When  
i start it, it forks mplayer and the focus is on mplayer now. If i  
press some keys on my keyboard these are send to mplayer.

Is there a solution to get focus on the terminal/script again (without  
user interaction) after i started mplayer? Or another solution to get  
keyboard commands into my script?

For some more information i included my program. The client is the one  
i’m talking about. The server sends the data via netcat to the client.  
It is the beginning of something you might call a pvr (personal video  
recorder).

Thanks in advance,

By the way this is my first ruby program (used to program c++/c#)

== pvr\_client.rb ==  
#!/usr/bin/ruby -w

require ‘soap/rpc/driver’

class UI  
def initialize  
@driver = SOAP::RPC::Driver.new(‘[http://localhost:8888/](http://localhost:8888/)’,  
‘urn:mySoapServer’)  
@driver.add\_method(‘startstream’, ‘toip’);  
fork do  
exec(‘nc -l -p 5000 | mplayer -’)  
end  
@driver.startstream(‘hoi’)  
end

```
def getInput
    while( command = gets ) do
        puts command
    end
end

```

end

ui = UI.new  
ui.getInput

== pvr\_server.rb ==  
#!/usr/bin/ruby -w  
require ‘soap/rpc/standaloneServer’

class MyServer \< SOAP::RPC::StandaloneServer  
def initialize(\*args)  
super  
add\_method(self, ‘startstream’, ‘toip’);  
end

```
def startstream(toip)
    fork do
        puts 'start streaming'
        exec('cat piet.mpg | nc localhost 5000')
    end
end

```

end

server = MyServer.new(‘CoolServer’, ‘urn:mySoapServer’, ‘localhost’,  
8888)  
trap(‘INT’) { server.shutdown }  
server.start

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 21, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/getting-keyboard-input-with-ruby-while-forking-mplayer/92458/2 "2007-04-21T12:24:54Z")

</div>

On 19/04/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> keyboard commands into my script?

In X the newly started program usually gets keyboard focus unless you  
configure your windowmanager do do something special. Of course, the  
steps vary wildly between different windowmanagers, and may would  
probably not allow such thing at all.

However, if you started the script from another machine through ssh or  
on another X server (you should be able to put in another videocard,  
and start a second X server) you should get the input to your script.

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 21, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/getting-keyboard-input-with-ruby-while-forking-mplayer/92458/3 "2007-04-21T18:20:33Z")

</div>

On Apr 21, 12:23 pm, “Michal S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Is there a solution to get focus on the terminal/script again (without  
> > and start a second X server) you should get the input to your script.  
> > Is there a way to get all keyboard input with my ruby script.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 21, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/getting-keyboard-input-with-ruby-while-forking-mplayer/92458/4 "2007-04-21T20:20:43Z")

</div>

On 21/04/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > on another X server (you should be able to put in another videocard,  
> > and start a second X server) you should get the input to your script.
> 
> Is there a way to get all keyboard input with my ruby script.

I guess there is no portable way (across different X desktops) except  
keyboard grab. However, keyboard grab is quite problematic. If your  
script breaks and does not release the grab nothing in X (not even the  
windowmanager) can use keyboard until you kill it. You will need some  
Xlib bindings or X11 protocol library like RubyX11 for that (you need  
to talk to the X server). And you will probably have to extend  
whatever you use to support such exotic feature.  
Using input grab (especially long) is considered quite unfriendly. It  
looks like firefox uses input grab for DnD and likes to lock up with  
input grabbed :-S

However, mplayer has some feature that allows you to specify a window  
id into which the video is rendered (it is used for mplayer mozilla  
plugin). That should avoid these problems. Have it render in the  
terminal window or open a window of your own that does not receive  
focus (I beleive there is a flag for that, and since it is your window  
you can get the input anyway).

Thanks

Michal
