Module: MiniGL::KB

Defined in:
lib/minigl/global.rb

Overview

Exposes methods for controlling keyboard events.

Class Method Summary collapse

Class Method Details

.initializeObject

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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/minigl/global.rb', line 231

def initialize
  @keys = [
    Gosu::KbUp, Gosu::KbDown,
    Gosu::KbReturn, Gosu::KbEscape,
    Gosu::KbLeftControl, Gosu::KbRightControl,
    Gosu::KbLeftAlt, Gosu::KbRightAlt,
    Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
    Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
    Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
    Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
    Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
    Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
    Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
    Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
    Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
    Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
    Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab,
    Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
    Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
    Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash
  ]
  @down = []
  @prev_down = []
  @held_timer = {}
  @held_interval = {}
end

.key_down?(key) ⇒ Boolean

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

Parameters:

key

Code of the key to be checked. See key_pressed? for details.

Returns:

  • (Boolean)


316
317
318
# File 'lib/minigl/global.rb', line 316

def key_down?(key)
  @down.index(key)
end

.key_held?(key) ⇒ Boolean

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

Parameters:

key

Code of the key to be checked. See key_pressed? for details.

Returns:

  • (Boolean)


334
335
336
# File 'lib/minigl/global.rb', line 334

def key_held?(key)
  @held_interval[key] == G.kb_held_interval
end

.key_pressed?(key) ⇒ Boolean

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 Gosu::KbUp, Gosu::KbDown, Gosu::KbReturn, Gosu::KbEscape, Gosu::KbLeftControl, Gosu::KbRightControl, Gosu::KbLeftAlt, Gosu::KbRightAlt, Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF, Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL, Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR, Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX, Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4, Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0, Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4, Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8, Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace, Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome, Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab, Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft, Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe, Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash.

Returns:

  • (Boolean)


308
309
310
# File 'lib/minigl/global.rb', line 308

def key_pressed?(key)
  @prev_down.index(key).nil? and @down.index(key)
end

.key_released?(key) ⇒ Boolean

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. See key_pressed? for details.

Returns:

  • (Boolean)


325
326
327
# File 'lib/minigl/global.rb', line 325

def key_released?(key)
  @prev_down.index(key) and @down.index(key).nil?
end

.updateObject

Updates the state of all keys.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/minigl/global.rb', line 259

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