Module: TTY::Reader::WinAPI

Includes:
Fiddle
Defined in:
lib/tty/reader/win_api.rb

Constant Summary collapse

Handle =
RUBY_VERSION >= "2.0.0" ? Fiddle::Handle : DL::Handle
CRT_HANDLE =
Handle.new("msvcrt") rescue Handle.new("crtdll")

Class Method Summary collapse

Class Method Details

.getchString

Get a character from the console without echo.

Returns:

  • (String)

    return the character read



21
22
23
24
# File 'lib/tty/reader/win_api.rb', line 21

def getch
  @@getch ||= Fiddle::Function.new(CRT_HANDLE["_getch"], [], TYPE_INT)
  @@getch.call
end

.getcheString

Gets a character from the console with echo.

Returns:

  • (String)

    return the character read



33
34
35
36
# File 'lib/tty/reader/win_api.rb', line 33

def getche
  @@getche ||= Fiddle::Function.new(CRT_HANDLE["_getche"], [], TYPE_INT)
  @@getche.call
end

.kbhitInteger

Check the console for recent keystroke. If the function returns a nonzero value, a keystroke is waiting in the buffer.

Returns:

  • (Integer)

    return a nonzero value if a key has been pressed. Otherwirse, it returns 0.



47
48
49
50
# File 'lib/tty/reader/win_api.rb', line 47

def kbhit
  @@kbhit ||= Fiddle::Function.new(CRT_HANDLE["_kbhit"], [], TYPE_INT)
  @@kbhit.call
end