I have put the following script together to try to grab any highlighted text from "any" window (well as many as possible) including browser windows, the intension being to assign the text to a variable for further use. I am using a combination of CTRL + Right mouse click once any text has been highlighted, this is a bit messy in as much as its not always possible to cleanly close the right click menu after the trigger combination. I did try using ALT + Right mouse click but had trouble with the script sending CTRL + ALT + C which shuts things down. the reason for using both a key + mouse click is it keeps the focus on the window of the highlighted text, I have found a hotkey only is more likely to loose focus to some other window when the user moves to press the hotkey. It seems to work reasonably well from some limited testing but was wondering if any one else had been able to do anything better?
;---------------------------
; Script to capture highligthed text via clipboard
; Function to wait for keys & mouse clicks
; Note use decimal of the hex codes from
; http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
#DefineFunction IsKeyDownEx(virtualkeycode)
ret = DllCall(StrCat(DirWindows(1),"user32.dll"), long:"GetAsyncKeyState", long:virtualkeycode)
If ret != 0 Then ret = 1
Return ret
#EndFunction
; Function to handle clipboard store & restore any contents
; & get highlighted text
#DefineFunction GetText()
WindowTitle = WinGetActive()
OrigClip = ClipGet() ; Get any existing content
;Display(3, "About to Activate", WindowTitle)
WinActivate(WindowTitle)
SendKey("^c") ; try to get the highlighted text
Display(3, "Selected Text:", ClipGet()) ; ****Assign to variable if needed****
WinActivate(WindowTitle) ;Activate original window to close right click menu
ClipPut(OrigClip) ;restore original contents
#EndFunction
; Wait for trigger key and right mouse click
;While @True
; Key = IsKeyDownEx(17) & IsKeyDownEx(2) ;CTRL = 17 ;ALT = 18 ;RMouse = 2
; If Key == 1
; GetText()
; Endif
; ;Endif
; TimeDelay(.5)
; If IsKeyDownEx(27) then exit
;EndWhile
; Modified so it does not keep calling GetText() repeatedly if the trigger key is held down
Count = 0
While @True
Key = IsKeyDownEx(17) & IsKeyDownEx(2) ;CTRL = 17 ;ALT = 18 ;RMouse = 2
If Key == 1
Count = (Count + 1)
If Count == 1
KeyT = 1
Endif
;BoxText (KeyT)
If KeyT == 1 then GetText() ; Fire once
KeyT = 0 ; Reset key trigger
Endif
TimeDelay(.1)
If Key == 0 then Count = 0 ; Reset count when hot key released
If IsKeyDownEx(27) then exit
EndWhile
;-------------------------
Thanks JW
Its not as easy as it would first appear.
Have you considered using on of the function keys such as F12 instead of the mouse right click?.
I notice that your script sometimes has the issue of not obtaining the actual active window on Windows 7. I am wondering if this issue is related to this hotkey issue on Windows 7, where the active window gets lost. http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/Shortcut~Information+Use~a~Hotkey~to~Sendkeys~to~an~Active~Window.txt. You might consider using this UDF instead of WinGetActive.
Quote from: Deana on January 07, 2014, 12:05:14 PM
Its not as easy as it would first appear.
Have you considered using on of the function keys such as F12 instead of the mouse right click?.
I notice that your script sometimes has the issue of not obtaining the actual active window on Windows 7. I am wondering if this issue is related to this hotkey issue on Windows 7, where the active window gets lost. http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/Shortcut~Information+Use~a~Hotkey~to~Sendkeys~to~an~Active~Window.txt. You might consider using this UDF instead of WinGetActive.
Yes I have considered using function keys but this does mean that there is an opportunity for other windows to take focus, the right click seems to keep focus better while highlighting the text to be copied. Is the Win 7 loss of focus only apparent when using keys as a trigger? I am considering a multipronged approach if there is no window title returned to get the reference to set focus.
Thanks JW
Ok it looks like a properly positioned TimeDelay before the SendKey is quite helpful. Here is the simplified code sample I came up with that seems to work well on Windows 7.
ClipPut('');initialize clipboard
While WaitForKey('!{F12}','','','','')
title = WinGetActive()
TimeDelay(0.5) ;IMPORTANT DO NOT REMOVE
SendKeysTo(title, '^c')
data = ClipGet()
if data == '' then continue
Pause(title,data)
;BoxOpen(title,data)
;timedelay(2)
;BoxShut()
ClipPut('')
EndWhile
Exit