Jump to content

AutoIt equivalent for OnCopy


Recommended Posts

I have tried googling and searching the help file but to no avail.

 

May i know how to trigger a function when an editbox is being copied? Like the javascript function oncopy where you assign a function to a <input> that will trigger a function when text is being copy from it.

 

Any help is appreciated.

Link to comment
Share on other sites

One way could be subclassing to intercept WM_COPY messages
It's a code from Danyfirex (adapted)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3> 
#include <WinAPIShellEx.au3>


$hGui = GUICreate("Input", 300, 118 )
$idInput = GUICtrlCreateInput("", 50, 50, 200, 18)
$hInput = GUICtrlGetHandle($idInput )
$lab = GUICtrlCreateLabel( "copied text", 50, 20, 200, 18 )

  ; Register callback function to subclass Input control
$pInputProc = DllCallbackGetPtr( DllCallbackRegister( "InputProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) )
_WinAPI_SetWindowSubclass( $hInput, $pInputProc, 9999, 0 ) ; SubclassId = 9999, $pData = 0

GUISetState()

While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
WEnd

_WinAPI_RemoveWindowSubclass( $hInput, $pInputProc, 9999)
GUIDelete( $hGui )
Exit


; InputProc callback function
Func InputProc( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData )
  Switch $iMsg
    Case $WM_COPY
        GuiCtrlSetData($lab, ControlCommand($hGui, "", $idInput, "GetSelected", ""))
  EndSwitch
  Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0] ; _WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc

 

Link to comment
Share on other sites

On 5/2/2016 at 8:56 PM, mikell said:

One way could be subclassing to intercept WM_COPY messages
It's a code from Danyfirex (adapted)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3> 
#include <WinAPIShellEx.au3>


$hGui = GUICreate("Input", 300, 118 )
$idInput = GUICtrlCreateInput("", 50, 50, 200, 18)
$hInput = GUICtrlGetHandle($idInput )
$lab = GUICtrlCreateLabel( "copied text", 50, 20, 200, 18 )

  ; Register callback function to subclass Input control
$pInputProc = DllCallbackGetPtr( DllCallbackRegister( "InputProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) )
_WinAPI_SetWindowSubclass( $hInput, $pInputProc, 9999, 0 ) ; SubclassId = 9999, $pData = 0

GUISetState()

While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
WEnd

_WinAPI_RemoveWindowSubclass( $hInput, $pInputProc, 9999)
GUIDelete( $hGui )
Exit


; InputProc callback function
Func InputProc( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData )
  Switch $iMsg
    Case $WM_COPY
        GuiCtrlSetData($lab, ControlCommand($hGui, "", $idInput, "GetSelected", ""))
  EndSwitch
  Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0] ; _WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc

Thx, i dont fully understand the code but i am trying to learn. I just started coding autoit 2 weeks ago. :D This will take time.

Thx, i dont fully understand the code but i am trying to learn. I just started coding autoit 2 weeks ago. :D This will take time.

 

Just realised that edit box cannot control + A !! :angry:

Edited by cyxstudio
Link to comment
Share on other sites

Normally most people would suggest using an Accelerator for Ctrl + A to select all in the edit box, but since you're already subclassing the edit (an input box is the same as an edit, just different window styles) you can do it like this

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIShellEx.au3>
#include <GuiEdit.au3>

$hGui = GUICreate("Input", 300, 118)
$lab = GUICtrlCreateLabel("copied text", 50, 10, 200, 18)
$idInput = GUICtrlCreateEdit("", 10, 30, 280, 80)
$hInput = GUICtrlGetHandle($idInput)

; Register callback function to subclass Input control
$pInputProc = DllCallbackGetPtr(DllCallbackRegister("EditProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
_WinAPI_SetWindowSubclass($hInput, $pInputProc, 9999, 0) ; SubclassId = 9999, $pData = 0

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

_WinAPI_RemoveWindowSubclass($hInput, $pInputProc, 9999)
GUIDelete($hGui)
Exit


; InputProc callback function
Func EditProc($hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData)
    Switch ($iMsg)
        Case $WM_COPY
            GUICtrlSetData($lab, ControlCommand($hGui, "", $idInput, "GetSelected", ""))
        Case $WM_CHAR
            If ($wParam = 1) Then Return _GUICtrlEdit_SetSel($hWnd, 0, -1)
    EndSwitch
    Return DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)[0] ; _WinAPI_DefSubclassProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>EditProc

(Changed the input control to an edit, included GUIEdit.au3, and added the Case $WM_CHAR for the Switch on $iMsg)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...