Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Keyboard Module

Ive got my keyboard module written, and everything seemed to be working correctly, but when i use Keyboard.Triggered?(Input['Enter']) it doesnt work. I removed what I had for Triggered?(key) and I was wondering if anyone could tell me, or point me to another Keyboard script that it does work on.

This is what I got so far... (not the entire code, just the part that is causing the problem)
Code:
def Triggered?(key)
    
end
  
def Pressed?(key)
  return true unless GetKeyState.call(Input[key]).between?(0,1)
  return false
end
  
def Toggle?(key)
  GetKeyState.call(Input[key]) & 0x01 == 1
end
  
def Toggled?(key)
  return false unless GetKeyState.call(Input[key]) & 0x01 == 1
  return true
end
Okay,
Pressed? is for when a key is being held down.
Toggle? is to toggle a key.
Toggled? is to check if a key is toggled or not.
But I dont know what code is used to just press a key once, and then release it.


Thank you so much,
~Brian
 
In most of the Keyboard scripts the triggering utility is done with an update, where it checks whether a pre-specified key has triggered. Look:
Code:
module Keyboard
  @triggered = []
  CHECK = [Input['Enter'], Input['Esc']]

  def self.update
    temp = []
    for key in CHECK
      state = self.Pressed?(key)
      temp.push key if state and !@triggered.include?(key)
    end
    @triggered = temp
  end

  def self.Trigger?(key)
    return true if @triggered.include?(key)
  end

end
Hope you got this small explanation (Sorry i´m tired ^^)
 
I dont really understand, Im kinda tired too, but my friend is over, who is helping me on this, and he doesnt understand either. Ill try to figure it out with what you posted, but if you think of an easier way to explain it, could you please let me know?

Thank you.
~Brian
 
Like, CHECK contains all the keys' values that you want to ckeck for trigger. For instance, space is addressed by a hexadecimal number equal to 0x20, and this number is defined at Keyboard::Space:
Code:
module Keyboard
  Space = 0x20
end
So, your space is represented by Keyboard::Space. Now you have to check whether the key has triggered or not. A triggered key is a key that haven´t been pressed since the last check/update and that is being pressed now.

To avoid making the game lag while checking the trigger, we select the keys we would check. For that i simply define an array that contains all the values of all the keys that we wanna check:
Code:
module Keyboard
  KEYS = [Keyboard::Space]
end
And also, we need a place to store the triggered keys:
Code:
module Keyboard
  @triggered = []
end
Now let´s comment on the update:
Code:
  def self.update
    # Create a temporary handler to check the triggering
    temp = []
    # Evaluate all the keys included at Keys
    for key in CHECK
      # Checking the actual state of the key
      state = self.Pressed?(key)
      # HERE IS WHERE THE MAGIC HAPPENS
      # If the key is actually active now and inactive in the last update,
      # push it to the temporary handler
      temp.push key if state and !@triggered.include?(key)
    end
    # Equals the Triggered keys variable to the temporary handler
    @triggered = temp
  end

And the Trigger...
Code:
  # This just checks to see if the key value is inside @triggered
  # in other means, if it was triggered.
  def self.Trigger?(key)
    return true if @triggered.include?(key)
  end

That way you can use
Code:
Keyboard.Trigger?(Keyboard::Space)
The same way you do with
Code:
Input.trigger?(Input::C)
With the same disadvantage of having to update the module on each frame.

Got it?
 
Substitute every Input.update by Keyboard.update on the main loop on all the Scene_ classes. Or even put them together. As i´ve said, it´s supposed to be somehow like the default Input module.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top