WorknMan Posted March 3, 2010 Share Posted March 3, 2010 So I've written an AutoIt script that is fired by a global hotkey, with the purpose being to copy some selected text from the active window and then do some stuff with it. The way I am attempting to do this is to have AutoIt send CTRL+C to the active window where text is selected, but this has been problematic; sometimes it works, and sometimes it doesn't. Here is my code: ;Get window title, in case we need it $window_title = WinGetTitle("[active]") ClipPut("") ;Clear the clipboard, so we can check for data when we write to it $readtext = "" ;We'll put new clipboard data in this variable ;For $i = 15 to 1 Step -1 ;Next Send("^c") ;Send CTRL+C to the active window $readtext = ClipGet() ;Any text on the clipboard? If $readtext <> "" Then return $readtext EndIf ;Try alernate method to get clipboard text ControlSend($window_title, "", "", "^c") If $readtext <> "" Then return $readtext EndIf The rate at which this is successful usually depends on the application I'm trying it with. With some apps (such as Firefox), it works most of the time. With other apps (such as IE8 and Metapad), it fails more often than it succeeds. Sometimes, if I try again right after a failed attempt (twice in a row), it'll work without me having to switch windows or anything. And sometimes, it'll just send a 'c' to the active window instead of CTRL+C. Anyway, I'm trying to increase the rate at which this succeeds. Do you guys have any ideas? Link to comment Share on other sites More sharing options...
martin Posted March 3, 2010 Share Posted March 3, 2010 (edited) With your ControlSend method you have omitted reading the clipboard. It might be worth adding something in your hotkey function to wait untill the hotkey has been released otherwise it might effect the Send. Instead of Ctrl C you could also try Ctrl Insert. Edited March 3, 2010 by martin Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
WorknMan Posted March 5, 2010 Author Share Posted March 5, 2010 Tried using CTRL+insert and it's working about a 99% success rate now. Thanks for tip Link to comment Share on other sites More sharing options...
WorknMan Posted March 9, 2010 Author Share Posted March 9, 2010 With your ControlSend method you have omitted reading the clipboard.It might be worth adding something in your hotkey function to wait untill the hotkey has been released otherwise it might effect the Send. Instead of Ctrl C you could also try Ctrl Insert.Ok, this isn't working out quite as well as I had hoped, because although the accuracy has improved, now if I'm typing in text, it's putting me into insert mode sometimes You said it might be worth adding something in your hotkey function to wait untill the hotkey has been released otherwise it might effect the Send'. Can you elaborate on this? I use Autohotkey for all of my global hotkeys, and one of them inparticular calls an AutoIt script, which actually sends the CTRL+Insert/CTRL+C key to the active window, and then does other stuff. Link to comment Share on other sites More sharing options...
Fulano Posted March 9, 2010 Share Posted March 9, 2010 This has worked very, very well for me:Func copySelected () Local $oldClip = ClipGet() If WinActive ("ACT!") Then ControlSend ("ACT", "", "[Name:Connected Menus]", "ec") Else ClipPut ("") Send ("^c") EndIf Send ("{CTRLDOWN}{CTRLUP}") For $i = 0 to 20 Sleep (10) if ClipGet() Then ExitLoop Next Local $clipStuff = ClipGet () ClipPut ($oldClip) Return $clipStuff EndFuncNote that I did have to add a handler for ACT!, which copies the contact data en masse rather than just the selected field, so I had to modify it's behavior slightly.This also saves and restores the clipboard, so the clipboard doesn't get clobbered. #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja! Link to comment Share on other sites More sharing options...
WorknMan Posted April 5, 2010 Author Share Posted April 5, 2010 I ended up using the following code, which is working with > 99% success rate, with none of the 'sticky key' problems I was having before: expandcollapse popupFunc _GetTextFromActiveWindow() ;Get window title, in case we need it $window_title = WinGetTitle("[active]") if $window_title == "" Then SoundPlay(@WindowsDir & "\media\Windows XP Error.wav",1) Exit EndIf WinActivate($window_title) ClipPut("") ;Clear the clipboard, so we can check for data when we write to it $readtext = "" ;We'll put new clipboard data in this variable For $i = 1 to 8 Step 1 $rand = random (1, 4, 1) Send ("{CTRLDOWN}") ;Try various methods until we get text on the clipboard Switch $rand Case 1 Send("{INS}") Case 2 ControlSend($window_title, "", "", "{INS}") Case 3 Send("c") Case 4 ControlSend($window_title, "", "", "c^") EndSwitch Send ("{CTRLUP}") $readtext = ClipGet() ;Any text on the clipboard? If $readtext <> "" Then return $readtext EndIf If $i = 4 Then WinActivate($window_title) Sleep(100) EndIf Next return $readtext EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now