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.

Force pressing a button IN AN EVENT

Hey.
I'm looking for a way, that forces pressing a button.
Not like
Con. Branch: "If [Any key] was pressed, then Blahblah".

Something like:
Blahblah
Control variable[1] =5

Con. Branch: V[1]==5
then
  FlashScreen
  Play SE "X"
  FORCE pressing button X (X= any...)

It's because I'm using an ABS and I want to use a skill if a variable is X... and I've got no scripting knowledges.
And because of the ABS, Force Action doesn't work.

Final Griever
 

e

Sponsor

Done and done, good Sir!

First of all, you'll need to go in the script editor and insert a new script. Label it whatever you wish (i.e.: Input::trigger). Then insert the following code :
Note: I only put the RMVX/RMXP possible characters, so with Alework's input module or something like that I don't know if it'd work.
Code:
module Input
  # Constants
  SCAN_CODES = { 
    :space => { :charCode => 0x20, :scanCode => 0x39 }, 
    :enter => { :charCode => 0x0D, :scanCode => 0x1C }, 
    :escape => { :charCode => 0x1B, :scanCode => 0x01 }, 
    :num0 => { :charCode => 0x60, :scanCode => 0x52 }, 
    :shift => { :charCode => 0x10, :scanCode => 0x2A }, 
    :z => { :charCode => 0x5A, :scanCode => 0x2C }, 
    :x => { :charCode => 0x58, :scanCode => 0x2D }, 
    :c => { :charCode => 0x43, :scanCode => 0x2E }, 
    :v => { :charCode => 0x56, :scanCode => 0x2F }, 
    :b => { :charCode => 0x42, :scanCode => 0x30 },
    :a => { :charCode => 0x41, :scanCode => 0x1E }, 
    :s => { :charCode => 0x53, :scanCode => 0x1F }, 
    :d => { :charCode => 0x44, :scanCode => 0x20 }, 
    :q => { :charCode => 0x51, :scanCode => 0x10 }, 
    :w => { :charCode => 0x57, :scanCode => 0x11 }
  }
  KEYEVENTF_EXTENDEDKEY = 0x1
  KEYEVENTF_KEYUP = 0x2
  KEYEVENTF_KEYDOWN = 0x0
  INPUT_KEYBOARD = 0x1
  
  def self.trigger(key)
    fn = Win32API.new("user32", "keybd_event", ['i', 'i', 'l', 'i'], 'V')
    fn.call(SCAN_CODES[key][:charCode], SCAN_CODES[key][:scanCode], 
            KEYEVENTF_KEYDOWN, 0)
  end
end

Then, where you want to trigger a key being pressed, you do :

Code:
Input.trigger(THE KEY BEING PRESSED)

Now, THE KEY BEING PRESSED part is a symbol; you should look at the SCAN_CODES table's keys for the possible symbols, which are :

Code:
:space
:enter
:escape
:num0
:shift
:z
:x
:c
:v
:b
:a
:s
:d
:q
:w

The colon in front of the letter/word is very important!

Also, note that this will only simulate that the player pressed this key on his keyboard; it is not the Input module's A key, for example, but the keyboard's A key, which are two different things. I could puzzle out how RMXP/VX assigned its key numbers.
 

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