Third333strike
Member
Is there a way to disable all keyboard input for a set amount of time?
#==============================================================================
# ** Input
#==============================================================================
module Input
#------------------------------------------------------------------------
# * Disabled Keys
#------------------------------------------------------------------------
@disabled_keys = []
@disabled_timer = {}
class << self
#------------------------------------------------------------------------
# * Alias Listings
#------------------------------------------------------------------------
unless self.method_defined?(:seph_disabledkeys_input_update)
alias_method :seph_disabledkeys_input_update, :update
alias_method :seph_disabledkeys_input_press?, :press?
alias_method :seph_disabledkeys_input_trigger?, :trigger?
alias_method :seph_disabledkeys_input_repeat?, :repeat?
alias_method :seph_disabledkeys_input_dir4, :dir4
alias_method :seph_disabledkeys_input_dir8, :dir8
end
#------------------------------------------------------------------------
# * Name : Disable Key
# Info : Disables Input Constant from being read
# Author : SephirothSpawn
# Call Info : Constant defined in Input Module to be disabled
# Frames to be disabled (nil for infinite frames)
#------------------------------------------------------------------------
def disable_key(constant, frames = nil)
# Add Key to Disabled List
@disabled_keys << constant unless @disabled_keys.include?(constant)
# Set Disabled Counter if non-nil
@disabled_timer[constant] = frames unless frames.nil?
end
#------------------------------------------------------------------------
# * Name : Enable Key
# Info : Enable Input Constant
# Author : SephirothSpawn
# Call Info : Constant defined in Input Module to be enabled
#------------------------------------------------------------------------
def enable_key(constant)
# Remove Constant From List
@disabled_keys.delete(constant)
# Set Nil Timer
@disabled_timer[constant] = nil
end
#------------------------------------------------------------------------
# * Frame Update
#------------------------------------------------------------------------
def update
# Pass Through Timer List
@disabled_timer.each do |key, timer|
# Next if nil timer or key not-disabled
next if timer.nil? || !@disabled_keys.include?(key)
# If Greater than 0 Timer
if timer > 0
timer -= 1
next
end
# Enable Key
@disabled_keys.delete(key) if @disabled_keys.include?(key)
# Set Timer to Nil
@disabled_timer[key] = nil
end
# Original Update
seph_disabledkeys_input_update
end
#------------------------------------------------------------------------
# * Press? Test
#------------------------------------------------------------------------
def press?(constant)
return @disabled_keys.include?(constant) ?
false : seph_disabledkeys_input_press?(constant)
end
#------------------------------------------------------------------------
# * Trigger? Test
#------------------------------------------------------------------------
def trigger?(constant)
return @disabled_keys.include?(constant) ?
false : seph_disabledkeys_input_trigger?(constant)
end
#------------------------------------------------------------------------
# * Repeat? Test
#------------------------------------------------------------------------
def repeat?(constant)
return @disabled_keys.include?(constant) ?
false : seph_disabledkeys_input_repeat?(constant)
end
#------------------------------------------------------------------------
# * Dir4 Test
#------------------------------------------------------------------------
def dir4
# Gets Original Direction Test
dir = seph_disabledkeys_input_dir4
# Return 0 if Direction Disabled
if (dir == 2 && @disabled_keys.include?(DOWN)) ||
(dir == 4 && @disabled_keys.include?(LEFT)) ||
(dir == 6 && @disabled_keys.include?(RIGHT)) ||
(dir == 8 && @disabled_keys.include?(UP))
return 0
end
# Return Original Dir Test
return dir
end
#------------------------------------------------------------------------
# * Dir8 Test
#------------------------------------------------------------------------
def dir8
# Gets Original Direction Test
dir = seph_disabledkeys_input_dir8
# Return 0 if Direction Disabled
if (dir == 2 && @disabled_keys.include?(DOWN)) ||
(dir == 4 && @disabled_keys.include?(LEFT)) ||
(dir == 6 && @disabled_keys.include?(RIGHT)) ||
(dir == 8 && @disabled_keys.include?(UP)) ||
(dir == 1 && @disabled_keys.include?(DOWN) &&
@disabled_keys.include?(LEFT)) ||
(dir == 3 && @disabled_keys.include?(DOWN) &&
@disabled_keys.include?(RIGHT)) ||
(dir == 7 && @disabled_keys.include?(UP) &&
@disabled_keys.include?(LEFT)) ||
(dir == 9 && @disabled_keys.include?(UP) &&
@disabled_keys.include?(RIGHT))
return 0
end
# Return Original Dir Test
return dir
end
end
end
Input.disable_key(constant, frames)
# Replace constant with Input::UP, Input::DOWN, Input::LEFT, Input::RIGHT, :Input::Alt, etc.
# Replace frames with the number of frames to delay or nil, to delay until re-enabled.
Input.enable_key(constant)
for c in [Input::DOWN, Input::LEFT, Input::RIGHT, Input::UP, Input::A, Input::B, Input::C, Input::L, Input::R, Input::X, Input::Y, Input::Z, Input::ALT, Input::CTRL, Input::SHIFT]
Input.disable_key(c, frames)
end