# Windows Console I/O in Ruby

**URL:** <https://rubytalk.org/t/windows-console-i-o-in-ruby/25564>\
**Category:** ruby-talk\
**Created:** [8 March 2006 18:18 UTC](https://rubytalk.org/t/windows-console-i-o-in-ruby/25564 "2006-03-08T18:18:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Hulse](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@Matt\_Hulse](https://rubytalk.org/u/Matt_Hulse)\
**Post date:** [8 March 2006 18:18 UTC](https://rubytalk.org/t/windows-console-i-o-in-ruby/25564/1 "2006-03-08T18:18:41Z")

</div>

Is there a Ruby equivalent to the WConio Python Module or similar conio  
type support for windows?  
([http://newcenturycomputers.net/projects/wconio.html](http://newcenturycomputers.net/projects/wconio.html))

If not, does anyone have some pointers or documention about porting  
this module from Python to Ruby that might help me take a stab at it?

I appreciate any help.

--Matt

---

<div class="post-metadata">

**Author:** ![Adam\_Shelly](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Adam\_Shelly](https://rubytalk.org/u/Adam_Shelly)\
**Post date:** [8 March 2006 20:19 UTC](https://rubytalk.org/t/windows-console-i-o-in-ruby/25564/2 "2006-03-08T20:19:14Z")

</div>

seems like Ncurses does most of the same things as wconio.  
[http://ncurses-ruby.berlios.de/](http://ncurses-ruby.berlios.de/)

I needed to compile PDCurses on windows to get ncurses to work:  
[http://pdcurses.sourceforge.net/](http://pdcurses.sourceforge.net/)

-Adam

> **···**
>
> On 3/8/06, Matt Hulse \<matt.hulse@gmail.com\> wrote:
> 
> > Is there a Ruby equivalent to the WConio Python Module or similar conio  
> > type support for windows?  
> > ([http://newcenturycomputers.net/projects/wconio.html\](http://newcenturycomputers.net/projects/wconio.html%5C))
> > 
> > If not, does anyone have some pointers or documention about porting  
> > this module from Python to Ruby that might help me take a stab at it?
> > 
> > I appreciate any help.
> > 
> > --Matt

---

<div class="post-metadata">

**Author:** ![Lou\_Vanek](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@Lou\_Vanek](https://rubytalk.org/u/Lou_Vanek)\
**Post date:** [8 March 2006 23:59 UTC](https://rubytalk.org/t/windows-console-i-o-in-ruby/25564/3 "2006-03-08T23:59:33Z")

</div>

this may be of use,  
&nbsp;&nbsp;&nbsp;[http://rubyforge.org/projects/win32console/](http://rubyforge.org/projects/win32console/)

and here's the top of one of its rdoc files:  
(there also are ANSI color options and escape-sequence options)

> **···**
>
> #  
> # = NAME  
> #  
> # Win32::Console - Win32 Console and Character Mode Functions  
> #  
> # = DESCRIPTION  
> #  
> # This module implements the Win32 console and character mode  
> # functions. They give you full control on the console input and output,  
> # including: support of off-screen console buffers (eg. multiple screen  
> # pages)  
> #  
> # - reading and writing of characters, attributes and whole portions of  
> # the screen  
> #  
> # - complete processing of keyboard and mouse events  
> #  
> # - some very funny additional features 🙂  
> #  
> # Those functions should also make possible a port of the Unix's curses  
> # library; if there is anyone interested (and/or willing to contribute)  
> # to this project, e-mail me. Thank you.  
> #  
> # = REFERENCE  
> #  
> # == Methods  
> #  
> # - Alloc  
> #  
> # Allocates a new console for the process. Returns C\<nil\> on errors, a  
> # nonzero value on success. A process cannot be associated with more  
> # than one console, so this method will fail if there is already an  
> # allocated console. Use Free to detach the process from the console,  
> # and then call Alloc to create a new console. See also: C\<Free\>  
> #  
> # Example:  
> #  
> # Win32::Console::Alloc()  
> #  
> # - Attr [attr]  
> #  
> # Gets or sets the current console attribute. This attribute is used by  
> # the Write method.  
> #  
> # Example:  
> #  
> # attr = console.Attr()  
> # console.Attr(FG\_YELLOW | BG\_BLUE)  
> #  
> # - Close  
> #  
> # Closes a shortcut object. Note that it is not "strictly" required to  
> # close the objects you created, since the Win32::Shortcut objects are  
> # automatically closed when the program ends (or when you elsehow  
> # destroy such an object).  
> #  
> # Example:  
> #  
> # link.Close()  
> #  
> # - Cls [attr]  
> #  
> # Clear the console, with the specified I\<attr\> if given, or using  
> # ATTR\_NORMAL otherwise.  
> #  
> # Example:  
> #  
> # console.Cls()  
> # console.Cls(FG\_WHITE | BG\_GREEN)  
> #  
> # - Cursor [x, y, size, visible]  
> #  
> # Gets or sets cursor position and appearance. Returns C\<nil\> on  
> # errors, or a 4-element list containing: I\<x\>, I\<y\>, I\<size\>,  
> # I\<visible\>. I\<x\> and I\<y\> are the current cursor position; ...  
> #  
> # Example:  
> #  
> # x, y, size, visible = console.Cursor()  
> #  
> # # Get position only  
> # x, y = console.Cursor()  
> #  
> # console.Cursor(40, 13, 50, 1)  
> #  
> # # Set position only  
> # console.Cursor(40, 13)  
> #  
> # # Set size and visibility without affecting position  
> # console.Cursor(-1, -1, 50, 1)  
> #  
> # - Display  
> #  
> # Displays the specified console on the screen. Returns C\<nil\> on errors,  
> # a nonzero value on success.  
> #  
> # Example:  
> #  
> # console.Display()  
> #  
> # - FillAttr [attribute, number, col, row]  
> #  
> # Fills the specified number of consecutive attributes, beginning at  
> # I\<col\>, I\<row\>, with the value specified in I\<attribute\>. Returns the  
> # number of attributes filled, or C\<nil\> on errors. See also:  
> # C\<FillChar\>.  
> #  
> # Example:  
> #  
> # console.FillAttr(FG\_BLACK | BG\_BLACK, 80\*25, 0, 0)  
> #  
> # - FillChar char, number, col, row  
> #  
> # Fills the specified number of consecutive characters, beginning at  
> # I\<col\>, I\<row\>, with the character specified in I\<char\>. Returns the  
> # number of characters filled, or C\<nil\> on errors. See also:  
> # C\<FillAttr\>.  
> #  
> # Example:  
> #  
> # console.FillChar("X", 80\*25, 0, 0)  
> #  
> # - Flush  
> #  
> # Flushes the console input buffer. All the events in the buffer are  
> # discarded. Returns C\<nil\> on errors, a nonzero value on success.  
> #  
> # Example:  
> #  
> # console.Flush()  
> #  
> # - Free  
> #  
> # Detaches the process from the console. Returns C\<nil\> on errors, a  
> # nonzero value on success. See also: C\<Alloc\>.  
> #  
> # Example:  
> #  
> # Win32::Console::Free()  
> #  
> # - GenerateCtrlEvent [type, processgroup]  
> #  
> # Sends a break signal of the specified I\<type\> to the specified  
> # I\<processgroup\>. I\<type\> can be one of the following constants:  
> #  
> # CTRL\_BREAK\_EVENT  
> # CTRL\_C\_EVENT  
> #  
> # they signal, respectively, the pressing of Control + Break and of  
> # Control + C; if not specified, it defaults to CTRL\_C\_EVENT.  
> # I\<processgroup\> is the pid of a process sharing the same console. If  
> # omitted, it defaults to 0 (the current process), which is also the  
> # only meaningful value that you can pass to this function. Returns  
> # C\<nil\> on errors, a nonzero value on success.  
> #  
> # Example:  
> #  
> # # break this script now  
> # Win32::Console::GenerateCtrlEvent()  
> #  
> # - GetEvents  
> #  
> # Returns the number of unread input events in the console's input  
> # buffer, or C\<nil\> on errors. See also: C\<Input\>, C\<InputChar\>,  
> # C\<PeekInput\>, C\<WriteInput\>.  
> #  
> # Example:  
> #  
> # events = console.GetEvents()  
> #  
> # - Info  
> #  
> # Returns an array of informations about the console (or C\<nil\> on  
> # errors), which contains:  
> #  
> # \* columns (X size) of the console buffer.  
> #  
> # \* rows (Y size) of the console buffer.  
> #  
> # \* current column (X position) of the cursor.  
> #  
> # \* current row (Y position) of the cursor.  
> #  
> # \* current attribute used for C\<Write\>.  
> #  
> # \* left column (X of the starting point) of the current console window.  
> #  
> # \* top row (Y of the starting point) of the current console window.  
> #  
> # \* right column (X of the final point) of the current console window.  
> #  
> # \* bottom row (Y of the final point) of the current console window.  
> #  
> # \* maximum number of columns for the console window, given the current  
> # buffer size, font and the screen size.  
> #  
> # \* maximum number of rows for the console window, given the current  
> # buffer size, font and the screen size.  
> #  
> # See also: \<b\>Attr\</b\>, \<b\>Cursor\</b\>, \<b\>Size\</b\>, \<b\>Window\</b\>, \<b\>MaxWindow\</b\>.  
> #  
> # Example:  
> #  
> # info = console.Info()  
> # puts "Cursor at #{info[3]}, #{info[4]}."  
> #  
> # - Input  
> #  
> # Reads an event from the input buffer. Returns an array of values, which  
> # depending on the event's nature are:  
> #  
> # - keyboard event  
> #  
> # The array will contain:  
> #  
> # \* event type: 1 for keyboard  
> #  
> # \* key down: TRUE if the key is being pressed, FALSE if the key is being released  
> #  
> # \* repeat count: the number of times the key is being held down  
> #  
> # \* virtual keycode: the virtual key code of the key  
> #  
> # \* virtual scancode: the virtual scan code of the key  
> #  
> # \* char: the ASCII code of the character (if the key is a character key, 0 otherwise)  
> #  
> # \* control key state: the state of the control keys (SHIFTs, CTRLs, ALTs, etc.)  
> #  
> # - mouse event  
> #  
> # The array will contain:  
> #  
> # \* event type: 2 for mouse  
> #  
> # \* mouse pos. X: X coordinate (column) of the mouse location  
> #  
> # \* mouse pos. Y: Y coordinate (row) of the mouse location  
> #  
> # \* button state: the mouse button(s) which are pressed  
> #  
> # \* control key state: the state of the control keys (SHIFTs, CTRLs, ALTs, etc.)  
> #  
> # \* event flags: the type of the mouse event  
> #  
> # This method will return \<b\>nil\</b\> on errors. Note that the events  
> # returned are depending on the input \<b\>Mode\</b\> of the console; for example,  
> # mouse events are not intercepted unless ENABLE\_MOUSE\_INPUT is  
> # specified. See also: \<b\>GetEvents\</b\>, \<b\>InputChar\</b\>, \<b\>Mode\</b\>,  
> # \<b\>PeekInput\</b\>, \<b\>WriteInput\</b\>.  
> #  
> # Example:  
> #  
> # event = console.Input()  
> #  
> # - InputChar number  
> #  
> # Reads and returns I\<number\> characters from the console input buffer,  
> # or \<b\>nil\</b\> on errors. See also: \<b\>Input\</b\>, \<b\>Mode\</b\>.  
> #  
> # Example:  
> #  
> # key = console.InputChar(1)  
> #  
> # - InputCP [codepage]  
> #  
> # Gets or sets the input code page used by the console. Note that this  
> # doesn't apply to a console object, but to the standard input  
> # console. This attribute is used by the Write method. See also:  
> # \<b\>OutputCP\</b\>.  
> #  
> # Example:  
> #  
> # codepage = Win32::Console::InputCP()  
> # Win32::Console::InputCP(437)  
> #  
> # - MaxWindow  
> #  
> # Returns the size of the largest possible console window, based on the  
> # current font and the size of the display. The result is \<b\>nil\</b\> on  
> # errors, otherwise a 2-element list containing col, row.  
> #  
> # Example:  
> #  
> # maxCol, maxRow = console.MaxWindow()  
> #  
> # - Mode [flags]  
> #  
> # Gets or sets the input or output mode of a console. I\<flags\> can be a  
> # combination of the following constants:  
> #  
> # ENABLE\_LINE\_INPUT  
> # ENABLE\_ECHO\_INPUT  
> # ENABLE\_PROCESSED\_INPUT  
> # ENABLE\_WINDOW\_INPUT  
> # ENABLE\_MOUSE\_INPUT  
> # ENABLE\_PROCESSED\_OUTPUT  
> # ENABLE\_WRAP\_AT\_EOL\_OUTPUT  
> #  
> # For more informations on the meaning of those flags, please refer to  
> # the L\<"Microsoft's Documentation"\>.  
> #  
> # Example:  
> #  
> # mode = console.Mode()  
> # console.Mode(ENABLE\_MOUSE\_INPUT | ENABLE\_PROCESSED\_INPUT)  
> #  
> # - MouseButtons  
> #  
> # Returns the number of the buttons on your mouse, or \<b\>nil\</b\> on errors.  
> #  
> # Example:  
> #  
> # puts "Your mouse has #{Win32::Console::MouseButtons()} buttons."  
> #  
> # - Win32::Console.new standard\_handle  
> #  
> # - Win32::Console.new [accessmode, sharemode]  
> #  
> # Creates a new console object. The first form creates a handle to a  
> # standard channel, I\<standard\_handle\> can be one of the following:  
> #  
> # STD\_OUTPUT\_HANDLE  
> # STD\_ERROR\_HANDLE  
> # STD\_INPUT\_HANDLE  
> #  
> # The second form, instead, creates a console screen buffer in memory,  
> # which you can access for reading and writing as a normal console, and  
> # then redirect on the standard output (the screen) with \<b\>Display\</b\>. In  
> # this case, you can specify one or both of the following values for  
> # I\<accessmode\>:  
> #  
> # GENERIC\_READ  
> # GENERIC\_WRITE  
> #  
> # which are the permissions you will have on the created buffer, and one  
> # or both of the following values for I\<sharemode\>:  
> #  
> # FILE\_SHARE\_READ  
> # FILE\_SHARE\_WRITE  
> #  
> # which affect the way the console can be shared. If you don't specify  
> # any of those parameters, all 4 flags will be used.  
> #  
> # Example:  
> #  
> # stdout = Win32::Console.new(STD\_OUTPUT\_HANDLE)  
> # stderr = Win32::Console.new(STD\_ERROR\_HANDLE)  
> # stdin = Win32::Console.new(STD\_INPUT\_HANDLE)  
> #  
> # buffer = Win32::Console.new()  
> # buffer = Win32::Console.new(GENERIC\_READ | GENERIC\_WRITE)  
> #  
> [snip]
> 
> Matt Hulse wrote:
> 
> > Is there a Ruby equivalent to the WConio Python Module or similar conio  
> > type support for windows?  
> > ([http://newcenturycomputers.net/projects/wconio.html\](http://newcenturycomputers.net/projects/wconio.html%5C))
> > 
> > If not, does anyone have some pointers or documention about porting  
> > this module from Python to Ruby that might help me take a stab at it?
> > 
> > I appreciate any help.
> > 
> > --Matt
