How to make the keys pressed?

I don't mean a conditional branch to check if the keys are pressed, I mean a way to make the game press the buttons A through R. I think if they are defined, they can be pressed and script called. Like what Roninator2 requested:

Haven't hear of someone asking to make it press those keys. Usually it's a conditional branch to check if the keys are pressed.

I made a different thread because it's a different request.

index.php
What you need is a keyboard script to use keys outside the default RPG Maker controls.
Perhaps this?

because what you're saying is keys a,b,c,d,e,f,g,h,i,j,k,l etc... Correct?
What you need is a keyboard script to use keys outside the default RPG Maker controls.
Perhaps this?

because what you're saying is keys a,b,c,d,e,f,g,h,i,j,k,l etc... Correct?

Thank you. Correct. I am using the script. Um, how do I use it to make the keys be pressed by the game?
I had to go searching, there was a previous poster that was using another script for the same thing. Here
https://forums.rpgmakerweb.com/index.php?threads/fyxinput.34073/
This is another keyboard script. In it you use the commands I mentioned before.
Input.press?
but instead of ( :A ) you use (0x25) for the key that you want to call.
Code:
Input.press?(0x25)

So try that and if you have problems then try the other script.
you cannot (and should not) add any data to the keyboard queue, as that is handled by windows and any try to add data there is usually considered a virus action or a hacking try and usually blocked by your anti-malware programs.

You need to bypass your idea of the keyboard press entirely - there are only two options for you:
If you want the game to react to a key, you need to check if it is pressed and then cause an action directly.
That is what is described above.

If you have an existing script that reacts to a key and want to trigger the same function automatically, you need to seach the script for the place where the key is checked, look for the function it calls when that key is pressed - and then call that function directly, without going through the keyboard at all.
You can force a "key press" using a WinAPI, no problem there.
But as long as that pesky F1 control menu is enabled in your game, there is no way to check which keys the player has set for which "buttons" there, not without opening the engine's DLL to check where that data is stored (you can actually read that from the registry too, but I won't go there, sorry).

If you use a custom keyboard script, AND disable the built-in F1 setting menu, than you can force a key press without worrying about these controls.

You can use this snippet for that:
Spoiler
Code:
class << Input

  SendInput = Win32API.new('user32', 'SendInput', 'ipi', 'i')

  def press_keys(*kys) # args: (key_code1,key_code2,...)
    ary = []
    kys.each {|ky| ary.push(1,ky,0) }
    inputs = ary.pack('LSx2Lx16'*kys.size)
    SendInput.call(kys.size, inputs, 28)
  end

  def release_keys(*kys) # args: (key_code1,key_code2,...)
    ary = []
    kys.each {|ky| ary.push(1,ky,2) }
    inputs = ary.pack('LSx2Lx16'*kys.size)
    SendInput.call(kys.size, inputs, 28)
  end

  def queue_keys(*kys) # args: ([key_code1,0/2],[key_code2,0/2],...)
    ary = []           #         0 = press, 2 = release
    kys.each {|ky| ary.push(1,*ky) }
    inputs = ary.pack('LSx2Lx16'*kys.size)
    SendInput.call(kys.size, inputs, 28)
  end
 
end
You can use it with these code lines from anywhere:
Code:
Input.press_keys(key_code1,key_code2,...)
Input.release_keys(key_code1,key_code2,...)
Input.queue_keys([key_code1,0/2],[key_code2,0/2],...)
Replace the key_codeX arguments with any valid keycodes. You must know the code of the key you want to press for this, obviously.
For example, ALT is 18, ENTER is 13, X is 88, and so on.
You can search on google for keycodes easily, there are many sites with nice key code tables.
Make sure to search for decimal key codes, not hexadecimals. I never tested with hexadecimals, so no idea if the code works with those or not.
You can add multiple keys to be pressed at the same time if you want as well.
Note that once you "press" a key like this, it will remain pressed until you "release" it with the other code line, or until the player presses the key on the keyboard manually twice.
You WILL need proper scripting experience to do anything with this, you can't just randomly force key presses, or else you might even break your game.

I don't know for what do you want to use this, but now you have a way to do it, or at least the basics of it.

you cannot (and should not) add any data to the keyboard queue, as that is handled by windows and any try to add data there is usually considered a virus action or a hacking try and usually blocked by your anti-malware programs.
Never did I see anything like that blocked by any anti-malware program. It is a simple API usage. APIs are made to be used.
If something blocks this, than you should get rid of that something, because it is clearly a bad thing to have on any PC.
I had to go searching, there was a previous poster that was using another script for the same thing. Here
https://forums.rpgmakerweb.com/index.php?threads/fyxinput.34073/
This is another keyboard script. In it you use the commands I mentioned before.
Input.press?
but instead of ( :A ) you use (0x25) for the key that you want to call.
Code:
Input.press?(0x25)

So try that and if you have problems then try the other script.

Thank you! After looking at that script, I goggled "rpgmaker keystroke simulate" and found this: https://adrarian.wordpress.com/2014/03/11/input-simulator/

I think this is what you and I want...although it's only for QWERTY keyboards.

you cannot (and should not) add any data to the keyboard queue, as that is handled by windows and any try to add data there is usually considered a virus action or a hacking try and usually blocked by your anti-malware programs.

You need to bypass your idea of the keyboard press entirely - there are only two options for you:
If you want the game to react to a key, you need to check if it is pressed and then cause an action directly.
That is what is described above.

If you have an existing script that reacts to a key and want to trigger the same function automatically, you need to seach the script for the place where the key is checked, look for the function it calls when that key is pressed - and then call that function directly, without going through the keyboard at all.

Thank you, I'll search Keyboard Manager for where the key is checked and, if possible, script call the function.

You can force a "key press" using a WinAPI, no problem there.
But as long as that pesky F1 control menu is enabled in your game, there is no way to check which keys the player has set for which "buttons" there, not without opening the engine's DLL to check where that data is stored (you can actually read that from the registry too, but I won't go there, sorry).

If you use a custom keyboard script, AND disable the built-in F1 setting menu, than you can force a key press without worrying about these controls.

You can use this snippet for that:
Spoiler
Code:
class << Input

  SendInput = Win32API.new('user32', 'SendInput', 'ipi', 'i')

  def press_keys(*kys) # args: (key_code1,key_code2,...)
    ary = []
    kys.each {|ky| ary.push(1,ky,0) }
    inputs = ary.pack('LSx2Lx16'*kys.size)
    SendInput.call(kys.size, inputs, 28)
  end

  def release_keys(*kys) # args: (key_code1,key_code2,...)
    ary = []
    kys.each {|ky| ary.push(1,ky,2) }
    inputs = ary.pack('LSx2Lx16'*kys.size)
    SendInput.call(kys.size, inputs, 28)
  end

  def queue_keys(*kys) # args: ([key_code1,0/2],[key_code2,0/2],...)
    ary = []           #         0 = press, 2 = release
    kys.each {|ky| ary.push(1,*ky) }
    inputs = ary.pack('LSx2Lx16'*kys.size)
    SendInput.call(kys.size, inputs, 28)
  end
 
end
You can use it with these code lines from anywhere:
Code:
Input.press_keys(key_code1,key_code2,...)
Input.release_keys(key_code1,key_code2,...)
Input.queue_keys([key_code1,0/2],[key_code2,0/2],...)
Replace the key_codeX arguments with any valid keycodes. You must know the code of the key you want to press for this, obviously.
For example, ALT is 18, ENTER is 13, X is 88, and so on.
You can search on google for keycodes easily, there are many sites with nice key code tables.
Make sure to search for decimal key codes, not hexadecimals. I never tested with hexadecimals, so no idea if the code works with those or not.
You can add multiple keys to be pressed at the same time if you want as well.
Note that once you "press" a key like this, it will remain pressed until you "release" it with the other code line, or until the player presses the key on the keyboard manually twice.
You WILL need proper scripting experience to do anything with this, you can't just randomly force key presses, or else you might even break your game.

I don't know for what do you want to use this, but now you have a way to do it, or at least the basics of it.


Never did I see anything like that blocked by any anti-malware program. It is a simple API usage. APIs are made to be used.
If something blocks this, than you should get rid of that something, because it is clearly a bad thing to have on any PC.

Thank you!
OK, the Input Simulator is making Vlue's Mouse System do something funny, see pic. I think it's because the Input Simulator scripts aren't script calls.

Spoiler
Capture.PNG

At top is the error message I receive when I click on a mouse button. At left is the line that the error message references. At right are the mouse buttons. "simulate_key, etc." is from Input Simulator:

Spoiler
Capture2.PNG

Like I said, I think the problem is that Input Simulator's scripts aren't script calls, which the mouse buttons use.
From what I can tell,
You are right that the two are not talking to each other in the same method.
I would suggest to call a common event where you can run the keyboard input in a script call.
$game_temp.reserve_common_event(ID)
From what I can tell,
You are right that the two are not talking to each other in the same method.
I would suggest to call a common event where you can run the keyboard input in a script call.
$game_temp.reserve_common_event(ID)

Thank you. Do I put the script into the mouse buttons where I once had the input scripts, and put the input scripts into the common event?

Edit: OK, I made a common event for each mouse button and put one input script in each, but I'm getting the same errors.

Spoiler
error.PNG
You're using the input code for the common event script call?
simulate_key(key, duration)

I can't see the picture.
@Burgerland
"syntax error" is the computers way of telling you that you made a typo somewhere in your scripts.
And since it's in the eval command, it is in your script line.

At minimum you're missing a ) in that line, but the entire line looks a bit funny and I can't tell if it is because of how the script works that you're calling or if you made more mistakes.
You're using the input code for the common event script call?
simulate_key(key, duration)

I can't see the picture.

Thank you. Yes, that's the script call I'm using. I just added a ")" - which I forgot - to the line.

@Burgerland
"syntax error" is the computers way of telling you that you made a typo somewhere in your scripts.
And since it's in the eval command, it is in your script line.

At minimum you're missing a ) in that line, but the entire line looks a bit funny and I can't tell if it is because of how the script works that you're calling or if you made more mistakes.

Thank you. I added a ")" to the script. If you look at the image that includes Input Simulator that I uploaded, you can see it looks weird because that's how the script works. After adding a ")", I still get the same error message.

Here is what game interpreter looks like at line 1411:

scripts.PNG
the error has nothing to do with the eval command script, it is still an error in the line you wrote.
and I strongly suspect that is because you used a wrong data sequence for the "key" part.
Please tell us what key you intend to set there, and we can check if you used the correct entry there.

And please repost the current line as well as the current error message.
In the instructions.
Each key appears to start with '_' and end with the colon, but when using
the key for the input, the colon must go first. For example, :_enter

So I would say you could use
Code:
simulate_key(:_esc, 10)
the error has nothing to do with the eval command script, it is still an error in the line you wrote.
and I strongly suspect that is because you used a wrong data sequence for the "key" part.
Please tell us what key you intend to set there, and we can check if you used the correct entry there.

And please repost the current line as well as the current error message.

Thank you! I did use the wrong sequence after all.

In the instructions.
Each key appears to start with '_' and end with the colon, but when using
the key for the input, the colon must go first. For example, :_enter

So I would say you could use
Code:
simulate_key(:_esc, 10)

Thank you, I used the wrong placement for the colon. It's fixed now!

Sorry for being an idiot.
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-the-keys-pressed.79654/