Jump to content

Send down not working how I want it to


 Share

Recommended Posts

I'm making a script that will act as a controller for Half-Life 2 (Garry's Mod actually, but it's an hl2 mod). I have working code with no errors, but Send("{w down}") doesn't actually send w down, it just constantly sends w until Send("{w up"}) is present. Normally this wouldn't be a problem, but in my case, my character walks/runs a lot slower because of this. Does anyone know how to avoid this? My entire code (Uses Adam123's modified version of Ejoc's Joystick UDF that works with AutoIt 3.2):

#include <Joystick.au3>

$JoyInit = _JoyInit()

While 1
    $Joy = _GetJoy($JoyInit, 0)
    Select
        Case $Joy[1] = 0
            Do
                Send("{w down}")
            Until $Joy[0] <> 0
            Send("{w up}")
        Case $Joy[0] = 0
            Do
                Send("{a down}")
            Until $Joy[1] <> 0
            Send("{a up}")
        Case $Joy[1] = 65535
            Do
                Send("{s down}")
            Until $Joy[1] <> 0
            Send("{s up}")
        Case $Joy[0] = 65535
            Do
                Send("{d down}")
            Until $Joy[0] <> 0
            Send("{d up}")
    EndSelect
WEnd

Ejoc's UDF is found here:

http://www.autoitscript.com/forum/index.ph...hl=joystick+udf

Adam123's modified version is in the 4th post.

EDIT: I'm using a DualShock 2 controller attached by a DualShock 2 to USB adapter.

Edited by Minikori

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

Bump.

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

try making send() command before do command

I have tried that. I changed lines 8 through 13 to this:

Case $Joy[1] = 0
    Send("{w down}")
    Do
        Sleep(1)
    Until $Joy[0] <> 0
    Send("{w up}")

And I also changed all the other letters of it.

Edited by Minikori

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

Correct me if im wrong, but don't you have to update the value of $joy in your Do loops?

Cheers,

Brett

I think so, but I don't know why that would be affecting it. It still sends w, just rather than holding down w, it constantly presses it until I say Send("{w up}").

I'll give it a try, one sec.

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

@BrettF: Your idea did vastly improve my character's speed, but I can tell it's still constantly sending w, not holding it down. So I haven't gotten the answer I wanted yet. But thanks.

EDIT: Sorry, I got that wrong. Your's did second best. The second piece of code I showed worked the best. Sorry about the possible confusion.

Edited by Minikori

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

I think so, but I don't know why that would be affecting it. It still sends w, just rather than holding down w, it constantly presses it until I say Send("{w up}").

I'll give it a try, one sec.

If a key is held down it auto repeats, so how do you know it's repeatedly sending 'w' rather than holding the key down?
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

If a key is held down it auto repeats, so how do you know it's repeatedly sending 'w' rather than holding the key down?

Read my first post. It says that the character is moving slower (and shaking, both of which are signs of constant pressing).

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

Ooo. Garry's Mod <3

Yes, Thats all I came here to say.

Okay, this forum is General Help and Support, not Chat. I'm fine with that after a question has been answered, but when I'm still expecting one, that doesn't help.

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

Okay, this forum is General Help and Support, not Chat. I'm fine with that after a question has been answered, but when I'm still expecting one, that doesn't help.

Then it's a good thing your desires are what govern responses to a thread on a public forum. :)

Try doing a sleep(2000) after the w key down.

What I think is occurring is relatively simple: Every time the program loops, it's sending w key down again. Meaning that even though it's already pressed, the input is received again and again every few ms, so it will get jerky.

If I'm right, the Sleep(2000) will give you smooth forward motion for 2 seconds, while excluding other input.

Assuming that's the case, you need to separate your joystick logic from your input emulation. Determine if the joystick direction is being activated, and only do a single key down and key up for each joystick direction state. The state remains constant during the While loops, instead of constantly reacquiring and setting the emulation state, providing smooth input.

If  $Joy[1] = 0 And $wActive = false Then 
    Send("{w down}")
    $wActive = True
EndIf

If $Joy[1] <> 0 And $wActive = True Then
    Send("{w up}")
    $wActive = False
EndIf

Replace your Case|Select statements with If/Then, testing against wasd Active variables as well as input, so that you aren't repeatedly firing the same event while joystick input is unchanging.

Don't be snippy when you're asking for help, it strikes me as incredibly rude. Take what you get, even if it's just a random comment. It keeps your thread active and in view at the very least.

Edited by JRowe
Link to comment
Share on other sites

Then it's a good thing your desires are what govern responses to a thread on a public forum. :)

Try doing a sleep(2000) after the w key down.

What I think is occurring is relatively simple: Every time the program loops, it's sending w key down again. Meaning that even though it's already pressed, the input is received again and again every few ms, so it will get jerky.

If I'm right, the Sleep(2000) will give you smooth forward motion for 2 seconds, while excluding other input.

Assuming that's the case, you need to separate your joystick logic from your input emulation. Determine if the joystick direction is being activated, and only do a single key down and key up for each joystick direction state. The state remains constant during the While loops, instead of constantly reacquiring and setting the emulation state, providing smooth input.

If  $Joy[1] = 0 And $wActive = false Then 
    Send("{w down}")
    $wActive = True
EndIf

If $Joy[1] <> 0 And $wActive = True Then
    Send("{w up}")
    $wActive = False
EndIf

Replace your Case|Select statements with If/Then, testing against wasd Active variables as well as input, so that you aren't repeatedly firing the same event while joystick input is unchanging.

Don't be snippy when you're asking for help, it strikes me as incredibly rude. Take what you get, even if it's just a random comment. It keeps your thread active and in view at the very least.

Thank you, I will give that a shot when I can, but the Do...Until should make it so I don't need the Sleep().

@AzKay: Sorry for being rude like that, I was just getting frustrated.

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

Well, I didnt read past Garrys Mod.

Anyway, If you were just looking for a basic program in general, Like.

If you were looking for a program, and decided to use autoit because you couldnt find one;

For my PSP as a joystick, I used; http://www.mypsp.com.au/GuideDetail.aspx?id=248

But if your doing it AutoIt because you want to; Then I guess I could look over the code and try it myself.

# MY LOVE FOR YOU... IS LIKE A TRUCK- #
Link to comment
Share on other sites

Disregard what I said before regarding the Sleep(), etc.

Just replace the Select/Case statements with the code I provided. Sorry for the confusion :)

Link to comment
Share on other sites

Disregard what I said before regarding the Sleep(), etc.

Just replace the Select/Case statements with the code I provided. Sorry for the confusion :)

Okay I'll try that. But where did you come up with this $wActive? I'm assuming it's WindowActive.

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

I apologize. I assumed by your post count that you were more familiar with AutoIt. I'll explain more thoroughly. :)

You have to create a variable that keeps track of whether or not you have activated a particular joystick direction. so $wActive = true means that the "w key down" state is active. $wActive = false means that the termination event has been fired (or, $joy[0] <> 0)

w = up, a = left, s = down, and d = right. Each direction can be either on (pressed) or off (not pressed).

You'll need a $wActive, $aActive, $sActive, and $dActive to keep track of your active states. So, create the variables in the beginning of your script.

;We initialize the variables to false, or off, to signal the emulator that they aren't pressed by default.
   $upActivated = false 
   $leftActivated = false 
   $downActivated = false 
   $rightActivated = false

Now that we have a way of keeping track of the pressed keys, we can find out what the joystick is doing.

According to the UDF we want the X position. This can be anywhere from 0 to 35999, or 0 to 359.99 degrees. 0/36000 is up, or w, but you obviously don't want to have to push the joystick precisely up. We'll give it a tolerance of 120 degrees (60 degrees to the sides of the centerpoint of the direction in question.)

Posted Image

Let's initialize the joystick:

$Joy = _GetJoy($JoyInit, 0)
   $JoyStickInput = $Joy[0]

Our conditions for up are : Greater Than 29250, or Greater than 0 AND Less Than 6750. If either of those conditions are met, we want to set $upActivated to true. If those conditions are not met, then we want $upActivatedto be false. If those conditions are met, and $upActivated is true, it means that the joystick has been pressing up, and you don't want to resend the "w key down" event, or you get herky movement. If it is true, but the conditions are not met, that means you've just moved out of the Up direction, and you want to turn off the "w key down" event, so you set $upActivated to false and send the "w key up" event.

If $JoyStickInput > 0 And < 6750 Or If $JoyStickInput > 29250 Then
       If $upActivated = False
           Send("{w down}")
           $upActivated = True
       EndIf
   EndIf
   
   If $upActivated = True And $JoyStickInput < 29250 Then
       If $JoyStickInput > 6750
           $upActivated = False
           Send("{w up}")
       EndIf
   EndIf

Easy enough. That keeps track of whether Up is being signalled by the joystick, with a range of 135 degrees across the top of your joystick... e.g. if your joystick is pointed in the Green direction, the up event is happening (in this case, the w key down.) Now you modify that to include the other three directions, testing against the conditions of both the state tracking variable and the direction of the controller.

Right Conditions = between 2250 and 15750

Down Conditions = between 11250 and 24750

Left Conditions = between 20250 and 33750

So, after doing that, you're left with a script that reads four directions, which can combine into 8 discrete 45 degree segments. Up, Up/Right, Right, Right/Down, Down, Down/Left, Left, and Left/Up.

I might be using the UDF wrong, as I don't have a joystick, but this should be a full solution to your need. :lmao:

CODE
#include <Joystick.au3>

$Joy = _GetJoy($JoyInit, 0)

$JoyStickInput = $Joy[0]

$upActivated = false

$leftActivated = false

$downActivated = false

$rightActivated = false

While 1

;;;;;;;;;;;;;;;;;;;Testing for UP direction and event.

If $JoyStickInput > 0 And $JoyStickInput < 6750 Or $JoyStickInput > 29250 Then

If $upActivated = False Then

Send("{w down}")

$upActivated = True

EndIf

EndIf

If $upActivated = True And $JoyStickInput < 29250 Then

If $JoyStickInput > 6750 Then

$upActivated = False

Send("{w up}")

EndIf

EndIf

;;;;;;;;;;;;;;;;;;;Testing for RIGHT direction and event.

If $JoyStickInput > 2250 And $JoyStickInput < 15750 Then

If $rightActivated = False Then

Send("{d down}")

$rightActivated = True

EndIf

EndIf

If $rightActivated = True And $JoyStickInput > 15750 Or $JoyStickInput < 2250 Then

$rightActivated = False

Send("{d up}")

EndIf

;;;;;;;;;;;;;;;;;;;Testing for DOWN direction and event.

If $JoyStickInput > 11250 And $JoyStickInput < 24750 Then

If $downActivated = False Then

Send("{s down}")

$downActivated = True

EndIf

EndIf

If $downActivated = True And $JoyStickInput > 24750 Or $JoyStickInput < 11250 Then

$downActivated = False

Send("{s up}")

EndIf

;;;;;;;;;;;;;;;;;;;Testing for LEFT direction and event.

If $JoyStickInput > 20250 And $JoyStickInput < 33750 Then

If $leftActivated = False Then

Send("{a down}")

$leftActivated = True

EndIf

EndIf

If $leftActivated = True And $JoyStickInput > 24750 Or $JoyStickInput < 33750 Then

$leftActivated = False

Send("{a up}")

EndIf

;Throw in a Sleep to keep CPU usage minimal.

Sleep(10)

WEnd

There may be some errors with the conditionals, but it should work. Let me know :think:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...