Permalink
Browse files

Renamed get_key_state to pressed_keys, and improved it.

* Runs SDL.PumpEvents() to ensure key state is up to date.

* The returned hash only has the keys that are pressed, non-pressed
  keys are omitted (instead of set to false). This is more
  lightweight, and easier to convert to a plain Array of the keys that
  are pressed.

* Simplified code.

* Rewrote docs.
  • Loading branch information...
jacius committed Feb 19, 2011
1 parent 6c01152 commit 52d3e40f3dd0a18af140039ca471b3af1ac7ab3b
Showing with 29 additions and 14 deletions.
  1. +1 −0 CREDITS.rdoc
  2. +28 −14 lib/rubygame/events.rb
View
@@ -69,4 +69,5 @@
- <b>Tyler Church</b>
Email:: tylertrain at yahoo.com
Contribution:: Patch to add Rubygame.get_key_state
+ (now Rubygame.pressed_keys)
Date:: 2010-09-06
View
@@ -21,6 +21,7 @@
#
# Changes:
# * Tyler Church, 2010-09-06: Added Rubygame.get_key_state
+# (now Rubygame.pressed_keys)
#
#++
@@ -88,21 +89,34 @@ def self.disable_key_repeat
end
- # Returns a hash of all the keys, if a key's value in the hash
- # is true, the key is currently pressed down, otherwise it's
- # value is false.
- #
- # Usage: Rubygame.get_key_state[:a]
- #
- def self.get_key_state
- hash = {}
- key_state = SDL.GetKeyState
- key_state.length.times do |i|
- state = (key_state[i] != 0)
- hash[Rubygame::Events._convert_key_symbol(i)] = state
+ # Returns a Hash of the keys that are currently being pressed on the
+ # keyboard. Keys that are being pressed with have a value of true.
+ # Keys that are not being pressed will not be in the Hash.
+ #
+ # Example:
+ #
+ # # Assuming that the "A" and left "Ctrl" keys are being pressed.
+ #
+ # Rubygame.pressed_keys
+ # # => {:a => true, :left_ctrl => true}
+ #
+ # Rubygame.pressed_keys.keys
+ # # => [:a, :left_ctrl]
+ #
+ # Rubygame.pressed_keys[:a]
+ # # => true
+ #
+ # # Not being pressed
+ # Rubygame.pressed_keys[:b]
+ # # => nil
+ #
+ def self.pressed_keys
+ SDL.PumpEvents()
+ keys = {}
+ SDL.GetKeyState().each_with_index do |state, key|
+ keys[Events._convert_key_symbol(key)] = true if state != 0
end
- hash.delete :unknown_key
- return hash
+ keys
end

0 comments on commit 52d3e40

Please sign in to comment.