Guest User

Untitled

a guest
Jan 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #cs
  2. This is my function for handling stuck ctrl, alt, win, & shift keys from a farmtown
  3. bot program I am working on (Thanks Timehorse and Jackalo for the farmville bot that I
  4. started from). Anyone familier with that script will certainly recognize the logic
  5. that I "borrowed" from Timehorse :)
  6.  
  7. in particular - I was running into big problems when I implemented a chat routine and
  8. used ctrl+key hotkeys to access it. Frequently the ctrl key would get stuck. I found
  9. that problem also existed for alt+key sequences... didn't really test the shift/win
  10. sequences, but through those keys in too.
  11. #ce
  12.  
  13. #include <Timers.au3>
  14. #Include <Array.au3>
  15.  
  16. ; Keyboard cleanup handler
  17. Global $user32dll = DllOpen("user32.dll") ; should be cleaned up at exit
  18. Global $key_down_too_long = 1000 ; if key held down over a second reset it
  19.  
  20. ; Global Array for timer functions corresponding to keys defined below
  21. Global $key_timer[8] = [0, 0, 0, 0, 0, 0, 0, 0]
  22.  
  23. ; Keys of interest are hotkey modifiers for ctrl, alt, win, and shift
  24. Global Const $keys[8] = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x5b, 0x5c]
  25. ;0xa0 LSHIFT
  26. ;0xa1 RSHIFT
  27. ;0xa2 LCTRL
  28. ;0xa3 RCTRL
  29. ;0xa4 LALT
  30. ;0xa5 RALT
  31. ;0x5b LWIN
  32. ;0x5c RWIN
  33.  
  34. Global $in_chat = False
  35.  
  36. ; Hot Keys
  37. Global $msg1_key1 = "^{UP}"
  38. Global $msg2_key1 = "^{LEFT}"
  39. Global $msg3_key1 = "^{RIGHT}"
  40.  
  41. Func unstick_keys($force_unstick=False)
  42. Local $i
  43.  
  44. ;Format of DllCall to press/release a key
  45. ;DllCall($dll,"int","keybd_event","int",$vkvalue,"int",0,"long",0,"long",0) ;To press a key
  46. ;DllCall($user32dll,"int","keybd_event","int",$vkvalue,"int",0,"long",2,"long",0) ;To release a key
  47.  
  48. If $force_unstick Then
  49. For $vkvalue in $keys
  50. DllCall($user32dll,"int","keybd_event","int",$vkvalue,"int",0,"long",2,"long",0) ;Release each key
  51. Next
  52. Else
  53. $i = 0
  54. For $vkvalue in $keys
  55. If _IsPressed($vkvalue) Then
  56. If $key_timer[$i] = 0 Then
  57. $key_timer[$i] = _Timer_Init() ; initialize a timer to watch this key
  58. ElseIf TimerDiff($key_timer[$i]) >= $key_down_too_long Then ; check elapsed time
  59. DllCall($user32dll,"int","keybd_event","int",$vkvalue,"int",0,"long",2,"long",0) ; release the key
  60. $key_timer[$i] = 0 ; reset the timer
  61. EndIf
  62. EndIf
  63. $i = $i + 1
  64. Next
  65. EndIf
  66. EndFunc
  67.  
  68. Func do_exit() ; call for common exit point
  69. DllClose($user32dll)
  70. unstick_keys(True)
  71. Exit
  72. EndFunc
  73.  
  74. ; previously troublesome function here - can't use ControlSend because the
  75. ; input field is part of a flash game app
  76. Func chat($text)
  77. $in_chat = True ; want to keep from re-entering function before it is finished
  78.  
  79. Local $mymsg = $text
  80. Local $sav_pos = MouseGetPos() ; remember mouse position
  81. Local $i
  82.  
  83. MouseClick("primary", $chatx, $chaty, 1, 4) ; click input area
  84.  
  85. For $i = 1 to Mod($msgdotcount, 2) ; flash (or Farmtown) wants different message each time
  86. $mymsg = $mymsg & "."
  87. Next
  88.  
  89. Send($mymsg, 1)
  90. Sleep(100)
  91. Send("{ENTER}", 0)
  92. MouseMove($sav_pos[0], $sav_pos[1], 1) ; restore mouse
  93.  
  94. $msgdotcount = $msgdotcount + 1
  95. unstick_keys()
  96. $in_chat = False
  97. EndFunc
  98.  
  99. ; put calls to unstick_keys at bottom of every hot-key handler function
  100. ; also place calls wherever you think they might be handy in your code
  101.  
  102. Func clear_hotkeys()
  103. If $msg1_key1 Then HotKeySet($msg1_key1)
  104. If $msg2_key1 Then HotKeySet($msg2_key1)
  105. If $msg3_key1 Then HotKeySet($msg3_key1)
  106. EndFunc
  107.  
  108. Func activate_hotkeys()
  109. If $msg1_key1 Then HotKeySet($msg1_key1, "on_msg1_1")
  110. If $msg2_key1 Then HotKeySet($msg2_key1, "on_msg2_1")
  111. If $msg3_key1 Then HotKeySet($msg3_key1, "on_msg3_1")
  112. EndFunc
  113.  
  114. Func on_msg1_1()
  115. If allow_chat() Then
  116. msg1_action()
  117. Else
  118. hotkey_passthrough($msg1_key1, "on_msg1_1")
  119. EndIf
  120. EndFunc
  121.  
  122. ; skipping msg2 and msg3
  123. Func allow_chat()
  124. Return ( is_idle() Or is_paused() ) And Not is_click_only() And Not is_in_chat()
  125. EndFunc
  126.  
  127. Func msg1_action()
  128. chat($msg1)
  129. ; unstick_keys() ; function moved to end of common chat function
  130. EndFunc
  131.  
  132. ; main program
  133. While 1
  134. ; do some stuff
  135. If something Then func_1()
  136. ElseIf something Then func_2()
  137. ElseIf something Then func_3()
  138. EndIf
  139.  
  140. If something Then do_exit()
  141.  
  142. sleep(50)
  143. unstick_keys()
  144. WEnd
  145.  
  146. Func func_1()
  147. For ........
  148.  
  149. For .....
  150. ;do stuff
  151. sleep(50)
  152. unstick_keys()
  153. Next
  154. Next
  155. EndFunc
  156.  
  157. ; func2 and func3 similar to func1, has call in inner loop
Add Comment
Please, Sign In to add comment