Sign in to follow this  
Followers 0
Chika95

Capture Keystrokes

7 posts in this topic

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=58089

Perhaps there is a way to use a DLLCall of "ASWhook.dll" to record keystrokes?

Any help would be appreciated

Share this post


Link to post
Share on other sites



Keylogger!

*click*

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks for the pointer to the _IsPressed() Function.

Here is the completed script

#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 by BotXpert

Share this post


Link to post
Share on other sites

Nice work! I noticed that I needed this include to make your script run:

#include <WindowsConstants.au3>

Share this post


Link to post
Share on other sites

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 :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0