Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Use keyboard

Garoe edited this page Jun 6, 2013 · 2 revisions

#Use keyboard In this tutorial we are going to explain how to use the software keyboard in an Android device with Gosu Android. To do this lest take a look at use_keyboard_activity.rb

require 'gosu'

class GameWindow < Gosu::Window
  def initialize
    #Creates a window of 600 by 400, not fullscreen, at 30 fps
    super 600, 480, false, 30
    self.caption = "Gosu Use Keyboard"
    @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
    #Bring up the keyboard
    self.show_soft_keyboard
  end

  def button_down(id)
    #Save the key value
    @key = id
  end
  
  def draw
    #Show which key the user pressed
    @font.draw("Press a key to see its code: #{@key}", 10, 10, 3, 1.0, 1.0, 0xffffff00)
  end
end

#Initialize gosu for android
class UseKeyboardActivity
  def on_create(bundle)
    super(bundle)
    Gosu::AndroidInitializer.instance.start(self)
    rescue Exception => e
      puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"    
  end  
  
  def on_ready
    window = GameWindow.new
    window.show  
    rescue Exception => e
      puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"    
  end
end

We see here a simple activity that creates a Window. On the Window initialization we got the line self.show_soft_keyboard this will bring up the software keyboard on an Android device that lacks a physical one.

Once you are done typing we provide the method self.hide_soft_keyboard this method will make the software keyboard disappear.

Clone this wiki locally