Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #cs
- This is my function for handling stuck ctrl, alt, win, & shift keys from a farmtown
- bot program I am working on (Thanks Timehorse and Jackalo for the farmville bot that I
- started from). Anyone familier with that script will certainly recognize the logic
- that I "borrowed" from Timehorse :)
- in particular - I was running into big problems when I implemented a chat routine and
- used ctrl+key hotkeys to access it. Frequently the ctrl key would get stuck. I found
- that problem also existed for alt+key sequences... didn't really test the shift/win
- sequences, but through those keys in too.
- #ce
- #include <Timers.au3>
- #Include <Array.au3>
- ; Keyboard cleanup handler
- Global $user32dll = DllOpen("user32.dll") ; should be cleaned up at exit
- Global $key_down_too_long = 1000 ; if key held down over a second reset it
- ; Global Array for timer functions corresponding to keys defined below
- Global $key_timer[8] = [0, 0, 0, 0, 0, 0, 0, 0]
- ; Keys of interest are hotkey modifiers for ctrl, alt, win, and shift
- Global Const $keys[8] = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x5b, 0x5c]
- ;0xa0 LSHIFT
- ;0xa1 RSHIFT
- ;0xa2 LCTRL
- ;0xa3 RCTRL
- ;0xa4 LALT
- ;0xa5 RALT
- ;0x5b LWIN
- ;0x5c RWIN
- Global $in_chat = False
- ; Hot Keys
- Global $msg1_key1 = "^{UP}"
- Global $msg2_key1 = "^{LEFT}"
- Global $msg3_key1 = "^{RIGHT}"
- Func unstick_keys($force_unstick=False)
- Local $i
- ;Format of DllCall to press/release a key
- ;DllCall($dll,"int","keybd_event","int",$vkvalue,"int",0,"long",0,"long",0) ;To press a key
- ;DllCall($user32dll,"int","keybd_event","int",$vkvalue,"int",0,"long",2,"long",0) ;To release a key
- If $force_unstick Then
- For $vkvalue in $keys
- DllCall($user32dll,"int","keybd_event","int",$vkvalue,"int",0,"long",2,"long",0) ;Release each key
- Next
- Else
- $i = 0
- For $vkvalue in $keys
- If _IsPressed($vkvalue) Then
- If $key_timer[$i] = 0 Then
- $key_timer[$i] = _Timer_Init() ; initialize a timer to watch this key
- ElseIf TimerDiff($key_timer[$i]) >= $key_down_too_long Then ; check elapsed time
- DllCall($user32dll,"int","keybd_event","int",$vkvalue,"int",0,"long",2,"long",0) ; release the key
- $key_timer[$i] = 0 ; reset the timer
- EndIf
- EndIf
- $i = $i + 1
- Next
- EndIf
- EndFunc
- Func do_exit() ; call for common exit point
- DllClose($user32dll)
- unstick_keys(True)
- Exit
- EndFunc
- ; previously troublesome function here - can't use ControlSend because the
- ; input field is part of a flash game app
- Func chat($text)
- $in_chat = True ; want to keep from re-entering function before it is finished
- Local $mymsg = $text
- Local $sav_pos = MouseGetPos() ; remember mouse position
- Local $i
- MouseClick("primary", $chatx, $chaty, 1, 4) ; click input area
- For $i = 1 to Mod($msgdotcount, 2) ; flash (or Farmtown) wants different message each time
- $mymsg = $mymsg & "."
- Next
- Send($mymsg, 1)
- Sleep(100)
- Send("{ENTER}", 0)
- MouseMove($sav_pos[0], $sav_pos[1], 1) ; restore mouse
- $msgdotcount = $msgdotcount + 1
- unstick_keys()
- $in_chat = False
- EndFunc
- ; put calls to unstick_keys at bottom of every hot-key handler function
- ; also place calls wherever you think they might be handy in your code
- Func clear_hotkeys()
- If $msg1_key1 Then HotKeySet($msg1_key1)
- If $msg2_key1 Then HotKeySet($msg2_key1)
- If $msg3_key1 Then HotKeySet($msg3_key1)
- EndFunc
- Func activate_hotkeys()
- If $msg1_key1 Then HotKeySet($msg1_key1, "on_msg1_1")
- If $msg2_key1 Then HotKeySet($msg2_key1, "on_msg2_1")
- If $msg3_key1 Then HotKeySet($msg3_key1, "on_msg3_1")
- EndFunc
- Func on_msg1_1()
- If allow_chat() Then
- msg1_action()
- Else
- hotkey_passthrough($msg1_key1, "on_msg1_1")
- EndIf
- EndFunc
- ; skipping msg2 and msg3
- Func allow_chat()
- Return ( is_idle() Or is_paused() ) And Not is_click_only() And Not is_in_chat()
- EndFunc
- Func msg1_action()
- chat($msg1)
- ; unstick_keys() ; function moved to end of common chat function
- EndFunc
- ; main program
- While 1
- ; do some stuff
- If something Then func_1()
- ElseIf something Then func_2()
- ElseIf something Then func_3()
- EndIf
- If something Then do_exit()
- sleep(50)
- unstick_keys()
- WEnd
- Func func_1()
- For ........
- For .....
- ;do stuff
- sleep(50)
- unstick_keys()
- Next
- Next
- EndFunc
- ; func2 and func3 similar to func1, has call in inner loop
Add Comment
Please, Sign In to add comment