Simulate CTRL+A and CTRL+C

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Simulate CTRL+A and CTRL+C

Post by jak64 »

Good morning,

Can we, with Purebasic, simulate CTRL+A, CTRL+C and copy the result into a variable or a text file?

Thank you for your help.
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Simulate CTRL+A and CTRL+C

Post by jak64 »

Hello AZJIO
Thanks for the link, I looked but I don't understand anything!

Is there an example to do a CTRL + A and a CTRL + C?
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Simulate CTRL+A and CTRL+C

Post by Marc56us »

It depends on the source of what you want to copy.
If it's just the content of a gadget with text, then it's easy:

Code: Select all

SetClipboardText( GetGadgetText( ... ) )
:wink:
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Simulate CTRL+A and CTRL+C

Post by jak64 »

Hello Marc56us,

I want to search in google and copy the result of what is displayed.

I tried to download the page but I don't get everything, that's why I want:

1) Create a web gadget
2) Launch my google search (example "Release date song Lionel Richie Can't get over you"
3) Copy the displayed result into a string variable
4) Find the release date of the song in the string variable

See picture

Image
ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: Simulate CTRL+A and CTRL+C

Post by ZX80 »

Hi, jak64.

Try this.

Good luck !
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Simulate CTRL+A and CTRL+C

Post by jak64 »

Hello ZX80,

thanks, i will test this in my code.
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Simulate CTRL+A and CTRL+C

Post by jak64 »

Hello ZX80,
I tried with CTRL + A, it doesn't work
ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: Simulate CTRL+A and CTRL+C

Post by ZX80 »

@jak64

May also need this.

Code: Select all

...
; Check To see If we are the foreground thread
  
  foregroundThreadID = GetWindowThreadProcessId_(GetForegroundWindow_(), 0)
  ourThreadID = GetCurrentThreadId_()
  ; If not, attach our thread's 'input' to the foreground thread's
  
  If (foregroundThreadID <> ourThreadID)
    AttachThreadInput_(foregroundThreadID, ourThreadID, #TRUE);
  EndIf
  ...
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Simulate CTRL+A and CTRL+C

Post by jak64 »

Hello ZX80,
Hello ZX80,
I don't understand anything at all, the content of the WEb page is not selected

Here is my code:

Code: Select all

EnableExplicit

;----- Enumérations
Enumeration
  #FenetreWindows
  #WebGadget
EndEnumeration

;----- Constantes
#debut_url="https://www.google.com/search?q="
#fin_url=""

;----- Variables
Global Event.q
Global Chanson.s
Global RequeteGoogle.s
Global Resultat.s
Global foregroundThreadID
Global ourThreadID
Global Dim SendInputData.INPUT(3)


; press CONTROL
SendInputData(0)\type = #INPUT_KEYBOARD
SendInputData(0)\ki\wVK = #VK_CONTROL

; press A
SendInputData(1)\type = #INPUT_KEYBOARD
SendInputData(1)\ki\wVK = #VK_A

; release A
SendInputData(2)\type = #INPUT_KEYBOARD
SendInputData(2)\ki\dwFlags = #KEYEVENTF_KEYUP
SendInputData(2)\ki\wVK = #VK_A

; release CONTROL
SendInputData(3)\type = #INPUT_KEYBOARD
SendInputData(3)\ki\dwFlags = #KEYEVENTF_KEYUP
SendInputData(3)\ki\wVK = #VK_CONTROL


OpenWindow(#FenetreWindows, 0, 0, 1366, 768, "", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  
Chanson="date chanson Michael Jackson Thriller"
RequeteGoogle=URLEncoder(#debut_url + Chanson + #fin_url  )

WebGadget(#WebGadget, 0, 0, 1366, 768, RequeteGoogle)

SetActiveGadget(#WebGadget)

  foregroundThreadID = GetWindowThreadProcessId_(GetForegroundWindow_(), 0)
  ourThreadID = GetCurrentThreadId_()
  ; If not, attach our thread's 'input' to the foreground thread's
  
  If (foregroundThreadID <> ourThreadID)
    AttachThreadInput_(foregroundThreadID, ourThreadID, #True);
  EndIf

SendInput_(1, @SendInputData(0), SizeOf(INPUT))
Delay(30)

SendInput_(1, @SendInputData(1), SizeOf(INPUT))
Delay(200)


SendInput_(1, @SendInputData(2), SizeOf(INPUT))
Delay(30)

SendInput_(1, @SendInputData(3), SizeOf(INPUT))
Delay(30)
        
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow


ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: Simulate CTRL+A and CTRL+C

Post by ZX80 »

@jak64

During the pause that I set for the test of 5 seconds, just click on the working field of the page.
It works. You only need set the focus to the working field, and then remove the selection.

Code: Select all

Delay(5000)

Dim SendInputData.INPUT(5)


; press CONTROL
SendInputData(0)\type = #INPUT_KEYBOARD
SendInputData(0)\ki\wVK = #VK_CONTROL

; press A
SendInputData(1)\type = #INPUT_KEYBOARD
SendInputData(1)\ki\wVK = #VK_A

; release A
SendInputData(2)\type = #INPUT_KEYBOARD
SendInputData(2)\ki\dwFlags = #KEYEVENTF_KEYUP
SendInputData(2)\ki\wVK = #VK_A

; press C
SendInputData(3)\type = #INPUT_KEYBOARD
SendInputData(3)\ki\wVK = #VK_C

; release C
SendInputData(4)\type = #INPUT_KEYBOARD
SendInputData(4)\ki\dwFlags = #KEYEVENTF_KEYUP
SendInputData(4)\ki\wVK = #VK_C

; release CONTROL
SendInputData(5)\type = #INPUT_KEYBOARD
SendInputData(5)\ki\dwFlags = #KEYEVENTF_KEYUP
SendInputData(5)\ki\wVK = #VK_CONTROL


For i=0 To 5
  SendInput_(1, @SendInputData(i), SizeOf(INPUT))
  Delay(200)
Next i
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Simulate CTRL+A and CTRL+C

Post by AZJIO »

jak64 wrote: Sat Apr 08, 2023 4:53 pm I looked but I don't understand anything!
You repeat this question.
The day before yesterday I tried to use Ctrl+A, but it didn't work. If I use SetActiveGadget() it still doesn't place the text cursor inside the page. When using CHM files, you can move the cursor to the page with the F6 key and use Ctrl + A, but this does not work in the gadget, you have to click, and this is not reliable, since you can accidentally click on the link. I don't have a solution.
BarryG
Addict
Addict
Posts: 4122
Joined: Thu Apr 18, 2019 8:17 am

Re: Simulate CTRL+A and CTRL+C

Post by BarryG »

jak64 wrote: Sat Apr 08, 2023 5:37 pm2) Launch my google search (example "Release date song Lionel Richie Can't get over you"
3) Copy the displayed result into a string variable
4) Find the release date of the song in the string variable
This will get you the Google search result; now you have to find the "1996" in it (it's there). It will be hard to extract, and maybe RegEx will be needed.

Don't forget that the format of Google's results can and will likely change, so whatever method you use to extract today will probably not work in a month.

It'd be better and more reliable to get the search result from a dedicated online music library, if possible.

Code: Select all

Procedure.s GetURLText(url$)
  *Buffer = ReceiveHTTPMemory(URLEncoder(url$))
  If *Buffer
    Size = MemorySize(*Buffer)
    text$ = PeekS(*Buffer, Size, #PB_UTF8 | #PB_ByteLength)
    FreeMemory(*Buffer)
  EndIf
  ProcedureReturn text$
EndProcedure

Debug GetURLText("https://www.google.com/search?q=Lionel Richie Can't get over you release date")
Allen
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Nov 10, 2021 2:05 am

Re: Simulate CTRL+A and CTRL+C

Post by Allen »

@Jak64

If you can accept the limitation of using external chrome.exe instead of PB webgaget, you can try below method.

Code: Select all

Procedure WebPage2Clipboard()
  Protected.INPUT InputKey
  ; Control key Down
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = #VK_CONTROL
  InputKey\ki\dwFlags = 0
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
  ;a press
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = 65
  InputKey\ki\dwFlags = 0
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
  ; a release
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = 65
  InputKey\ki\dwFlags =  #KEYEVENTF_KEYUP
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
  ;c press
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = 67
  InputKey\ki\dwFlags = 0
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
  ;c release
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = 67
  InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)

  ;Control key up
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = #VK_CONTROL
  InputKey\ki\dwFlags = #KEYEVENTF_KEYUP
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
EndProcedure

#debut_url="https://www.google.com/search?q="
#fin_url=""
Chanson$="date chanson Michael Jackson Thriller"
RequeteGoogle$=URLEncoder(#debut_url + Chanson$ + #fin_url  )

RunProgram("C:\Program Files (x86)\Google\Chrome\Application\chrome_proxy.exe",RequeteGoogle$,"")
Delay(4000); wait till chrome becomes stable
;OriginalClipBoard$=GetClipboardText()
WebPage2Clipboard()
A$=GetClipboardText()
Debug a$
;SetClipboardText(OriginalClipBoard$)
Thanks

Allen
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Simulate CTRL+A and CTRL+C

Post by jak64 »

Hello BarryG,

I tried to use musical bases but I did not find how to exploit them.
Post Reply