root/version_0/KEYBOARD

Revision 548, 1.2 kB (checked in by blackhedd, 1 year ago)

some documentation cleanup.

  • Property svn:keywords set to Id
Line 
1 EventMachine (EM) can respond to keyboard events. This gives your event-driven programs the ability to respond to input from local users.
2
3 Programming EM to handle keyboard input in Ruby is simplicity itself. Just use EventMachine#open_keyboard, and supply the name of a Ruby module or class that will receive the input:
4
5  require 'rubygems'
6  require 'eventmachine'
7  
8  module MyKeyboardHandler
9         def receive_data keystrokes
10                 puts "I received the following data from the keyboard: #{keystrokes}"
11         end
12  end
13  
14  EM.run {
15         EM.open_keyboard(MyKeyboardHandler)
16  }
17
18
19 If you want EM to send line-buffered keyboard input to your program, just include the LineText2 protocol module in your handler class or module:
20
21
22
23  require 'rubygems'
24  require 'eventmachine'
25  
26  module MyKeyboardHandler
27         include EM::Protocols::LineText2
28         def receive_line data
29                 puts "I received the following line from the keyboard: #{data}"
30         end
31  end
32  
33  EM.run {
34         EM.open_keyboard(MyKeyboardHandler)
35  }
36
37 As we said, simplicity itself. You can call EventMachine#open_keyboard at any time while the EM reactor loop is running. In other words, the method invocation may appear anywhere in an EventMachine#run block, or in any code invoked in the #run block.
38
Note: See TracBrowser for help on using the browser.