Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. Red Dead Redemption 2

      1. PC
      2. Gameplay
      3. Missions
      4. Help & Support
    2. Red Dead Online

      1. Gameplay
      2. Find Lobbies & Outlaws
      3. Help & Support
      4. Frontier Pursuits
    1. Crews & Posses

      1. Recruitment
    2. Events

    1. GTA Online

      1. Diamond Casino & Resort
      2. DLC
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Grand Theft Auto Series

    3. GTA 6

    4. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    5. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA IV Mods
    6. GTA Chinatown Wars

    7. GTA Vice City Stories

    8. GTA Liberty City Stories

    9. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA SA Mods
    10. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA VC Mods
    11. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA III Mods
    12. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    13. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption

    2. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. News

    2. Forum Support

    3. Site Suggestions

stillhere

Simulate keyboard presses OnTick?

Recommended Posts

stillhere

Hi, I'm trying to recreate something like this but I can't seem to get certain keys to work, mainly the ENTER and ADD keys (on the numpad). This is using SendKeys.

 

My script:

using GTA; // This is a reference that is needed! do not edit thisusing GTA.Math;using GTA.Native; // This is a reference that is needed! do not edit thisusing System; // This is a reference that is needed! do not edit thisusing System.Windows.Forms; // This is a reference that is needed! do not edit thisusing System.IO;namespace SimulateKeys{    public class SimulateKeys : Script // declare Modname as a script    {        bool JC2Hook = false;        int VehicleJetpackTimer;        int VehicleJetpackCounter = 0;        public SimulateKeys() // main function        {            Tick += OnTick;            KeyDown += OnKeyDown;            KeyUp += OnKeyUp;        }        void OnTick(object sender, EventArgs e) // This is where most of your script goes        {            Ped player = Game.Player.Character;            if (Game.IsControlPressed(2, GTA.Control.Aim) && Game.IsControlPressed(2, GTA.Control.VehicleHorn)) //Turn on script with Aim key + LS            {                if (JC2Hook)                {                    JC2Hook = false;                    SendKeys.SendWait("H");                    Wait(300);                }                else if (!JC2Hook)                {                    JC2Hook = true;                    SendKeys.SendWait("H");                    Wait(300);                }            }            if (player.IsInVehicle())            {                VehicleJetpackTimer++;                if (Game.IsControlJustPressed(2, GTA.Control.Sprint))                {                    VehicleJetpackTimer = 0;                    if (VehicleJetpackTimer < 25)                    {                        VehicleJetpackCounter++;                    }                    if (VehicleJetpackCounter == 2)                    {                        SendKeys.SendWait("{ENTER}");                        VehicleJetpackCounter = 0;                        Wait(300);                    }                }                                if (VehicleJetpackTimer == 25)                {                    VehicleJetpackCounter = 0;                }                UI.ShowSubtitle(VehicleJetpackCounter.ToString());            }        }        //https://msdn.microsoft.com/en-us/library/ms171548(v=vs.110).aspx        //https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys(v=vs.110).aspx        void OnKeyDown(object sender, KeyEventArgs e)        {        }        void OnKeyUp(object sender, KeyEventArgs e)        {        }    }}

The JC2Hook section works (I have the Just Cause 2 hook mod toggle to "H"). But the VehicleJetpack section is weird when using {ENTER}; what's weird is that it does press the ENTER key, but even though I have the Vehicle Jetpack mod set to enable the jetpack using ENTER (VK_ENTER), it doesn't enable the mod. I've also tried it with the Jetpack mod set to VK_ADD and SendKeys.SendWait("{ADD}").

 

Anyone, please enlighten me!

Share this post


Link to post
Share on other sites
leftas

Please check if that SendWait method does even get called, if so then need to investigate in there. But I guess it doesn't even get called.

 

All the best,

Paul

Share this post


Link to post
Share on other sites
Azorah

Put this on Tick

if (Game.IsKeyPressed = Keys.NumPadEnter){   // Do something}

Or just use this

        void OnKeyDown(object sender, KeyEventArgs e)        {                if (e.KeyCode == Keys.NumPadEnter)                {                    // Do something                }

Share this post


Link to post
Share on other sites
stillhere

Please check if that SendWait method does even get called, if so then need to investigate in there. But I guess it doesn't even get called.

 

All the best,

Paul

It is getting called, because it is working when I use SendKeys.SendWait("{ENTER}");
But it is not activating the Vehicle Jetpack mod which is set to enable when ENTER is pressed. When I use SendKeys.SendWait("H");, it does press H, and the JC2 Hook mod does activate, which is why it's so weird why it isn't working for that specific mod.

 

Put this on Tick

if (Game.IsKeyPressed = Keys.NumPadEnter){   // Do something}

Or just use this

        void OnKeyDown(object sender, KeyEventArgs e)        {                if (e.KeyCode == Keys.NumPadEnter)                {                    // Do something                }

My friend, that's not exactly what I'm asking. I am trying to simulate a keypress using Gamepad controls!

Edited by stillhere

Share this post


Link to post
Share on other sites
Azorah

 

Ohh, sorry I don't know how to help you with that then, best of luck!

 

 

Please check if that SendWait method does even get called, if so then need to investigate in there. But I guess it doesn't even get called.

 

All the best,

Paul

 

It is getting called, because it is working when I use SendKeys.SendWait("{ENTER}");

But it is not activating the Vehicle Jetpack mod which is set to enable when ENTER is pressed. When I use SendKeys.SendWait("H");, it does press H, and the JC2 Hook mod does activate, which is why it's so weird why it isn't working for that specific mod.

Put this on Tick

 

if (Game.IsKeyPressed = Keys.NumPadEnter){   // Do something}
Or just use this

 

void OnKeyDown(object sender, KeyEventArgs e)        {                if (e.KeyCode == Keys.NumPadEnter)                {                    // Do something                }
My friend, that's not exactly what I'm asking. I am trying to simulate a keypress using Gamepad controls!

Share this post


Link to post
Share on other sites
leftas

 

Put this on Tick

if (Game.IsKeyPressed = Keys.NumPadEnter){   // Do something}

Or just use this

        void OnKeyDown(object sender, KeyEventArgs e)        {                if (e.KeyCode == Keys.NumPadEnter)                {                    // Do something                }

Also one thing to mention - your first code is incorrect(it assigns to property(I guess you wanted check if equal), but it's not property, it's a method) :)

 

 

 

Please check if that SendWait method does even get called, if so then need to investigate in there. But I guess it doesn't even get called.

 

All the best,

Paul

It is getting called, because it is working when I use SendKeys.SendWait("{ENTER}");
But it is not activating the Vehicle Jetpack mod which is set to enable when ENTER is pressed. When I use SendKeys.SendWait("H");, it does press H, and the JC2 Hook mod does activate, which is why it's so weird why it isn't working for that specific mod.

 

Put this on Tick

if (Game.IsKeyPressed = Keys.NumPadEnter){   // Do something}

Or just use this

        void OnKeyDown(object sender, KeyEventArgs e)        {                if (e.KeyCode == Keys.NumPadEnter)                {                    // Do something                }

My friend, that's not exactly what I'm asking. I am trying to simulate a keypress using Gamepad controls!

 

Hmm, okay. But when you press enter on your keyboard it works ? Also please show me your ini.

You could try create another mod which would check if ENTER have been pressed and if so show notification.

 

All the best,

Paul

Share this post


Link to post
Share on other sites
stillhere

Hmm, okay. But when you press enter on your keyboard it works ? Also please show me your ini.

 

 

You could try create another mod which would check if ENTER have been pressed and if so show notification.

 

All the best,

Paul

 

Yes, it does work when I press the ENTER key physically. I've also tried it with the numbad plus key ("{ADD}") but it doesn't work. Here is the ini of the Vehicle Jetpack mod in question:

 

[VehicleJetpackV]

; To change settings mid-game just disable and enable the jet after saving the file.
; Version 1.2.2
Story_Mode_Done =1
;------------------------------------------------------------------------------------------------
;Keyboard controls:
KEY_ENABLE = 0x0D ;default: 0x6B | ' + '
key_up = 0x20 ;default: 0x20 | ' Spacebar'
key_down = 0x10 ;default: 0x10 | ' Shift '
key_left = 0x44 ;default: 0x44 | ' A '
key_right = 0x41 ;default: 0x41 | ' D '
key_forwrad = 0x57 ;default: 0x57 | ' W '
key_backwards = 0x53 ;default: 0x53 | ' S '
;------------------------------------------------------------------------------------------------
;Gamepad controls:
KEY_GAMEPAD_ENABLE = 0x0400 ;default: 0x0008 | ' GAMEPAD_DPAD_RIGHT ' | 0x0400 is nothing
key_gamepad_up = 0x1000 ;default: 0x0200 | ' RIGHT_SHOULDER '
key_gamepad_down = 0x4000 ;default: 0x1000 | ' GAMEPAD A ' | 0x4000 is Square
;------------------------------------------------------------------------------------------------
;Advanced Settings:
!!!!DO NOT ALTER FOR THE BEST GAMEPLAY EXPERIENCE!!!!
yaw_sensetivity = 15000 ;default: 15000 | 0 < value < 15000 (the higher the more sensitive).
speed_limit = 300 ;default: 151 km/h | The maximum theoretical speed the vehicle can reach while in the air. (game-top-limit: ~538)
vertical_speed_limit = 100 ;default: 57 km/h | The maximum theoretical vertical speed the vehicle can reach while in the air.
jet_enabled_HUD = 0 ;default: 0 | 1: "Jet Enabled" Will always be displayed in the left upper corner of the screen.
SPACEBAR_ONLY = 1 ;default: 0 | 1: Free falling or leaving the ground will NOT start the jets only pressing the spacebar does.
reverse_switch = 0 ;default: 0 | 1: Switches the direction 'A' and 'D' while the vehicles is in reverse.
mind_control = 0 ;default: 0 | 1: You can mind control your vehicle while you're walking. (you should change WASD, spacebar keys)
;------------------------------------------------------------------------------------------------
;Automatic settings:
New_Install =0
active =0

 

I've also tried your suggestion of using another mod and checking if ENTER is actually pressed, and it works! It just seems like the Vehicle Jetpack mod doesn't seem to work with this. Maybe it is because it is an asi mod and not a .NET mod like the JC2 hook mod (which is working with my script)?
Thanks for the help by the way!
Edited by stillhere

Share this post


Link to post
Share on other sites
leftas

The problem is with Windows' low level keyboard hook that it doesn't see the SendKeys old implementation. So what you can try to do:

1. Try to force your dll to use newer implementation. How to do this is explained in MSDN https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx

2. Use(PInvoke) this https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx

3. Last opinion use library http://inputsimulator.codeplex.com/

 

All the best,

Paul

Share this post


Link to post
Share on other sites
stillhere

The problem is with Windows' low level keyboard hook that it doesn't see the SendKeys old implementation. So what you can try to do:

1. Try to force your dll to use newer implementation. How to do this is explained in MSDN https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx

2. Use(PInvoke) this https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx

3. Last opinion use library http://inputsimulator.codeplex.com/

 

All the best,

Paul

Thanks, I will try the first option.

It seems the second option (PInvoke) is for C++ only? I'm still a newbie so I'm not sure what to do with this.

And I've tried the third option, and I get the error about missing a reference in scripthook even though I have "Using WindowsInput;" in my script. Maybe I have to actually compile the script instead of running the script from a .cs? Or do I need to place the WindowsInput DLL in the GTA 5 folder? Sorry for the many questions!

 

Edit:

Okay the first option didn't seem to do the trick.

I tried the third option again and the script loads (had to put InputSimulator.dll in the scripts folder), but the Vehicle Jetpack mod still doesn't activate using my script!

 

I'm using InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);

Is there any way to make it specifically press the hexadecimal value of the ENTER/RETURN key (0x0D)?

Edited by stillhere

Share this post


Link to post
Share on other sites
leftas

 

The problem is with Windows' low level keyboard hook that it doesn't see the SendKeys old implementation. So what you can try to do:

1. Try to force your dll to use newer implementation. How to do this is explained in MSDN https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx

2. Use(PInvoke) this https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx

3. Last opinion use library http://inputsimulator.codeplex.com/

 

All the best,

Paul

Thanks, I will try the first option.

It seems the second option (PInvoke) is for C++ only? I'm still a newbie so I'm not sure what to do with this.

And I've tried the third option, and I get the error about missing a reference in scripthook even though I have "Using WindowsInput;" in my script. Maybe I have to actually compile the script instead of running the script from a .cs? Or do I need to place the WindowsInput DLL in the GTA 5 folder? Sorry for the many questions!

 

Edit:

Okay the first option didn't seem to do the trick.

I tried the third option again and the script loads (had to put InputSimulator.dll in the scripts folder), but the Vehicle Jetpack mod still doesn't activate using my script!

 

I'm using InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);

Is there any way to make it specifically press the hexadecimal value of the ENTER/RETURN key (0x0D)?

 

That's very strange... Anyway sadly, I can't help you :(

PInvoke is the ability C# to use native(c++) functions.

For your another problem: just do the casting -

InputSimulator.SimulateKeyPress((VirtualKeyCode)0x0D);

P.S. If I will find solution to your problem, I will post in this thread.

 

All the best,

Paul

Share this post


Link to post
Share on other sites
stillhere

That's very strange... Anyway sadly, I can't help you :(

 

 

 

 

PInvoke is the ability C# to use native(c++) functions.

For your another problem: just do the casting -

InputSimulator.SimulateKeyPress((VirtualKeyCode)0x0D);

P.S. If I will find solution to your problem, I will post in this thread.

 

All the best,

Paul

 

Bro, I finally got it to work!! :D All I had to do was make the script Wait for the input after a KeyDown statement before doing KeyUp! I switched to using the Input Manager wrapper instead of Windows Input Simulator because it uses System.Windows.Forms.Keys instead of of VirtualKeyCode, but it also works with Input Simulator if you add a Wait. Thanks a bunch for the help man, feels great to finally getting it to work! Here's my current script:

using GTA; // This is a reference that is needed! do not edit thisusing GTA.Math;using GTA.Native; // This is a reference that is needed! do not edit thisusing System; // This is a reference that is needed! do not edit thisusing System.Windows.Forms; // This is a reference that is needed! do not edit thisusing System.IO;//using WindowsInput; //For Input Simulatorusing InputManager; //For Input Managernamespace SimulateKeys{    public class SimulateKeys : Script // declare Modname as a script    {        int VehicleJetpackTimer;        int VehicleJetpackCounter = 0;        public SimulateKeys() // main function        {            Tick += OnTick;            KeyDown += OnKeyDown;            KeyUp += OnKeyUp;        }        void OnTick(object sender, EventArgs e) // This is where most of your script goes        {            Ped player = Game.Player.Character;            if (Game.IsControlPressed(2, GTA.Control.Aim) && Game.IsControlPressed(2, GTA.Control.VehicleHorn)) //Turn on JC3Hook mod with Aim key + LS            {                JC3HookToggle();            }            if (player.IsInVehicle())            {                VehicleJetpackTimer++;                if (Game.IsControlJustPressed(2, GTA.Control.Sprint))                {                    VehicleJetpackTimer = 0;                    if (VehicleJetpackTimer < 25)                    {                        VehicleJetpackCounter++;                    }                    if (VehicleJetpackCounter == 2)                    {                        PressENTER(); //Turn on Vehicle Jetpack mod by double-tapping A                        VehicleJetpackCounter = 0;                    }                }                if (VehicleJetpackTimer == 25)                {                    VehicleJetpackCounter = 0;                }                //UI.ShowSubtitle(VehicleJetpackCounter.ToString());            }        }        //http://www.codeproject.com/Articles/117657/InputManager-library-Track-user-input-and-simulate        public void PressENTER()        {            Keyboard.KeyDown(Keys.Enter);            Wait(100);            Keyboard.KeyUp(Keys.Enter);            Wait(300);        }        public void JC3HookToggle()        {            Keyboard.KeyDown(Keys.ControlKey);            Wait(100);            Keyboard.KeyDown(Keys.H);            Wait(100);            Keyboard.KeyUp(Keys.H);            Wait(100);            Keyboard.KeyUp(Keys.ControlKey);            Wait(300);        }    }}

Share this post


Link to post
Share on other sites
Johan111

Hi Stillhere,

 

after adding the InputManager.dll to the project references and adding it to the code (using InputManager), the script fails to execute - strangely just by adding the line "unsing InputManager".
Could you give some additional information on how you managend to get this working?!

 

Thank you

 

Share this post


Link to post
Share on other sites
Tanjitsu
using GTA;
using System;
using System.Runtime.InteropServices;

namespace SimulateKeys
{
    public class SimulateKeys : Script
    {
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

        Ped player = Game.Player.Character;

        public SimulateKeys()
        {
            Tick += OnTick;
        }

        void OnTick(object sender, EventArgs e)
        {
            if (player.IsInVehicle())
            {
                if (Game.IsControlJustPressed(2, GTA.Control.Sprint))
                {

                    keybd_event(0x0D, 0, 0, 0);     // 0x0D = Enter  / KeyDown
                    Wait(100);
                    keybd_event(0x0D, 0, 0x0002, 0);  // 0x0002  / KeyUp
                }
            }
        }
    }
}

This will work.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.