Chika95 Posted April 21, 2008 Posted April 21, 2008 Hello all - I'm an advanced AutoIT user, however I'm having trouble trying to figure out how to capture keystokes from the user. The script is a gaming bot for LOTRO. I'm basically looking for a way to capture keybinds. For example, I'll have the script pop a small GUI and ask the user to press a keystroke (i.e "Control-4"). I want the script to recognize that as a "^4". Then I'll take that, save it to an ini, to recall later.In other words, I'm trying to duplicate a video games' key mapping.I think this guy was trying to do the same thing: http://www.autoitscript.com/forum/index.php?showtopic=58089Perhaps there is a way to use a DLLCall of "ASWhook.dll" to record keystrokes?Any help would be appreciated
JustinReno Posted April 22, 2008 Posted April 22, 2008 Keylogger!*click*He wasn't asking for a keylogger. Use _IsPressed() (Misc.au3) in the helpfile to start.He was just asking to see if the user pressed a correct key.Anyways, since you an advanced scripter, I thought you would have had the answer. If your wondering; yes, _IsPressed is the best answer.
Chika95 Posted April 22, 2008 Author Posted April 22, 2008 (edited) Thanks for the pointer to the _IsPressed() Function. Here is the completed script expandcollapse popup#comments-start AutoIt Version: 3.1.0 Title: Keypress Converter Author: BotXPert<LOTROBot@gmail.com> Script Function: Captures key strokes 0-9 w/ the Modifiers CTRL, SHIFT & ALT. Returns the appropriate string to place in a "Send()" function This was developed to capture and save user created key binds in a video game bot. Example: _GetKeyPress() function called User presses the SHIFT key User presses the 6 key Function will return the string "+6" The string can be saved to an ini file & variable to recall later in a Send($KeyPress) function #comments-end #include <Misc.au3> #include <GuiConstants.au3> $Keypress = _GetKeyPress() ;Save this to a variable inside a script to recall later MsgBox(0, "Key Pressed was:", $Keypress) ;Included here to verify Exit Func _GetKeyPress() Opt("GUICloseOnESC", 1) $HexKeyArray = StringSplit("10|11|12|30|31|32|33|34|35|36|37|38|39", "|") $HumanReadableName = StringSplit("SHIFT|CTRL|ALT|0|1|2|3|4|5|6|7|8|9", "|") $SendKey = StringSplit("+|^|!|0|1|2|3|4|5|6|7|8|9", "|") $ArrayElement = 0 $SaveModifierElement = 0 $KeyPressGUI = GUICreate("Push a Number", 200, 55, @DesktopWidth / 2 - 100, @DesktopHeight / 2 - 30, -1, $WS_EX_TOPMOST) $StatusInput = GUICtrlCreateInput("", 10, 5, 180, 20, 0x0801, $WS_EX_STATICEDGE) GUISetState() ;Unhide GUI While 1 If $SaveModifierElement = 0 Then GUICtrlSetData($StatusInput, "Push a Number or key Modifier") ;Display some text in the input box Else GUICtrlSetData($StatusInput,"Now Push A Number: """ & $HumanReadableName[$SaveModifierElement] & " - ?""" ) ;Display some text in the input box EndIf For $i = 1 To 13 If _IsPressed($HexKeyArray[$i]) = 1 Then $ArrayElement = $i ;Find if one keys in the array is pressed Next ;------------------------------- $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exit Case $ArrayElement > 0 And $ArrayElement <= 3 ;If one of the Modifier keys is pressed GUICtrlSetData($StatusInput, "Release " & $HumanReadableName[$ArrayElement] & ", and press a Number") ;Instruct user to release they key since this script can not detect multiple keys pressed at once $SaveModifierElement = $ArrayElement ; Save the element of the Modifier key that was pressed for later recall $ArrayElement = 0 ; Set the $ArrayElement to 0 for the next loop While _IsPressed($HexKeyArray[$SaveModifierElement]) = 1 ;Pause while the Modifier key is held down, instructing the user to release it Sleep(1) ;To reduce CPU load WEnd Case $ArrayElement >= 4 ;If one of the non Modifier keys is pressed ;Create the Yss and No buttons $ConfirmButton = GUICtrlCreateButton(" Yes ", 35, 25) GUICtrlSetColor($ConfirmButton, 0x339900) $RetryButton = GUICtrlCreateButton(" No, Retry ", 100, 25) GUICtrlSetColor($RetryButton, 0xFF3300) ;Ask user if he pressed a key or Modifier w/ key If $SaveModifierElement > 0 Then GUICtrlSetData($StatusInput, "Did you press: """ & $HumanReadableName[$SaveModifierElement] & " - " & $HumanReadableName[$ArrayElement] & """ ?") Else GUICtrlSetData($StatusInput, "Did you press: """ & $HumanReadableName[$ArrayElement] & """ ?") EndIf ;Loop until the user presses YES or NO ;------------------------------- While 1 $msg1 = GUIGetMsg() Select Case $msg1 = $GUI_EVENT_CLOSE Exit Case $msg1 = $ConfirmButton ;YES button is pused, Hide the window and return the appropriate send key If $SaveModifierElement > 0 Then GUISetState(@SW_HIDE) Return $SendKey[$SaveModifierElement] & $SendKey[$ArrayElement] Else GUISetState(@SW_HIDE) Return $SendKey[$ArrayElement] EndIf Case $msg1 = $RetryButton ;NO button is pushed, reset variables, destroy buttons, and loop to top $SaveModifierElement = 0 $ArrayElement = 0 GUICtrlDelete($ConfirmButton) GUICtrlDelete($RetryButton) ContinueLoop (2) EndSelect Sleep(1) ;To reduce CPU load WEnd ;------------------------------- EndSelect ;------------------------------- Sleep(1) ;To reduce CPU load WEnd EndFunc ;==>_GetKeyPress Edited April 22, 2008 by BotXpert
traylorre Posted July 26, 2012 Posted July 26, 2012 Nice work! I noticed that I needed this include to make your script run: #include <WindowsConstants.au3>
Moderators JLogan3o13 Posted July 26, 2012 Moderators Posted July 26, 2012 Hi, traylorre, welcome to the forum. Unfortunately, resurrecting a 4 year old post - especially one that violates the current forum rules against game bots, is not the most auspicious start. AutoIt has changed a lot in four years; I would suggest a thorough reading of the both the forum rules and the help file "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
Moderators Melba23 Posted July 26, 2012 Moderators Posted July 26, 2012 traylorre,Listen to the wise words above. The Forum rules can be found here - please read them before your next post. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts