Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Minecraft script that holds down keys/mouse buttons



  • Please log in to reply
6 replies to this topic
CSharpp
  • Members
  • 4 posts
  • Last active: Dec 23 2014 12:21 PM
  • Joined: 19 Dec 2014

Hey all. I started a Minecraft script today to do 3 common things that hurt my fingers:

  • Hold down left click continuously
  • Double tap W, and during the second tap, keep holding it continuously
  • Rapid spam left click

Thanks to some googling, I got the first case working. I am stuck on the second and third case though. Can you guys take a look and suggest code improvements?

; Mine - Holds down left click until F8 is pressed again
F8::
	Send % "{Click " . ( GetKeyState("LButton") ? "Up}" : "Down}" )
return

; Sprint - Double tap W, not releasing the W key, until F9 is pressed again
; TODO: this script works for walking (single tap) but not running (double tap). maybe the timing is off?
F9::
	send w
	send {w down}
return

; Attack - Spams left click rapidly until F12 is pressed again
; TODO: get this to work
F12::
	
return

In fact, I don't even like that first code too much. I'd rather expand it out and make it more readable so that I know how it works and I can eventually become fluent in this language.

 

What is the best way to do a toggle key? Should I set a variable to track if F8, F9, and F12 are on their first or second press, then have a conditional? Should I use a getState function like this guy did with his F8 code? If you guys could demonstrate your style for coding toggle keys that'd be awesome.

 

Thanks in advance for your help.



Dilof
  • Members
  • 74 posts
  • Last active: Jun 28 2015 11:52 AM
  • Joined: 24 Jul 2014
✓  Best Answer
; Mine - Holds down left click until F8 is pressed again
*f8::
    toggle := !toggle
	if (toggle) {
	SendInput {LButton Down}
	}
	else {
	SendInput {LButton Up}
	}
return 

; Sprint - Double tap W, not releasing the W key, until F9 is pressed again
*f9::
    toggle2 := !toggle2
	if (toggle2) {
	SendInput {w Down}
	sleep, 45
	SendInput {w Up}
	Sleep, 45
	SendInput {w Down}
	}
	else {
	SendInput {w Up}
	}
return 

; Attack - Spams left click rapidly until F12 is pressed again
*f12:: 
    SetTimer Rapid_Click, 10
    toggle3 := !toggle3

Rapid_Click:
    if (!toggle3)
        return
    SendInput {LButton Down}
    sleep, 5
    SendInput {LButton Up}
return


CSharpp
  • Members
  • 4 posts
  • Last active: Dec 23 2014 12:21 PM
  • Joined: 19 Dec 2014

Hi Dilof,

 

Thanks for your reply! Your code is styled very nicely. I will try it out in Minecraft the next time I play and let you know if everything works. My guess is it will since the code looks so nice. Hahaha.



Dilof
  • Members
  • 74 posts
  • Last active: Jun 28 2015 11:52 AM
  • Joined: 24 Jul 2014

Hey there, I've already tested it on the latest version of AHK and Minecraft 1.7.2 and it seems to do the job. ;)

By the way, if you are playing Minecraft 1.8+ you can use a much faster and more reliable code for sprinting.

; Sprint - Holds LCtrl and W, releases when F9 is pressed again
; Note - Works only for Minecraft version 1.8+

*f9::
    toggle2 := !toggle2
	if (toggle2) {
	SendInput {LControl Down}
	SendInput {w Down}

	}
	else {
	SendInput {LControl Up}
	SendInput {w Up}
	}
return 


CSharpp
  • Members
  • 4 posts
  • Last active: Dec 23 2014 12:21 PM
  • Joined: 19 Dec 2014

I tested it and everything works like a charm. Thank you!!

 

It's strange that AutoHotKey doesn't allow functions with the SetTimer command. Labels/goto's are not very OOP and make the code less readable. A function would be much better I think.



CSharpp
  • Members
  • 4 posts
  • Last active: Dec 23 2014 12:21 PM
  • Joined: 19 Dec 2014

I looked up how to turn a timer off. That might be more efficient than having it run Rapid_Click every 10 milliseconds even when the toggle is set to off. Do you think this code would work?

; Attack - Spams left click rapidly until F12 is pressed again
*f12:: 
	toggle3 := !toggle3
	if (toggle3) {
		SetTimer Rapid_Click, 10
	} else {
		SetTimer Rapid_Click, Off
	}
return
	
Rapid_Click:
	SendInput {LButton Down}
	sleep, 5
	SendInput {LButton Up}
return


Dilof
  • Members
  • 74 posts
  • Last active: Jun 28 2015 11:52 AM
  • Joined: 24 Jul 2014

Your code will work just the same as example I provided, with less resources used. Up to you which you prefer and is more readable. ;)