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.



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
282
283
284
285
286
287
# File 'lib/minigl/global.rb', line 257

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
  ]
  @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. The available codes are all the constants in Gosu started with KB_.

Returns:

  • (Boolean)


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

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. The available codes are all the constants in Gosu started with KB_.

Returns:

  • (Boolean)


352
353
354
# File 'lib/minigl/global.rb', line 352

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 all the constants in Gosu started with KB_.

Returns:

  • (Boolean)


323
324
325
# File 'lib/minigl/global.rb', line 323

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. The available codes are all the constants in Gosu started with KB_.

Returns:

  • (Boolean)


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

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

.updateObject

Updates the state of all keys.



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/minigl/global.rb', line 290

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