AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Game macro
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Ghazkhull



Joined: 17 Apr 2010
Posts: 4

PostPosted: Sat Apr 17, 2010 11:03 am    Post subject: Game macro Reply with quote

Hello all. I heard about AutoHotkey and picked up this script on a message board I frequent. I read the FAQ and the Tutorial, but I don't know ANYTHING about coding, so I didn't understand anything I was reading.

I have to bind a couple of things to a key in a game I play and I figured this would really make my life a lot easier. As it is now, I have to try to remember to push the key every 5 seconds... all while doing everything else in the game. Needless to say, it's a big pain in the butt, not to mention my wrist.

So, here's the deal:

I was told to bind my functions to the F6 key and use AutoHotkey to loop the keypresses for me. This script he wrote is supposed to turn on the script, press F6 repeatedly until you hit F5 once or twice to stop it running, Here's the script:

Code:
F4 UP::
Send, {F6 DOWN}{F6 UP}
Sleep, 1000
Loop
{
    Sleep, 200
    GetKeyState, state, F5, P
    if state = D  ; The key has been pressed, so break out of the loop.
        break
 
    Send, {F6 DOWN}{F6 UP}
}


So, I bound the functions needed to a hotbar key, then bound that key to activate on pressing F6. Before I start my game, I double-click the .ahk file I made on my desktop and then hit F4 to start the script. When I log in to the game, F6 should be being pressed every couple of seconds by the script. What is happening though, is nothing. If I hit F6 manually, it activates just as it is supposed to, but will not register a press from AutoHotkey.

Also, I play the game in windowed mode, fullscreen so, when I click on my second monitor (web browser) and then click back into the game, it activates. I didn't hit F6 this time, though, AHK did and the game recognizes it. I tried running the game in fullscreen mode and it didn't work either.

Any ideas on why this may be? Could it be something within Vista 64? Could there be an error in this code?



Ghazkhull

[Title edited. Please write descriptive titles for your topics. ~jaco0646]
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Sun Apr 18, 2010 9:13 am    Post subject: Reply with quote

It's hard to know how to help without knowing which game you are referring to (I don't play games much, so knowing might not help much, either!) One thing that it might be could be there is no delay between the F6 down and up events. I think that games need to register those events separately. This code has a 100ms delay between the down and up F6 events. Also, there is only one key needed to start and stop the the auto send, F5 (this won't make it work better, just simpler for you.) HTH. If not, maybe search the forum for that game to see if anyone else has had the same issue.

Code:
SetKeyDelay, 100   ;100ms delay between down and up events
Return
looper:
   Send, {F6 DOWN}   ;separate these so there can be a delay between them.  EDIT: Doesn't matter- they can be on the same line. Oops.
   Send, {F6 UP}
Return

F5::
   If (!loopit)
   {   SetTimer, looper, 1000   ;run after 1 sec, every sec
      Gosub, looper   ;run immediately, too
   }
   Else
   {   SetTimer, looper, Off
   }
   loopit := !loopit   ;toggle
Return

_________________


Last edited by Micahs on Sun Apr 18, 2010 9:54 am; edited 1 time in total
Back to top
View user's profile Send private message
Brad
Guest





PostPosted: Sun Apr 18, 2010 9:50 am    Post subject: Reply with quote

You mentioned that you log into the game and then F6 should function.

Try using the Window spy to capture some window elements, so that you can use "WinActivate" to bring the game forward so that F6 will work just for that program window.

Also why not just use: SendInput {F6} instead of Send {F6 Down}{F6 Up}
I'd also remove the comma after the Send command if you are goinf to use it as I have had issues with that in the past.
Back to top
Brad
Guest





PostPosted: Sun Apr 18, 2010 9:53 am    Post subject: Reply with quote

Also to pause the script you could simply use Pause by adding

Pause::Pause

...instead of using F5

or use

F5::
Pause
Back to top
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Sun Apr 18, 2010 10:13 am    Post subject: Reply with quote

Using just "{F6}" would work, but then you would have to change SetKeyDelay to "SetKeyDelay, 100, 75" or something. The default press duration is only 10ms and might not be long enough. (That might be needed anyway. Mine does not address that, either!)

Just using "Pause" would make for a clunky script if you have multiple hotkeys. If only one, it might be ok (but still clunky.) Either way, it might stop the send process in the middle of the sequence if it is more complicated. The user might want the final send sequence to complete instead of pausing and then resuming later halfway through. (This one is just F6, but others might have more keystroke combinations to send.)

Whatever works!
_________________
Back to top
View user's profile Send private message
JDN



Joined: 24 Mar 2004
Posts: 299

PostPosted: Sun Apr 18, 2010 11:09 am    Post subject: Reply with quote

I would like to try and help.

But it always makes me nervous when someone starts a thread by posting a request for help and then doesn't acknowledge any of the replies for more than 24 hours and it's been 24 hours now since our new friend first posted their request.

However, I will try to keep an eye on this thread and hope that our new friend hasn't committed one of those Post and Run offences! Cuz Dango! That just ain't very nice!

Usually, I wouldn't get involved in this because I wouldn't want to give Ghazkhull the impression that I am slagging him. But I feel that I really just have to admire anyone who admits they know nothing about some subject and so they couldn't understand anything they were reading but went ahead and read it anyway.

I was shocked! I find there is just something admirable about anyone who would do that - and even more admirable that they would then admit to it.

As a result, I would very much like to give him some help. So, here is just a thought that may help:

I don't see you have specified the RETURN statement anywere.

So when the F4 key lifts up, only the first line in your code will run. That is this one: SEND {F6 DOWN}{F6 UP}.

In order to get the Sleep and Loop statements included, you need to code a RETURN statement after the final RSB (Right Square Bracket).

Somehow, I doubt that is the problem becaue you probably did specify a RETURN statement somewhere. But, in case you didn't, this info may help you. Try to specify a RETURN statement at the bottom of your code.

Actually, I'm not sure when the other code will run.

It may actually run when you first load the script - if it is considered part of the Auto-Execute section. I'm not really very experienced with that.

You may want to look up the "Auto Execute" section in the Help system and see if you can figure out when it will run if there is no RETURN stmt.

The "Auto Execute" section of the Help file is located in the "Scripts" section at the top - just under the Table of Contents and it's called "The Top of the Script (the Auto-Execute Section):" this portion executes automatically when the script starts.

Good Luck!
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Sun Apr 18, 2010 8:58 pm    Post subject: Reply with quote

Good eye! My code addresses this, but I failed to mention it. Thanks!
_________________
Back to top
View user's profile Send private message
Ghazkhull



Joined: 17 Apr 2010
Posts: 4

PostPosted: Wed Apr 21, 2010 2:59 pm    Post subject: Reply with quote

JDN wrote:
hasn't committed one of those Post and Run offences!

Nope. Still here. It's been a very hectic week for me. In fact, I haven't even been back to the forums since the first reply. Which, by the way, doesn't work either.

It's the same problem. I see the script running while out of the game, but nothing happens when I have the game as the main window. I use a dual monitor setup, so I play in windowed mode and I have web browser, MP3 player, etc. on my secondary monitor. If I click on the secondary monitor and activate a web browser (or whatever) for a second or two, when I click back in the game, it performs the task assigned the F6 key.

So, if I hit F6 manually, or I click out of the game every couple seconds and back it, I can get it to work. However, that counter-productive to playing the game. That's playing monitor-hopping.

So, we know that both scripts work, as in they both repeatedly push F6. Problem is, neither will do it while the game is the active window.

Thanks so much for your responses so far people!



Ghazkhull
Back to top
View user's profile Send private message
argneo



Joined: 14 Sep 2007
Posts: 220

PostPosted: Wed Apr 21, 2010 3:08 pm    Post subject: Reply with quote

Have you tried Brad's comment? Sending keys to a game is not the same as sending regular keys to any other window based program...
_________________
Search&Replace

Don't use cannon to kill mosquito
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Wed Apr 21, 2010 6:15 pm    Post subject: Reply with quote

Agreed.

Quick question. In your first post, you said
Quote:
Also, I play the game in windowed mode, fullscreen so...

Do you play the game in windowed mode or fullscreen? The two are mutually exclusive. Fullscreen mode is known to have some issues receiving sent keys sometimes (most times?) and could be the cause of the problems here. You could search the forum for "fullscreen games" (or your game) and see if there is anything that could help. Brad's comment on using sendinput and other forms of send might indeed help.

Also, if you are playing fullscreen, have you tried playing in windowed mode maximized, instead of fullscreen, to see if it works? (Or when you say fullscreen, do you mean maximized?)
_________________
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Wed Apr 21, 2010 6:34 pm    Post subject: Reply with quote

Also, if clicking off and then back works, maybe add winactivate as Brad again suggested.

Code:
SetKeyDelay, 100   ;100ms delay between down and up events
Return
looper:
   SetTimer, looper, Off   ;so we don't have several identical threads if there is a long wait (like the game not running yet...)
   Winactivate, <game name>
   WinWaitActive, <game name>
   Send, {F6}
   If(loopit)   ;only turn back on if enabled
   {   SetTimer, looper, On
   }
Return

F5::
   If (!loopit)
   {   loopit=1
      SetTimer, looper, 1000   ;run after 1 sec, every sec
      Gosub, looper   ;run immediately, too
   }
   Else
   {   loopit=0
      SetTimer, looper, Off
   }
Return

_________________
Back to top
View user's profile Send private message
Ghazkhull



Joined: 17 Apr 2010
Posts: 4

PostPosted: Wed Apr 21, 2010 8:18 pm    Post subject: Reply with quote

argneo wrote:
Have you tried Brad's comment?

Well, I read both, but they make no sense to me. I was hoping that a different fix might come along and I wouldn't have to come across as a COMPLETE noob waste of time...

In the first he says something about using the "Window spy to capture some window elements" and something about "WinActivate". I was assuming that those are programs I will have to install and run, etc. I would like to avoid doing that except as an all-else-fails scenario.

In both of his posts, he talks about code. As soon as I see code talk that I can copy/paste, I move on. I have NO CLUE about this stuff. I was kindof assuming he was making code suggestions to the guy before him that posted a code suggestion for me to try.


Micahs wrote:
(Or when you say fullscreen, do you mean maximized?)

Exactly, sorry. I play windowed mode, maximized. I have, however, tried it in fullscreen mode as well. Neither worked.

Michas, I'm going to try your code right now.


***EDIT***
Michas, I pasted your code above over the code from your first response. No dice. There was no evidence that it was even running at all. The first code you posted at least showed activity when I pulled up my taskbar from auto-hide.



Ghazkhull


Last edited by Ghazkhull on Wed Apr 21, 2010 9:11 pm; edited 1 time in total
Back to top
View user's profile Send private message
argneo



Joined: 14 Sep 2007
Posts: 220

PostPosted: Wed Apr 21, 2010 8:29 pm    Post subject: Reply with quote

What I meant was that Send is maybe not the command for you...
maybe "ControlSend"?
_________________
Search&Replace

Don't use cannon to kill mosquito
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Thu Apr 22, 2010 2:46 am    Post subject: Reply with quote

Well actually, you have to know SOMETHING about this stuff to make it work. Just cutting and pasting will not always work.

The last bit of code I posted is not complete. I'm sorry that I did not explain exactly how to use it. You have to substitute the window title for the text "<game name>" in two places.

The lines with "WinActivate" and "WinWaitActive" are structured like this (in this case):
Code:
WinActivate, "WindowTitle"   ;don't include the quotes!

If the window title of the game is "Really Cool Game" then that is what you put instead.
Code:
WinActivate, Really Cool Game


You can remove the lines, but then it won't be activating the game each time.

Also, instead of just "Send", maybe try "SendInput" or "SendPlay" to see if there is any improvement (those are direct replacements for "Send.") "ControlSend" might also work, as argneo suggested (it does not require that the window be the active window.) In your case,
Code:
ControlSend, , {F6}, Really Cool Game
would be the code to use. This might not work, though.

HTH.
_________________
Back to top
View user's profile Send private message
Ghazkhull



Joined: 17 Apr 2010
Posts: 4

PostPosted: Thu Apr 22, 2010 8:20 pm    Post subject: Reply with quote

Ok, well, maybe I overplayed my ignorance a little bit. I can catch on to things ok. I mean, when I saw <game name>, I knew I was supposed to overwrite that with the name of the game. I figured out that the green text were probably comments and not actual code.

What I mean about knowing nothing is the actual code. For example, I don't know what "ControlSend" means. Also, I'm not sure about "{F6 DOWN}{F6 UP}". If I were guessing, I would probably say that relates to the physical state of the key, pressed (down) or released (up).

So, for the game name, I tried it and it still didn't work. There's a snag though. I'm not sure what game name it's looking for. There's the title of the game on the box and CD, etc, there's the name of the launcher shortcut, but there's also the name of the .exe process running. Also, when looking at the taskbar for the tab of the game, it has another title, different than the rest...

Maybe it's just time to throw in the towel. This is frustrating and I'm sure you guys have better things to do. I really do appreciate all the help you've given so far. Very kind of you.



***EDIT***
Well, it seems that the WinActivate <game name> bit works. Plus, I guess I got the name of the game right. When the game and script are running, if I hit the windows key or click on my other monitor, it makes the game window the active one again. Still not registering the F6 keypresses though.



Ghazkhull
_________________
"The two most abundant things in the universe are hydrogen and stupidity." -Harlan Ellison
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group