class Gosu::TextInput

Overview

A TextInput is an invisible object that handles input using the operating system's input manager.

At its most basic, you only need to set Gosu::Window#text_input to an instance of this class. The TextInput will then handle all keyboard input until Gosu::Window#text_input is set to nil. Any text the user has typed is available through #text.

This class is purely back-end and does not come with a GUI; drawing the input field is up to you, the programmer. The best way to do that is left completely open. TextInput only aims to provide a foundation for you to build your own GUI.

see Gosu::Window#text_input

see file: examples/TextInput.cr

Defined in:

gosu/text_input.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def caret_pos : UInt32 #

The position of the editing caret.


[View source]
def caret_pos=(position : UInt32 | Int32) #

Sets the position of the editing caret to position.


[View source]
def delete_backward #

Deletes the current selection, if any, or the previous character.


[View source]
def delete_forward #

Deletes the current selection, if any, or the next character.


[View source]
def filter(text : String) : String #

This method is an overridable filter that is applied to all newly entered text. This allows for restricting input characters or format, automatic macro or abbreviation expansion and so on.

The return value of this method will be inserted at the current caret position.

The default implementation returns its argument unchanged.

Example: Forcing input to all uppercase, alphanumeric characters.

input = TextInput.new

def input.filter(text_in)
  text_in.upcase.gsub(/[^A-Z0-9]/, "")
end

[View source]
def insert_text(text : String) #

Replaces the current selection (if any) and inserts the given string at the current caret position. The filter method will not be applied before appending the string.


[View source]
def selection_start : UInt32 #

The starting position of the currently selected text.


[View source]
def selection_start=(position : UInt32 | Int32) #

Sets the starting position of the currently selected text to position.


[View source]
def text : String #

The text that the user has typed.


[View source]
def text=(text : String) #

Replaces TextInput text with #text.


[View source]