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.



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

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)


341
342
343
# File 'lib/minigl/global.rb', line 341

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)


359
360
361
# File 'lib/minigl/global.rb', line 359

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)


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

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)


350
351
352
# File 'lib/minigl/global.rb', line 350

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

.updateObject

Updates the state of all keys.



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/minigl/global.rb', line 284

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