Sign in to follow this  
Followers 0
spike_s2

Function _isPressed() not capturing keys ingame

9 posts in this topic

Function _isPressed() not capturing keys ingame.

It is working properly whenever the game is not running. The game is a directx game, which runs full screen.

Following is a sample code. Please let me know how to make the autoit program capture the keys that are sent ingame.

Func _IsPressed($hexKey)

; $hexKey must be the value of one of the keys.

; _IsPressed will return 0 if the key is not pressed, 1 if it is.

; $hexKey should entered as a string, don't forget the quotes!

; (yeah, layer. This is for you)

Local $aR, $bO

$hexKey = '0x' & $hexKey

$aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)

If Not @error And BitAND($aR[0], 0x8000) = 0x8000 Then

$bO = 1

Else

$bO = 0

EndIf

Return $bO

EndFunc ;==>_IsPressed

.....

While 1

$MSG = GUIGetMsg()

Select

Case $MSG = $GUI_EVENT_CLOSE

Exit

Case $MSG = $BTNSET

;_SetControls()

;GUICtrlSetData($MB1, $MB1Func)

;GUICtrlSetData($MB2, $MB2Func)

EndSelect

If _IsPressed("71") Then

GUICtrlSetData($LBLSTATE, "Running")

MsgBox(1,"","Capture")

;_ProcessKeys()

EndIf

WEnd

Share this post


Link to post
Share on other sites



Function _isPressed() not capturing keys ingame.

It is working properly whenever the game is not running. The game is a directx game, which runs full screen.

Following is a sample code. Please let me know how to make the autoit program capture the keys that are sent ingame.

Func _IsPressed($hexKey)

; $hexKey must be the value of one of the keys.

; _IsPressed will return 0 if the key is not pressed, 1 if it is.

; $hexKey should entered as a string, don't forget the quotes!

; (yeah, layer. This is for you)

Local $aR, $bO

$hexKey = '0x' & $hexKey

$aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)

If Not @error And BitAND($aR[0], 0x8000) = 0x8000 Then

$bO = 1

Else

$bO = 0

EndIf

Return $bO

EndFunc ;==>_IsPressed

.....

While 1

$MSG = GUIGetMsg()

Select

Case $MSG = $GUI_EVENT_CLOSE

Exit

Case $MSG = $BTNSET

;_SetControls()

;GUICtrlSetData($MB1, $MB1Func)

;GUICtrlSetData($MB2, $MB2Func)

EndSelect

If _IsPressed("71") Then

GUICtrlSetData($LBLSTATE, "Running")

MsgBox(1,"","Capture")

;_ProcessKeys()

EndIf

WEnd

Try HotkeySet like that :

HotkeySet("{YourKey}","Your Function")

No-life of autoit...what could be better ?LAST SCRIPTS WITH AUTO-IT : CLICK HERE

Share this post


Link to post
Share on other sites

I need to capture the left mouse button click. I don't know of a way of doing that with Hotkey.

Share this post


Link to post
Share on other sites

Have you tried mouse hooks instead?

Share this post


Link to post
Share on other sites

I've seen quiet a lot talks about keyboard/mouse hooks on this forum, most of them point to some mysterious "Larry's hook" topic which is gone, so at the end, most of the topics became useless.

So a little "practical" assistance regarding this would be very helpful...

The thing is, directx is tend to block keyboard events for "outside" applications and without low level keyboard hook AU3 is pretty much useless tool.

AutoHotkey on other hand has a very nice little feature #InstallKeybdHook and #InstallMouseHook just add these two lines to the code and you all set, your hotkey will work from most applications.

In this topic you can see an example how to use keyboard hook to log the key presses (it even works in games), however I don't know how to use that method for hotkeys, where two or three keys must be pressed at the same time...

It would be nice have a keyboard hook that would work with HotkeySet() function...

Share this post


Link to post
Share on other sites

Oh? I didn't even think about Larry's hooks having gone missing. Would've been nice if he didn't go deleting his posts when he disappeared.

Share this post


Link to post
Share on other sites

I found MrCreatoR's code which uses mouse hooks in order to capture mouse events.

Following is the link to the post.

http://www.autoitscript.com/forum/index.php?showtopic=64738

I modified the code accordingly and tested it in the directx game.

Still not working !!! The program is unable to capture mouse events from ingame.

The code I tested. Works well outside the game.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <MouseSetOnEvent_UDF.au3>
#include <GuiConstants.au3>

Opt('MustDeclareVars', 1)

Global $LBLSTATE
Global $MBState;  1-started 0-stoped

$MBState=0
;using middle mouse button to control program
;capturing middle mouse button and enabling function
_Main()


;was unable to set any other mouse buttons then left or right. for my application wheel scroll would do as well
;middle mouse botton had the windows vista function even after capture
;extra mouse button did not work at all
Func MouseWheelScroll()
    if $MBState == 0 then 
    ;capture LMB, enable function
        _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "MousePrimaryDown")
    ;capture started
        $MBState=1
        GUICtrlSetData($LBLSTATE,"Capture started")
    ElseIf $MBState == 1 Then
    ;stop capture
        _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)
        $MBState=0
        GUICtrlSetData($LBLSTATE,"Capture stopped")
    EndIf
EndFunc

Func MousePrimaryDown()
;process left mouse button click
;disable capture so we can send our sequence
    _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT)
;send sequence
    MouseClick("left")
    sleep(100)
    MouseClick("right")
;reenable capture for next sequence
    _MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT, "MousePrimaryDown")
EndFunc

Func _Main()

    Local $MSG, $parent

    $parent=GUICreate("Mouse Controls", 440, 200, 100, 100) ; will create a dialog box
    
    $LBLSTATE = GUICtrlCreateInput("Initialised: Stopped", 180, 100, 120, 30, $ES_READONLY)
    GUICtrlSetTip(-1, "Program State: Press middle mouse button to start/stop the program")

    
    GUISetState()   
    
;enable wheel scroll capture
    _MouseSetOnEvent( $MOUSE_WHELLSCROLL_EVENT, "MouseWheelScroll")
    

; Run the GUI until the dialog is closed
    While 1
        Sleep ( 100 )
        $MSG = GUIGetMsg()  
        Select
            Case $MSG = $GUI_EVENT_CLOSE
            ;reenable wheel scroll
                _MouseSetOnEvent( $MOUSE_WHELLSCROLL_EVENT)
                Exit
        EndSelect   

    WEnd
            
EndFunc  ;==>_Main

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Yes, its a shame what happened to Larry (I've read about it on his yahoo tech group)...

Anyway with hope that this might bring us one step closer to a working solution, here is Larry's keyboard hook script:

#include <DllCallBack.au3>

Global Const $WH_KEYBOARD_LL = 13

Global $hHook, $pStub_KeyProc

Global $pStub_KeyProc = _DllCallBack ("_KeyProc","int;ptr;ptr")

Global $hmod = DllCall("kernel32.dll","hwnd","GetModuleHandle","ptr",0)

Global $hHook = DllCall("user32.dll","hwnd","SetWindowsHookEx","int",$WH_KEYBOARD_LL, _
                                "ptr",$pStub_KeyProc,"hwnd",$hmod[0],"dword",0)
Global $buffer = ""

While 1
    Sleep(10)
WEnd

Func _KeyProc($nCode, $wParam, $lParam)
    Local $ret,$KEYHOOKSTRUCT
    
    If $nCode < 0 Then
        $ret = DllCall("user32.dll","long","CallNextHookEx","hwnd",$hHook[0], _
                        "int",$nCode,"ptr",$wParam,"ptr",$lParam)
        Return $ret[0]
    EndIf
    
    If $wparam = 256 Then
        $KEYHOOKSTRUCT = DllStructCreate("dword;dword;dword;dword;ptr",$lParam)
        $buffer = Chr(DllStructGetData($KEYHOOKSTRUCT,1))
        ToolTip($buffer)
    EndIf
    
    $ret = DllCall("user32.dll","long","CallNextHookEx","hwnd",$hHook[0], _
                    "int",$nCode,"ptr",$wParam,"ptr",$lParam)
    Return $ret[0]
EndFunc

Func OnAutoItExit()
    DllCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hHook[0])
EndFunc

And in case you are missing DllCallBack.au3 (as I did after fresh installing lattest AU3 version) you can find it here

[EDIT]

While I was typing all this two replies were posted....

Edited by vanowm

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0