module MiniGL::KB

Exposes methods for controlling keyboard and gamepad events.

Public Class Methods

initialize() click to toggle source

This is called by GameWindow.initialize. Don’t call it explicitly.

# File lib/minigl/global.rb, line 265
def initialize
  @keys = [
    Gosu::KB_A, Gosu::KB_B, Gosu::KB_C, Gosu::KB_D, Gosu::KB_E, Gosu::KB_F,
    Gosu::KB_G, Gosu::KB_H, Gosu::KB_I, Gosu::KB_J, Gosu::KB_K, Gosu::KB_L,
    Gosu::KB_M, Gosu::KB_N, Gosu::KB_O, Gosu::KB_P, Gosu::KB_Q, Gosu::KB_R,
    Gosu::KB_S, Gosu::KB_T, Gosu::KB_U, Gosu::KB_V, Gosu::KB_W, Gosu::KB_X,
    Gosu::KB_Y, Gosu::KB_Z, Gosu::KB_1, Gosu::KB_2, Gosu::KB_3, Gosu::KB_4,
    Gosu::KB_5, Gosu::KB_6, Gosu::KB_7, Gosu::KB_8, Gosu::KB_9, Gosu::KB_0,
    Gosu::KB_NUMPAD_1, Gosu::KB_NUMPAD_2, Gosu::KB_NUMPAD_3, Gosu::KB_NUMPAD_4,
    Gosu::KB_NUMPAD_5, Gosu::KB_NUMPAD_6, Gosu::KB_NUMPAD_7, Gosu::KB_NUMPAD_8,
    Gosu::KB_NUMPAD_9, Gosu::KB_NUMPAD_0, Gosu::KB_F1, Gosu::KB_F2,
    Gosu::KB_F3, Gosu::KB_F4, Gosu::KB_F5, Gosu::KB_F6, Gosu::KB_F7,
    Gosu::KB_F8, Gosu::KB_F9, Gosu::KB_F10, Gosu::KB_F11, Gosu::KB_F12,
    Gosu::KB_APOSTROPHE, Gosu::KB_BACKSLASH, Gosu::KB_BACKSPACE,
    Gosu::KB_BACKTICK, Gosu::KB_COMMA, Gosu::KB_DELETE, Gosu::KB_DOWN,
    Gosu::KB_END, Gosu::KB_ENTER, Gosu::KB_EQUALS, Gosu::KB_ESCAPE,
    Gosu::KB_HOME, Gosu::KB_INSERT, Gosu::KB_ISO, Gosu::KB_LEFT,
    Gosu::KB_LEFT_ALT, Gosu::KB_LEFT_BRACKET, Gosu::KB_LEFT_CONTROL,
    Gosu::KB_LEFT_META, Gosu::KB_LEFT_SHIFT, Gosu::KB_MINUS,
    Gosu::KB_NUMPAD_DIVIDE, Gosu::KB_NUMPAD_MINUS,
    Gosu::KB_NUMPAD_MULTIPLY, Gosu::KB_NUMPAD_PLUS, Gosu::KB_PAGE_DOWN,
    Gosu::KB_PAGE_UP, Gosu::KB_PERIOD, Gosu::KB_RETURN, Gosu::KB_RIGHT,
    Gosu::KB_RIGHT_ALT, Gosu::KB_RIGHT_BRACKET, Gosu::KB_RIGHT_CONTROL,
    Gosu::KB_RIGHT_META, Gosu::KB_RIGHT_SHIFT, Gosu::KB_SEMICOLON,
    Gosu::KB_SLASH, Gosu::KB_SPACE, Gosu::KB_TAB, Gosu::KB_UP,
    Gosu::GP_0_BUTTON_0, Gosu::GP_0_BUTTON_1, Gosu::GP_0_BUTTON_2, Gosu::GP_0_BUTTON_3,
    Gosu::GP_0_BUTTON_4, Gosu::GP_0_BUTTON_5, Gosu::GP_0_BUTTON_6, Gosu::GP_0_BUTTON_7,
    Gosu::GP_0_BUTTON_8, Gosu::GP_0_BUTTON_9, Gosu::GP_0_BUTTON_10, Gosu::GP_0_BUTTON_11,
    Gosu::GP_0_BUTTON_12, Gosu::GP_0_BUTTON_13, Gosu::GP_0_BUTTON_14, Gosu::GP_0_BUTTON_15,
    Gosu::GP_0_DOWN, Gosu::GP_0_LEFT, Gosu::GP_0_RIGHT, Gosu::GP_0_UP,
    Gosu::GP_1_BUTTON_0, Gosu::GP_1_BUTTON_1, Gosu::GP_1_BUTTON_2, Gosu::GP_1_BUTTON_3,
    Gosu::GP_1_BUTTON_4, Gosu::GP_1_BUTTON_5, Gosu::GP_1_BUTTON_6, Gosu::GP_1_BUTTON_7,
    Gosu::GP_1_BUTTON_8, Gosu::GP_1_BUTTON_9, Gosu::GP_1_BUTTON_10, Gosu::GP_1_BUTTON_11,
    Gosu::GP_1_BUTTON_12, Gosu::GP_1_BUTTON_13, Gosu::GP_1_BUTTON_14, Gosu::GP_1_BUTTON_15,
    Gosu::GP_1_DOWN, Gosu::GP_1_LEFT, Gosu::GP_1_RIGHT, Gosu::GP_1_UP,
    Gosu::GP_2_BUTTON_0, Gosu::GP_2_BUTTON_1, Gosu::GP_2_BUTTON_2, Gosu::GP_2_BUTTON_3,
    Gosu::GP_2_BUTTON_4, Gosu::GP_2_BUTTON_5, Gosu::GP_2_BUTTON_6, Gosu::GP_2_BUTTON_7,
    Gosu::GP_2_BUTTON_8, Gosu::GP_2_BUTTON_9, Gosu::GP_2_BUTTON_10, Gosu::GP_2_BUTTON_11,
    Gosu::GP_2_BUTTON_12, Gosu::GP_2_BUTTON_13, Gosu::GP_2_BUTTON_14, Gosu::GP_2_BUTTON_15,
    Gosu::GP_2_DOWN, Gosu::GP_2_LEFT, Gosu::GP_2_RIGHT, Gosu::GP_2_UP,
    Gosu::GP_3_BUTTON_0, Gosu::GP_3_BUTTON_1, Gosu::GP_3_BUTTON_2, Gosu::GP_3_BUTTON_3,
    Gosu::GP_3_BUTTON_4, Gosu::GP_3_BUTTON_5, Gosu::GP_3_BUTTON_6, Gosu::GP_3_BUTTON_7,
    Gosu::GP_3_BUTTON_8, Gosu::GP_3_BUTTON_9, Gosu::GP_3_BUTTON_10, Gosu::GP_3_BUTTON_11,
    Gosu::GP_3_BUTTON_12, Gosu::GP_3_BUTTON_13, Gosu::GP_3_BUTTON_14, Gosu::GP_3_BUTTON_15,
    Gosu::GP_3_DOWN, Gosu::GP_3_LEFT, Gosu::GP_3_RIGHT, Gosu::GP_3_UP,
  ]
  @down = []
  @prev_down = []
  @held_timer = {}
  @held_interval = {}
end
key_down?(key) click to toggle source

Returns whether the given key is down in the current frame.

Parameters:

key

Code of the key to be checked. The available codes are all the constants in Gosu started with KB_.

# File lib/minigl/global.rb, line 360
def key_down?(key)
  @down.index(key)
end
key_held?(key) click to toggle source

Returns whether the given key is being held down. See GameWindow.initialize for details.

Parameters:

key

Code of the key to be checked. The available codes are all the constants in Gosu started with KB_.

# File lib/minigl/global.rb, line 380
def key_held?(key)
  @held_interval[key] == G.kb_held_interval
end
key_pressed?(key) click to toggle source

Returns whether the given key is down in the current frame and was not down in the frame before.

Parameters:

key

Code of the key to be checked. The available codes are all the constants in Gosu started with KB_.

# File lib/minigl/global.rb, line 351
def key_pressed?(key)
  @prev_down.index(key).nil? and @down.index(key)
end
key_released?(key) click to toggle source

Returns whether the given key is not down in the current frame but was down in the frame before.

Parameters:

key

Code of the key to be checked. The available codes are all the constants in Gosu started with KB_.

# File lib/minigl/global.rb, line 370
def key_released?(key)
  @prev_down.index(key) and @down.index(key).nil?
end
update() click to toggle source

Updates the state of all keys.

# File lib/minigl/global.rb, line 318
def update
  @held_timer.each do |k, v|
    if v < G.kb_held_delay; @held_timer[k] += 1
    else
      @held_interval[k] = 0
      @held_timer.delete k
    end
  end

  @held_interval.each do |k, v|
    if v < G.kb_held_interval; @held_interval[k] += 1
    else; @held_interval[k] = 0; end
  end

  @prev_down = @down.clone
  @down.clear
  @keys.each do |k|
    if G.window.button_down? k
      @down << k
      @held_timer[k] = 0 if @prev_down.index(k).nil?
    elsif @prev_down.index(k)
      @held_timer.delete k
      @held_interval.delete k
    end
  end
end